My goal was to write a simple cron task that called a simple AppleScript stored in a text file or compiled script. The AppleScript was easy enough - but getting it to run in cron was a DOG!!! I don't know exactly what the interaction is between cron and osascript - but it ain't pretty. I spent hours researching other peoples efforts and finally came up with a viable solution.
Read the rest of the article for the details...
The solution involves turning on the "Accept Remote AppleEvents" in the sharing control panel. Thats the first step. Next step is to create the text file containing the script. Create a text file (I used BBEdit), save with UNIX linendings and enter:
#!/bin/sh[Note: The "tell" line has been broken in two for easier reading; enter it as one line]. Replace VOLNAME and the path with the volume and path to your music folder(s). Save the text file to your choice of locations - I chose the Scripts menu directory. Then set the permissions to executeable:
osascript <<EOF
using terms from application "iTunes" of machine "eppc://127.0.0.1"
tell application "iTunes" of machine "eppc://127.0.0.1" to add
{"VOLNAME:Path:to:Music:"} to playlist "Library"
end using terms from
EOF
chmod 755 filenameNow edit your crontab or the System's crontab to run the script every hour, 15 mins, or whateever you want:
* 1 * * * sh "/path/to/text/file/saved/above"Thats it!
Interesting note here - the same technique can be used to access and control iTunes over an SSH connection to a remote machine. I must admit I did not figure this out myself but picked it up from various sources. Basically you will SSH into the remote machine - the first time in you will have to be at the remote machine to enter your login/pasword (store in keychain). Then you can pass command to the remote machine in the same way as above - always passing 127.0.0.1 as the IP address of the machine so the cmds are executed locally (local to the remote machine). For example:
osascript <<EOFwould cause the iTunes on the remote machine to play the next track. I won't expoundon this anymore as it has been covered in the forums and over at MacNN forums. Have a look - lots of possibilities here.
-> using terms from application "iTunes" of machine "eppc://127.0.0.1"
-> tell application "iTunes" of machine "eppc://127.0.0.1" to play the current track
-> end using terms from
-> EOF
I think the secret to the whole thing is enabling the Allow Remote AppleEvents - I don't know why (even on my local machine) but as soon as I did that all my cron/osascript errors disappeared.

