dougscripts.com

    Listing g

  • Delete a selected shared playlist from 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


tell application "iTunes"
	set selectedPlaylist to (get view of front window)
	if (kind of (get container of selectedPlaylist) is shared library) then
		tell selectedPlaylist
			set playlistName to (get name)
			set sharedLibName to (get name of container)
		end tell
		
		-- == optional "Are You Sure?" dialog
		display dialog "Are you SURE you want to delete the playlist \"" & playlistName & ¬
			"\" from \"" & sharedLibName & "\"?" & return & return & ¬
			"This action cannot be undone." buttons {"Cancel", "OK"} default button 2 with icon 2
	else
		display dialog "Selected playlist is not a remote shared playlist." buttons {"Cancel"} ¬
			default button 1 with icon 0 giving up after 15
		return
	end if
end tell

my get_login_info()

-- == delete the remote playlist
try
	set osaCom to quoted form of ¬
		("osascript -e 'tell application \"iTunes\" to delete playlist \"" & my escape_single_quotes(playlistName) & "\"'")
	do shell script ("ssh " & remAddr & space & osaCom)
on error m number n
	log m
	log n
end try

-- == end 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
			set remUserName to text returned of (display dialog ¬
				"Enter the username associated with the shared library \"" & ¬
				sharedLibName & "\":" default answer "")
			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.