I think I am starting to get the hang of AppleScript, but the file/folder paths still seem to work strangely to me.
The following script works fine, taking new songs from a folder on the desktop and loading them into a new playlist in iTunes “ but only for my computer. If I move it to another machine the absolute path is broken.
tell application "iTunes"
set songfolder to alias "Macintosh HD:Users:username:Desktop:Newmusic:songs:"
if not (exists user playlist "NEWplaylist") then
make new user playlist with properties {name:"NEWplaylist", shuffle:false, song repeat:yes}
add songfolder to playlist "NEWplaylist"
end if
end tell
So I then I try all sorts of variations on the theme of the following, but
tell application "iTunes"
set songfolder to alias "Desktop:Newmusic:songs:" of user domain
if not (exists user playlist "NEWplaylist") then
make new user playlist with properties {name:"NEWplaylist", shuffle:false, song repeat:yes}
add songfolder to playlist "NEWplaylist"
end if
end tell
but I just get the error: Can’t get alias “Desktop:Newmusic:songs:” of user domain.
Any ideas where I am going wrong? Any hints or just outright abuse or ridicule would be much appreciated.
many thanks in advance - Anatole
Note: Another little puzzle is that I somteimes get an AppleScript error: iTunes got an error: AppleEvent timed out, possibly becuase iTunes is taking a little time to import the songs - is there anyway I can extend the time out or just stop the error being reported?
So since you’re using an alias in your script that’s what’s causing your problems when you move to another machine. Try replacing “alias” with “folder”, then applescript should follow the path you specify rather than looking for the original folder specified as an alias.
to make the paths portable, use path to desktop
and you can avoid timeout errors with a with timeout block
tell application "iTunes"
set songfolder to alias ((path to desktop as Unicode text) & "Newmusic:songs:")
if not (exists user playlist "NEWplaylist") then
make new user playlist with properties {name:"NEWplaylist", shuffle:false, song repeat:yes}
with timeout of 30 * minutes seconds
add songfolder to playlist "NEWplaylist"
end timeout
end if
end tell
Note: the folders Newmusic:songs: must exist on desktop
The first thing I se in this script that might cause errors is this:
set songfolder to alias “Macintosh HD:Users:username:Desktop:Newmusic:songs:”
With an alias reference, the folder “songs” of folder “Newmusic” of etc. must exist. If it doesn’t exist, then you’ll get an error. If this is not a static folder (i.e. it’s not permanent), then you’ll get errors when it does not exist.
That’s great Stefan - and thanks to the other replies too.
It works perfectly if I take out the alias:
tell application "iTunes"
set songfolder to ((path to desktop as Unicode text) & "Newmusic:songs:")
if not (exists user playlist "NEWplaylist") then
make new user playlist with properties {name:"NEWplaylist", shuffle:false, song repeat:yes}
with timeout of 30 * minutes seconds
add songfolder to playlist "NEWplaylist"
end timeout
end if
end tell
I’m still getting timeout errors. Really I just want AppleScript to ignore the transfer going on in the background and get on with it.
I tried this, getting all hopeful, but I still get a damned timeout error:
tell application "iTunes"
set songfolder to ((path to desktop as Unicode text) & "Newmusic:songs:")
if not (exists user playlist "NEWplaylist") then
make new user playlist with properties {name:"NEWplaylist", shuffle:false, song repeat:yes}
ignoring application responses
add songfolder to playlist "NEWplaylist"
end ignoring
end if
end tell
Normally applescript time-outs after a default period of time waiting for an application to report back that it has done its thing. You can change that time-out period like so…
with timeout of x seconds
– do something
end
So just make x into something large like 86400, which is a days worth of seconds. Make it a million if you want, it doesn’t mater. The applescript will respond normally when the transfer completes.
OK - but is there any way, I can just get it to ignore the progress of the activity it has triggered? I would rather that it doesn’t wait but continues to finish the script without any error dialogue. I was hoping that the “ignoring application responses” would do precisely this, but I still get a timeout error.
Thanks again - the sourcebook is the bit I read when I found this way of doing it. Unfortunately moving the ignoring application responses bit to enclose everything inside the tell iTunes statement doesn’t work either.
It looks like I will have to live with the extended timeout to eliminate the error, but have to put up with the wait.