dougscripts.com

    Listing e

  • Copy the files of the selected shared tracks as displayed in the local user's iTunes from the particular remote user's iTunes Music folder to the local user's iTunes Music folder and then add them to the local 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.

property defaultsDomain : "com.dougscripts.copyshared"

global remUserName, remHostIPAddr, remAddr
global locMusicLibrary, remMusicLibrary
global sel, sharedLibName

-- == selection of tracks?

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
	else
		set t1 to item 1 of sel
		if (get class of t1 is shared track) then
			set sharedLibName to (get name of container of container of t1)
		else
			display dialog "No shared tracks selected." buttons {"Cancel"} default button 1 with icon 0
			return
		end if
	end if
end tell

-- == get/create ssh login info specific to selected shared library

my get_login_info()

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

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

tell application "iTunes"
	repeat with t in sel
		try
			my copy_this_file_by_pid(get t's persistent ID)
		end try
	end repeat
end tell

-- == end of run == == ==

-- == handlers = == == ==
to get_login_info()
	try
		set {remUserName, remHostIPAddr} to my read_defaults(sharedLibName)
	on error m number n
		if n is in {1, -1728} then
			-- get username for sharedLibName
			set remUserName to text returned of (display dialog ¬
				"Enter the username associated with the shared library \"" & ¬
				sharedLibName & "\":" default answer "")
			-- get ip address for sharedLibName
			set remHostIPAddr to text returned of (display dialog ¬
				"Enter the IP address associated with the shared library \"" & ¬
				sharedLibName & "\":" default answer "")
			my write_defaults(sharedLibName, {remUserName, remHostIPAddr})
		else
			-- some other error; abort
			log m
			log n
			error number -128 -- cancel
		end if
	end try
	
	set remAddr to (remUserName & "@" & remHostIPAddr) as text
	
end get_login_info

on write_defaults(k, l)
	do shell script "defaults write " & defaultsDomain & space & ¬
		quoted form of k & " -array " & (("'") & my list_to_text(l, "' '") & ("'"))
end write_defaults

on read_defaults(k)
	return paragraphs of (do shell script (("defaults read " & defaultsDomain & space & ¬
		quoted form of k) & space & "| grep ' '|sed -e 's/ //g' -e's/\"//g' -e's/\\,//g'"))
end read_defaults

to copy_this_file_by_pid(pid)
	
	set osaCom to quoted form of ¬
		("osascript -e 'tell application\"iTunes\" to return POSIX path of (get location of some track of library playlist 1 whose persistent id is\"" & pid & "\")'")
	set posixLoc to do shell script ("ssh " & remAddr & space & osaCom)
	set fileFolderHeirarchy to text ((offset of remMusicLibrary in posixLoc) + (length of remMusicLibrary)) thru -1 of posixLoc
	set locLoc to (locMusicLibrary & fileFolderHeirarchy) as text
	set locContainer to (my list_to_text(items 1 thru -2 of my text_to_list(locLoc, "/"), "/") & "/") as text
	
	if not my loc_file_exist(locLoc) then
		set mkdirCom to ("mkdir -p " & my escape_special_chars(locContainer)) as text
		try
			set x to do shell script mkdirCom
			log x -- this result not needed; observation/debugging only
		on error m
			log ("mkdir err: " & m)
		end try
		
		set scpCom to ("scp -EC" & space & remAddr & ":" & ¬
			quoted form of my escape_special_chars(posixLoc) & space & ¬
			quoted form of locContainer) as text
		set rez to do shell script scpCom
		log rez -- this result not needed; observation/debugging only
		
		try
			set aliasToAdd to (POSIX file (locLoc)) as text
			with timeout of (1 * days) seconds
				tell application "iTunes"
					-- add the file
					set newTrack to add alias aliasToAdd
				end tell
			end timeout
		on error m number n
			log m
			log n
		end try
	else
		log "EXISTS - SKIP"
	end if
	
end copy_this_file_by_pid

on loc_file_exist(locLoc)
	try
		set x to do shell script "test -e \"" & locLoc & "\""
		return (x is "")
	on error
		return false
	end try
end loc_file_exist

to escape_special_chars(n)
	set escThese to " &()'"
	repeat with chr in escThese
		set n to my replace_chars(n, chr, ("\\" & chr) as text)
	end repeat
	return n
end escape_special_chars

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