The legal tricks-Learn Your Self

Latest gadgets,softwares,hardware,reviews,programming and campuses, game cheats ext......

Post iTunes track info to del.icio.us via AppleScript

I work in an office with lots of Macs with iTunes libraries, and often play stuff off other people's libraries to check it out. But I don't want to be busy looking up bands when I'm at work, so I wrote two AppleScripts to post a link containing a Google search for the current iTunes track info to my del.icio.us account. 

By default, the posts are given the tag musictoget. Then every couple of weeks, I check out what I've accumulated under my musictoget tag. One script works with regular tracks, and the other one works with streams (provided the stream contains artist and title information). 

Script #1 (for regular tracks): 

Quote:
(*
"Track info to del.icio.us"

uses del.icio.us API v1 - http://del.icio.us/help/api/

partly based on "Current to Clipboard" for iTunes and "Google Video Search" written by Doug Adams
*)

property base_url : "http://www.google.com/search?q="

tell application "iTunes"
if player state is not stopped then
set ct to current track
set station_name to ""
set {nom, art, alb} to {"", "", ""}

tell ct
try
if artist is not "" and artist is not missing value then
set art to (" - " & artist)
end if
end try
try
if name is not "" then
set nom to name
else
set nom to ""
end if
end try
try
if album is not "" then
set alb to (" - " & album)
else
set alb to ""
end if
end try
end tell

set search_term to (nom & art & alb)
end if
end tell

set search_term to searchReplace(search_term, " ", "%20") as string

set the_command to ("curl \"https://username:password@api.del.icio.us/v1/posts/add?tags=musictoget&description=" & search_term & "&url=")

set search_url to (base_url & search_term) as string

set the_command to (the_command & search_url & "\"") as string

do shell script the_command

on searchReplace(origStr, searchStr, replaceStr)
set old_delim to AppleScript's text item delimiters
set AppleScript's text item delimiters to searchStr
set origStr to text items of origStr
set AppleScript's text item delimiters to replaceStr
set origStr to origStr as string
set AppleScript's text item delimiters to old_delim
return origStr
end searchReplace


Script 2: 
Quote:
(*
"Track info to del.icio.us"
uses del.icio.us API v1 - http://del.icio.us/help/api/
partly based on "Search iTMS for Current Song In Stream" by Doug Adams
*)

property base_url : "http://www.google.com/search?q="

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, " - ")
set art to my replace_chars(art, " ", "%20")
set nom to my replace_chars(nom, " ", "%20")
set search_term to (nom & "%20" & art)
end if
end if
end tell

set the_command to ("curl \"https://username:password@api.del.icio.us/v1/posts/add?tags=musictoget&description=" & search_term & "&url=")

set search_url to (base_url & search_term) as string

set the_command to (the_command & search_url & "\"") as string

do shell script the_command


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

0 comments: