- Use launchd to activate the script at login and every hour thereafter
You may be familiar with AppleScript Folder Actions; compiled AppleScripts which, when attached to a folder, can perform tasks when a particular action occurs with that folder. For instance, a Folder Action can be attached to a folder that detects when new files are added to the folder and adds the files to iTunes. A drawback to Folder Actions, however, is that they only "pay attention" to files at the top level of the folder; changes to sub-folders are undetected. This kinda sorta rules out using a Folder Action to detect when changes occur to the iTunes Music folder.
Another all-AppleScript solution might be to run a Stay-Open script with an idle handler that periodically runs our rsync/osascript code. This just seems clunky to me, but it would work.
Instead, we will use launchd, a system service primarily used to launch programs at startup, to periodically run the script completely in the background. Now that's much more attractive.
Space does not allow me to explain exactly what is going on with launchd. So, to put it very simply: we will create a launchd agent .plist file that will be placed in your [username]/Library/LaunchAgents/ folder. This .plist file will contain instructions for launchd. At startup, launchd will retrieve these instructions.
This is the .plist file that is in my dougadams/Library/LaunchAgents/ folder on the Macbook Pro. You can copy this to a text editor and substitute your specific information as explained just afterwards:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Disabled</key> <false/> <key>Label</key> <string>com.dougadams.MySyncScript</string> <key>Program</key> <string>/usr/bin/osascript</string> <key>ProgramArguments</key> <array> <string>osascript</string> <string>/Users/dougadams/Library/Scripts/rsync_music_folder.scpt</string> </array> <key>RunAtLoad</key> <true/> <key>StartInterval</key> <integer>3600</integer> </dict> </plist>
Here are the sections you must change for your use:
The Label value may be written like this: com.someDomain.nameOfThisFile, where someDomain is, perhaps, your username or some other identifying label and nameOfThisFile is the file name this .plist file will be saved as; thus, this file would be saved as ~/Library/LaunchAgents/MySyncScript.plist
<key>ProgramArguments</key> <array> <string>osascript</string> <string>/Users/dougadams/Library/Scripts/rsync_music_folder.scpt</string> </array>
Change the file path to wherever you have saved the AppleScript we created in the last section.
<key>RunAtLoad</key> <true/> <key>StartInterval</key> <integer>3600</integer>
When to fire the script. I've set the value of the "RunAtLoad" key to true so that the script will run when "dougadams" logs in. Then, I've the set the "StartInterval" value to 3600 seconds, an hour. So the script will fire every hour as long as "dougadams" is logged in.
Save this as a plain text file, using the name you devised ("MySyncScript") and a ".plist" extension to your [username]/Library/LaunchAgents/ folder. (If a "LaunchAgents" folder doesn't exit, create it.) Now, log out and log back in (FYI, Apple says: "If you are installing a new job, do not make the user reboot or log out and back in to start the job. You can use launchctl to load your new job right away, without affecting other services or inconveniencing the user. There is no reason to interrupt the user's workflow to start a job."). Your script will be under launchd control and run at login and every hour thereafter. If you add new files to iTunes, they will be copied to the other user's iTunes automatically (er, eventually--the next time the script is activated).
Get More Information:
See Getting Started with launchd at Apple's website.
Craig Smith wrote the excellent Using launchd with AppleScript to Access a Flash Drive Automatically at MacScripter, which explains how to set up launchd to fire an AppleScript.