- Part 2: The "Pull" Version
We can slightly change some of the commands we've already used to write a script that looks for changes on the remote user's machine and copies those files to the local machine.
Is the local iTunes active?
Instead of aborting the script if the remote iTunes is not running, we'll need to check to see if the local iTunes is running. Replace the are_you_logged_in() handler and the call to it with the following handler:
on itunes_is_running() tell application "System Events" to return (exists process "iTunes") end itunes_is_running
One additional file path variable
Later in the script we will need a "spaces-escaped" version of the local iTunes Music folder, so include a locMusicLibraryEscaped variable wihen formatting the other variables:
set locMusicLibraryEscaped to my replace_chars(locMusicLibrary, " ", "\\ ") set locMusicLibraryQuoted to quoted form of locMusicLibrary set remMusicLibraryEscaped to my replace_chars(remMusicLibrary, " ", "\\ ") set remMusicLibraryEscapedQuoted to quoted form of remMusicLibraryEscaped
Reverse the source and destination paths in the rsync routines
Instead of rsync looking for new files locally, we want to see what's new on the remote machine. Both rsync routines will have to switch their source and destination addresses:
-- == rsync dry run set theCommand to ("rsync -nEvauz --ignore-existing --exclude '.DS_Store'" & space & ¬ excludeThese & space & ¬ remAddr & ":" & remMusicLibraryEscapedQuoted & space & ¬ locMusicLibraryQuoted) set rsyncRez to do shell script theCommand log rsyncRez
Make the same address changes in the second "for real" rsync routine.
Change the perl routine's $base string to the local iTunes Music folder
We want to mate the partial files we retrieve with rsync with the path to the local iTunes Music folder using the new locMusicLibraryEscaped variable we initialized at the start:
-- == get file paths set perlCom to "perl -e' @rez=(" & (("\"") & my list_to_text(rezList, "\",\"") & ("\"")) & "); $base=\"" & locMusicLibraryEscaped & "\"; while(1){ $res=shift(@rez); last unless ($res!~m/.../); } while(1){ $res=pop(@rez); if ($res eq \"\"){last} } foreach $z(@rez) { push (@final,$base.$z.\"\\n\") unless ($z=~m/.\\/$/); } print sort @final; '" set perlRez to do shell script perlCom if perlRez is "" then return set filesToAdd to my text_to_list(perlRez, ASCII character 13) log filesToAdd
Add files to local iTunes
Finally! Plain ol' iTunes AppleScripting:
-- == add files to iTunes set trackAddedCounter to 0 repeat with fileToAdd in filesToAdd try set aliasToAdd to (POSIX file (fileToAdd)) as text with timeout of (3 * days) seconds tell application "iTunes" to add alias aliasToAdd set trackAddedCounter to (trackAddedCounter + 1) end timeout on error m number n log m log n end try end repeat
Get More Information:
See the man page for rsync. As manual pages go, it's very descriptive.
There is quite a lot of information on perl on the web. The routines (using shift, pop, while and foreach loops, push, unless, sort, print) are fairly basic and tutorials can be googled fruitfully.
Apple's Technical Note TN2065 should be read by anyone who uses do shell script, with especial regard to string quoting and escaping.