dougscripts.com

Note: passwordless ssh login, as described here, must be set up among each user in order for this script to work.

Any of the extant comments or logged items are for debugging and so on. They can be removed.

-- == allocate globals

global remUserName, remHostIPAddr, remAddr

-- == init variables

set remUserName to "cleanuser" -- use your remote username
set remHostIPAddr to "192.168.1.206" -- use your remote IP address

set remAddr to (remUserName & "@" & remHostIPAddr) as text

if my are_you_logged_in() is false then return

set start_time to my get_now()

-- == get mx folder loc's via handlers

set locMusicLibrary to my get_local_music_folder()
set remMusicLibrary to my get_remote_music_folder()

-- == make re-formatted variants of mx folder file paths

set locMusicLibraryQuoted to quoted form of locMusicLibrary
set remMusicLibraryEscaped to my replace_chars(remMusicLibrary, " ", "\\ ")
set remMusicLibraryEscapedQuoted to quoted form of remMusicLibraryEscaped

-- == exclude files in these folders (optional; set to "" if not using)

set excludeThese to "--exclude 'Movies/' --exclude 'Podcasts/' --exclude 'TV Shows/'"

-- ==  rsync dry run

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

set rezList to my text_to_list(my fix_single_quotes(do shell script theCommand), ASCII character 13)

log rezList

-- == get file paths

set perlCom to "perl -e'
	@rez=(" & (("\"") & my list_to_text(rezList, "\",\"") & ("\"")) & ");
	$base=\"" & remMusicLibraryEscaped & "\";

 	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(my replace_chars(perlRez, "'\\''", "'"), ASCII character 13)

log filesToAdd

-- == rsync for real

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

set rez to (do shell script theCommand)

log rez

-- == add files to iTunes

set trackAddedCounter to 0
repeat with fileToAdd in filesToAdd
	set fileToAdd to my fix_single_quotes(fileToAdd)
	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)
			set trackAddedCounter to (trackAddedCounter + 1)
		end timeout
	on error m number n
		log m
		log n
	end try
end repeat

-- == generate log text (optional)

set logResults to ("Started: " & start_time & return & ¬
	"Finished: " & my get_now() & return & ¬
	rez & return & return & ¬
	"Files added to iTunes: " & trackAddedCounter & return & ¬
	"== == == == == == == == == == == ==" & return)

log logResults

if logResults does not contain "Number of files transferred: 0" then
	log "WOULD BE LOGGED"
else
	log "WOULD NOT BE LOGGED"
end if

-- == end of run ==

-- == handlers == == == == == == ==
to fix_single_quotes(x)
	return my replace_chars(x, "'", "'\\''") as text
end fix_single_quotes

on are_you_logged_in()
	set whoCom to quoted form of ("who -q | grep \"" & remUserName & "\"")
	try
		set rez to (do shell script ("ssh " & remAddr & space & whoCom))
		set osaCom to quoted form of ("osascript -e 'tell application\"iTunes\" to launch'")
		do shell script ("ssh " & remAddr & space & osaCom)
		return true
	on error
		return false
	end try
end are_you_logged_in

to get_local_music_folder()
	set o to (do shell script "defaults read com.apple.iApps iTunesRecentDatabasePaths") as text
	set xm to text ((offset of "\"" in o) + 1) through ((offset of ".xml" in o) + 3) of o
	set xml to (my replace_chars(xm, " ", "\\ ")) as text
	-- based on unix script by Dave Taylor
	return (do shell script "head -n 14 " & xml & ¬
		"| grep  '>Music Folder<' | cut -d/ -f4- | sed 's/localhost//g' | \\cut -d\\< -f1 | sed 's/%20/ /g'")
end get_local_music_folder

to get_remote_music_folder()
	set dCom to quoted form of ("defaults read com.apple.iApps iTunesRecentDatabasePaths")
	set o to (do shell script ("ssh " & remAddr & space & dCom))
	set xm to text ((offset of "\"" in o) + 1) through ((offset of ".xml" in o) + 3) of o
	set xml to (my replace_chars(xm, " ", "\\ ")) as text
	-- again, based on unix script by Dave Taylor
	set hCom to quoted form of ("head -n 14 " & xml & ¬
		"| grep  '>Music Folder<' | cut -d/ -f4- | sed 's/localhost//g' | \\cut -d\\< -f1 | sed 's/%20/ /g'")
	return (do shell script ("ssh " & remAddr & space & hCom))
end get_remote_music_folder

to get_now()
	return (do shell script "date '+%x %X'")
end get_now

on replace_chars(txt, srch, repl)
	set AppleScript's text item delimiters to the srch
	set the item_list to every text item of txt
	set AppleScript's text item delimiters to the repl
	set txt to the item_list as string
	set AppleScript's text item delimiters to ""
	return txt
end replace_chars

on text_to_list(txt, delim)
	set saveD to AppleScript's text item delimiters
	try
		set AppleScript's text item delimiters to {delim}
		set theList to every text item of txt
	on error errStr number errNum
		set AppleScript's text item delimiters to saveD
		error errStr number errNum
	end try
	set AppleScript's text item delimiters to saveD
	return (theList)
end text_to_list

on list_to_text(theList, delim)
	set saveD to AppleScript's text item delimiters
	try
		set AppleScript's text item delimiters to {delim}
		set txt to theList as text
	on error errStr number errNum
		set AppleScript's text item delimiters to saveD
		error errStr number errNum
	end try
	set AppleScript's text item delimiters to saveD
	return (txt)
end list_to_text


 

The Other Way 'Round

This "push" workflow could be written the other way around, as a "pull" workflow, such that the local machine periodically checks the remote machine for new new files and copies them over to add to the local iTunes. That's the next project.

 

Get More Information:

See the man page for who.

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

 

1This will be accurate providing your system is not suffering from Multiple Library Confusion, whereby the iApps.plist is not updated with the correct location of your current iTunes library files.

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.