Auto update the iTunes library

Apr 27, '02 10:06:08AM

Contributed by: bluehz

I was searching for a way to automatically scan my Music folder with iTunes on a regular basis to keep my Library up to date. What seemed like an easy enough task turned into a nightmare of a trip - mainly because of the weird interaction between cron and Applescript.

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
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
[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:
chmod 755 filename
Now 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 <<EOF
-> 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
would 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.

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.

Comments (8)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20020427100608542