More Applescript Goodness – iTunes to Twitter

Surprisingly (well, not really), it turns out that not everyone out there liked the idea of my putting my currently playing iTunes track into Twitter. Apparently, I listen to too much music.

So, I created a separate Twitter account for my currently playing tunes, and promptly realized that I didn’t have to go though iChat anymore, and could just use Applescipt to query iTunes directly.

You can just copy and paste the code below into Script Editor, and change the Twitter username and password, then save and run. I changed the method of getting the username & password from the my previous Applescript, which queried the Keychain for them. I didn’t like that a requester would pop up each time I ran the script. Call me fussy. Next up, I’ll merge the two existing scripts I have running – this one and the one that uploads the last three songs to my blog – (over on the right under Currently Listening), so that there is only one script running.

Eventually, I’ll break down, fire up Xcode and write some Objective C to do the same thing, but with a pretty interface. At least I’m on the road, learning to develop for the Mac.

You can download the zip file (5.9 kb), or copy and paste the code below. Without further ado; here’s the code:

(* iTunes to Twitter
Version 0.09
April 5 2007
By W. Thomas Leroux -
Thanks to the Applescript mailing list for the help in getting this thing to work.

set twitter_username to "YourTwitterUsername"
set twitter_password to "YourTwitterPassword"
set twitter_login to quoted form of (twitter_username & ":" & twitter_password)
set twitter_url to ""
set update_interval to 3 (* minutes *)
set update_interval to (update_interval * 60) (* convert to seconds *)
set update to "yes"
set music_playing to ""
set old_music_playing to ""
set twitter_results to ""

(* Check if iTunes is running and grab current song / artist / album *)
tell application "System Events"
if exists process "iTunes" then
tell application "iTunes"
if player state is playing then
set current_song to (name of current track) as Unicode text
set current_artist to (artist of current track) as Unicode text
if current_artist is equal to "" then set current_artist to "Unknown"
set current_album to (album of current track) as Unicode text
if current_album is equal to "" then set current_album to "Unknown Album"
set music_playing to current_song & " by " & current_artist & " (" & current_album & ")"
end if
end tell
else (* What to do if iTunes *isn't* running *)
if button returned of (display dialog "iTunes isn't running" & return & "" buttons {"Wait", "Quit"}) is "Quit" then quit
end if
end tell

(* Is there need to send an update? *)
if (music_playing is equal to old_music_playing) or (music_playing is equal to "") then
set update to "no"
set update to "yes"
end if

if update is "yes" then
set the_status to quoted form of ("status=" & music_playing)
(*display dialog "Sending Twitter" & update_interval & ":" & last_character*)
set twitter_results to do shell script "curl --user " & twitter_login & " --data-binary " & the_status & ""
on error
(* In case curl fails for some reason, just don't do anything *)
end try

end if

set twitter_results to ""
set old_music_playing to music_playing
delay update_interval
end repeat

One thought on “More Applescript Goodness – iTunes to Twitter

  1. Pingback: Twittra låt från iTunes | Niklas Malmgren

Leave a Reply

Your email address will not be published. Required fields are marked *