Listing d

  • This script will copy the selected iTunes tracks from the local user's iTunes Music folder to a remote user's iTunes Music folder and add them 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, locMusicLibrary, remMusicLibrary

-- == selection?

tell application "iTunes"
	set sel to selection
	if sel is {} then
		display dialog "No tracks selected." buttons {"Cancel"} default button 1 with icon 0
		return
	end if
end tell

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

-- == check if remote iTunes is running

if my are_you_logged_in() is false then
	tell application "iTunes"
		display dialog ("\"" & remUserName & "\" is not logged in and running iTunes.") ¬
			buttons {"Cancel"} default button 1 with icon 0
		return
	end tell
end if

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

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

-- == process track selection

tell application "iTunes"
	repeat with t in sel
		if (get class of t is file track) then
			try
				set loc to t's location
				if loc is not missing value then
					my process_this_file(POSIX path of (loc as text))
				end if
			on error m
				log "ERROR: " & m
			end try
		end if
	end repeat
end tell

-- == end run

-- == handlers = == == == == == == == 

-- copy to remote iTunes and add
to process_this_file(posixLoc)
	
	-- == init some variables
	
	set fileFolderHeirarchy to text ((length of locMusicLibrary) + 1) thru -1 of (posixLoc as text)
	set remLoc to (remMusicLibrary & fileFolderHeirarchy) as text
	set remContainer to (my list_to_text(items 1 thru -2 of my text_to_list(remLoc, "/"), "/") & "/") as text
	
	-- == copy and add the file only if it doesn't already exist on the remote side
	
	if not my rem_file_exists(remLoc) then
		
		-- == == containing folder exists? if not, create
		
		set mkdirCom to quoted form of ("mkdir -p " & my escape_for_unix(remContainer)) as text
		try
			set x to do shell script "ssh " & remAddr & space & mkdirCom
			log x
		on error m
			log ("mkdir err: " & m)
		end try
		
		-- -- == copy the file
		
		set scpCom to ("scp" & space & quoted form of posixLoc & space & ¬
			remAddr & ":\"" & my escape_for_unix(remContainer) & "\"") as text
		
		set rezList to do shell script scpCom
		log rezList -- this result not needed; observation only
		
		-- == == add the newly copied file to remote iTunes
		
		set fileToAdd to my fix_single_quotes(remLoc)
		try
			with timeout of (1 * 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
		
	else
		log "EXISTS - SKIP"
	end if
end process_this_file

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

on rem_file_exists(remF)
	set testCom to quoted form of ("test -e \"" & remF & "\"")
	try
		set x to (do shell script ("ssh " & remAddr & space & testCom))
		if x is "" then return true
		return false
	on error
		return false
	end try
end rem_file_exists

to escape_for_unix(n)
	set n1 to my replace_chars(n, " ", "\\ ")
	return my replace_chars(n1, "'", "\\'")
end escape_for_unix

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

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

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 - 2014 (that's right: 2001) Doug Adams and weblished by Doug Adams, dougscripts AT mac DOT com. About.
All rights reserved. Legal.
AppleScript, iTunes, iPod, iPad, and iPhone are registered trademarks of Apple Inc.