Skip to content

Instantly share code, notes, and snippets.

@nacho4d
Created May 4, 2012 07:28
Show Gist options
  • Save nacho4d/2592872 to your computer and use it in GitHub Desktop.
Save nacho4d/2592872 to your computer and use it in GitHub Desktop.
Tweet current iTunes song using Tweeted for iMac with Applescript
on replace(src, tg, rp)
set oldDel to AppleScript's text item delimiters
set AppleScript's text item delimiters to tg
set myList to text items of src
set AppleScript's text item delimiters to rp
set myText to myList as string
set AppleScript's text item delimiters to oldDel
return myText
end replace
to tweetMessageFromiTunesSong()
try
tell application "iTunes"
set thisTrack to current track
set trackTime to thisTrack's time
if trackTime > 0 then
set trackName to the name of thisTrack
set trackArtist to thisTrack's artist
set trackAlbum to thisTrack's album
set trackGenre to thisTrack's genre
set trackTime to thisTrack's time
-- Construct tweet message: "#nowplaying album - song by artist. genre."
if trackName's length > 0 then
if trackAlbum's length > 0 then
set tweetMessage to "#nowplaying " & trackAlbum & " - " & trackName
else
set tweetMessage to "#nowplaying " & trackName
end if
end if
if trackArtist's length > 0 then
set tweetMessage to tweetMessage & " by " & trackArtist
end if
set tweetMessage to tweetMessage & "."
if trackGenre's length > 0 then
set tweetMessage to tweetMessage & " #" & trackGenre & "."
end if
else
-- Construct tweet message: "#nowplaying trackTitle - trackName"
set trackTitle to current stream title
set trackName to the name of thisTrack
set nowPlaying to "#nowPlaying "
set tweetMessage to nowPlaying & trackTitle
set separator to " - "
if (tweetMessage's length) + (separator's length) + (trackName's length) ≤ 140 then
set tweetMessage to tweetMessage & separator & trackName
end if
set trackArtist to ""
set trackAlbum to ""
set trackGenre to ""
end if
end tell
-- say "time is " & trackTime
set tweetMessage to replace(tweetMessage, "&", "&")
on error errmesg number errn
beep
set tweetMessage to ""
say errmesg
end try
return tweetMessage
end tweetMessageFromiTunesSong
on tweet(message)
try
--tell application "System Events" to tell process "Twitter" to key code {102}
tell application "Twitter"
activate
tell application "System Events"
tell process "Twitter"
keystroke "n" using {command down} -- Open "New Tweet" little window
delay 0.5
tell text area 1 of window 1
set value to message
end tell
--keystroke message as string
-- BUG:
-- When only the Kotoeri Japanese input language is available characters after a
-- space (and other special characters) will be changed to Kana. I've tried to
-- solve it with below things but no success so far. The work-around is to add
-- the english US language (or another single stage input language) to the
-- languages input list. If that is your case then make sure you have "US" checked
-- in the list in System Preferences > Language & text > Input sources)
--tell application "System Events" to key code 102
--keystroke ";" using {shift down, control down}
--tell application "System Events" to key code 100 -- Key 英数 in the keyboard, but still not 100% working
delay 0.5
tell UI element 4 of front window
--click
end tell
end tell -- process "Twitter"
end tell -- application "Syste Events"
end tell -- application "Twitter"
on error errmesg number errn
beep
beep
say errmesg
end try
end tweet
set message to tweetMessageFromiTunesSong()
if message's length > 0 then
tweet(message)
else
say "Nothing to tweet"
end if
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment