dougscripts.com

    Listing a

  • This script will compare and automatically copy any files in the local user's iTunes Music folder which are not in a remote user's iTunes Music folder and then add those copied files to the remote user's iTunes

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

-- use your own remote and local paths to the iTunes Music folders
set locMusicLibrary to "/Users/dougadams/Music/iTunes/iTunes Music/"
set remMusicLibrary to "/Users/cleanuser/Music/iTunes/iTunes Music/"

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

-- ==  rsync dry run

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

set rezList to my text_to_list(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(perlRez, ASCII character 13)

log filesToAdd

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

-- == add files to iTunes

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

-- == end of run ==

-- == handlers == == == == == == ==
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 - 2019 (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 interrupt the one doing it.