Skip to content

Instantly share code, notes, and snippets.

@blech75
Created January 13, 2012 14:37
Show Gist options
  • Save blech75/1606529 to your computer and use it in GitHub Desktop.
Save blech75/1606529 to your computer and use it in GitHub Desktop.
Search iTMS for Current Song In Stream
(*
"Search iTMS for Current Song In Stream" for iTunes
written by Doug Adams
[email protected]
Get more free AppleScripts and info on writing your own
at Doug's AppleScripts for iTunes
http://www.malcolmadams.com/itunes/
Modified on 2012-01-12 by Justin Blecher <justin at worksperfectly dot net>
- removed text in parens and square brackets of artist and track names to increase likelyhood of iTMS search results
- added proper URL escaping on query params to make it more robust
*)
tell application "iTunes"
if player state is playing and current track's class is URL track then
if current stream title is not "" then
set {art, nom} to my text_to_list(current stream title, " - ")
-- remove text in parens and square brackets of artist and track names
-- e.g. "Some Cool Track (Live Version at Cool Club)" or "Other Cool Track [DJ Mix]"
-- NOTE: sed expression/command before escaping is: s/(\([^)]*\)|\[[^]]*\])//g
set art to do shell script "echo \"" & art & "\" | sed -E 's/(\\([^)]*\\)|\\[[^]]*\\])//g'"
set nom to do shell script "echo \"" & nom & "\" | sed -E 's/(\\([^)]*\\)|\\[[^]]*\\])//g'"
-- URL encode artist and track name params
-- technique courtesy of http://stackoverflow.com/questions/296536/urlencode-from-a-bash-script
set art to do shell script "perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' \"" & art & "\""
set nom to do shell script "perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' \"" & nom & "\""
set search_url to "itms://phobos.apple.com/WebObjects/MZSearch.woa/wa/advancedSearchResults?songTerm=" & nom & "&" & "artistTerm=" & art
-- display alert "URL" message search_url
open location search_url
end if
end if
end tell
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 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment