dougscripts.com

    rsync for real

  • rsync the two iTunes Music folders for real and add the newly copied files to the destination's iTunes

We'll run the same rsync command now, except we will remove the dry-run "n" flag from the options. rsync will copy the files and folders to the destination's iTunes Music folder using the Artist/Album/File structure from the source iTunes Music folder:1

-- == rsync for real

set theCommand to ("rsync -Evauz --stats --ignore-existing --exclude '.DS_Store'" & space & ¬
	locMusicLibraryQuoted & space & ¬
	remAddr & ":" & remMusicLibraryEscapedQuoted)

set rez to (do shell script theCommand)

log rez


We don't need the result from this, but I always like to log the result anyway in case there is an error to investigate.

The final routine is where the files just copied to the destination will be added to iTunes. For this we will ssh to "cleanuser" and use osascript to run an AppleScript. (Yes, an AppleScript running a do shell script that runs an AppleScript. While under some circumstances it might be just as well to use remote scripting, which would require the user's password, it will be easier and more secure to use ssh via the public key.)

repeat with fileToAdd in filesToAdd
	try
		with timeout of (3 * days) seconds
			set osaCom to quoted form of ¬
				("osascript -e 'tell application\"iTunes\" to add POSIX file \"" & fileToAdd & "\"'")
			do shell script ("ssh " & remAddr & space & osaCom)
		end timeout
	on error m number n
		log m
		log n
	end try
end repeat


Using a repeat loop, each file path in the filesToAdd list (that we got from the first rsync dry-run) is used in the construction of an osascript command that adds that file to the remote iTunes. This command is ssh'd to the remote addresss ("cleanuser@192.168.1.206") via a do shell script. The with timeout value of three days is a bit overboard just to make sure there is plenty of time to operate.

Note that if the remote user's iTunes is set to "Copy files to iTunes Music folder..." then iTunes won't seemingly copy any files. The second rsync will have already ostensibly copied each file to its "proper iTunes" location, or, at least, to the same heirarchical scheme as on the source machine, which is presumably acceptable. However, if "Keep iTunes Music folder organized" is also checked in preferences, iTunes may be obliged to change the file name by appending the track number or using its tags to rename the filename and/or its containing Artist and Album folders. If the "Copy files..." preference is not set then, again, the file location is just as appropriate.

That's pretty much it. The next page has a listing of the entire script thus far.

 

Get More Information:

See the man pages for:
osascript
rsync
ssh

Apple's Technical Note TN2065 should be read by anyone who uses do shell script, with especial regard to string quoting and escaping.

 

1One supposes that this rsync routine could be skipped, and that the subsequent osascript routine could copy the files from the local to the remote machine by virtue of adding them directly to the remote iTunes---provided its "Copy files to iTunes Music folder when adding to library" preference is checked--but using the rsync method 1) eliminates having to format/escape file paths correctly and 2) is ultimately faster.

Site contents © 2001 - 2024 (that's right: 2001) Doug Adams and weblished by Doug Adams. Contact support AT dougscripts DOT com. About.
All rights reserved. Privacy.
AppleScript, iTunes, iPod, iPad, and iPhone are registered trademarks of Apple Inc. This site has no direct affiliation with Apple, Inc.
The one who says "it cannot be done" should not be interrupting the one who is doing it.