dougscripts.com

    Listing c

  • The full "pull" version, intended to be controlled by launchd

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

-- here, if iTunes is active on THIS machine
if my itunes_is_running() is false then return

-- == init variables

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

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

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 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

-- == exclude files in these folders

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 & ¬
	remAddr & ":" & remMusicLibraryEscapedQuoted & space & ¬
	locMusicLibraryQuoted)

set rsyncRez to do shell script theCommand

set rezList to my text_to_list(my fix_single_quotes(rsyncRez), ASCII character 13)

log rezList

-- == 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

-- == rsync for real

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

set rez to (do shell script theCommand)

log rez

-- == 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

-- == generate log text
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 itunes_is_running()
	tell application "System Events" to return (exists process "iTunes")
end itunes_is_running

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


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.