Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Auto update the iTunes library Apps
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.
    •    
  • Currently 0.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (0 votes cast)
 
[6,537 views]  

Auto update the iTunes library | 8 comments | Create New Account
Click here to return to the 'Auto update the iTunes library' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Can't get event dictionary
Authored by: SeanAhern on Apr 27, '02 01:25:48PM

I haven't tried the script through cron, but it doesn't work from the command line yet.

It prompts me for my password (and asks if I want to add it to the keychain). Then it goes:

syntax error: Can't get the application's event dictionary. (-2709)

Any ideas?



[ Reply to This | # ]
Can't get event dictionary
Authored by: bluehz on Apr 28, '02 02:33:49AM

Since submitting the tip on Friday - some errant behaviour has cropped up - seems the osascript component is much buggier than first thought - sometimes it runs properly - sometimes not.

There is a lively discussion I started on the boards including a proposed solution.

http://forums.macosxhints.com/showthread.php?s=&postid=11756



[ Reply to This | # ]
just adding - no update
Authored by: csshsh on Apr 28, '02 07:27:56AM

that script is basicly just adding all the files of my mp3 directories. but not really updating them. its adding mp3s which are already in my playlists, and doesnt remove the mp3s listen in the library, which dont exist on the filesystem anymore..

anybody got a idea? =)



[ Reply to This | # ]
just adding - no update
Authored by: bluehz on Apr 28, '02 08:09:11AM

The script is very simple and nevr meant to remove files. All I wanted to avioid was having to manually scan my Music folder all the time - wanted it setup as a cron job.

You might try:

http://www.malcolmadams.com/itunes/

That place is definitely the authority on iTunes Applescripts and it seems like I saw one there that deleted files no longer in your library.



[ Reply to This | # ]
just adding - no update
Authored by: csshsh on Apr 28, '02 10:27:30AM

okay =)

yeah. i think i will mix some scripts.. like yours, then another one which
removes duplicates and at last one which removes non existing songs..

i hope it will work. hehe



[ Reply to This | # ]
just adding - no update
Authored by: bluehz on Apr 28, '02 11:41:39AM

Make sure to check out the thread in the forums about running osascript in cron....simple solution - but hard to figure it out. Using the solution provided - you can run your update via cron. Sure would like to anything you work up - maybe you could post here for us all.



[ Reply to This | # ]
just adding - no update
Authored by: DougAdams on Apr 28, '02 09:25:06PM
If you are interested, we have a script called Link Playlist to Folder; lets you associate an iTunes playlist with a folder of audio files such that additions and deletions in the folder can be updated in the playlist. Doug Doug's AppleScripts for iTunes

[
Reply to This | # ]
Another solution
Authored by: ilja on Apr 28, '02 04:01:49PM
iTunes bad library managment seems to be the main problem here. I wrote a little application in Cocoa that gives you a live view of your MP3 directory, works for multiple libraries and lets you playback songs. Its not finished yet, but you can already use it for listening to your MP3 collection instead of i(ll)Tunes.
If you have the time to check it out, you can get it from here (~300 kB).
Drop me a line if you like it. (e-mail included with the application).

[ Reply to This | # ]