dougscripts.com

    Listing f

  • Copy the selected shared tracks as displayed in the local user's iTunes to a newly created playlist on 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.

property defaultsDomain : "com.dougscripts.copyshared"

global remUserName, remHostIPAddr, remAddr
global sel, sharedLibName, playlistName, playPid

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
	
	my get_login_info()
	
	-- == get a name for the new playlist
	set playlistName to ""
	set defAns to (t1's artist & " - " & t1's album) as text
	set ddResult to (display dialog "Put new tracks in playlist named:" default answer defAns ¬
		buttons {"Cancel", "OK"} default button 2)
	if text returned of ddResult is not "" and button returned of ddResult is "OK" then
		set playlistName to my escape_single_quotes(text returned of ddResult)
	else -- (will cancel on empty string returned)
		return
	end if
	
	-- == make remote playlist, get pid, pass to track routine as global
	if my make_rem_playlist() is false then
		display dialog return & "Unable to create remote playlist." buttons {"Cancel"} ¬
			default button 1 with icon 0 giving up after 15
		return
	end if
	
	-- == copy the tracks to the new playlist
	repeat with t in sel
		try
			my dupe_to_new_playlist(get t's persistent ID)
		end try
	end repeat
end tell

-- == end run

-- == handlers = == == ==
to make_rem_playlist()
	try
		set osaCom to quoted form of ¬
			("osascript -e 'tell application \"iTunes\"' -e 'return persistent id of (make user playlist with properties {name:\"" & playlistName & "\"})' -e 'end tell'")
		set playPid to (do shell script ("ssh " & remAddr & space & osaCom))
		return true
	on error
		return false
	end try
end make_rem_playlist

to dupe_to_new_playlist(pid)
	try
		with timeout of (1 * days) seconds
			set osaCom to quoted form of ¬
				("osascript -e 'tell application \"iTunes\"' -e 'duplicate (some track of library playlist 1 whose persistent id is \"" & pid & "\") to some playlist whose persistent id is \"" & playPid & "\"' -e 'end tell'")
			do shell script ("ssh " & remAddr & space & osaCom)
		end timeout
	on error m number n
		log m
		log n
	end try
end dupe_to_new_playlist

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
		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 escape_single_quotes(x)
	return my replace_chars(x, "'", "'\\''") as text
end escape_single_quotes

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