iTweetTunes

Utilizing Twitterrific as a medium for sharing your “Currently Listening to” to Twitter. Script is well documented, if you have any questions please post. To run have Twitterific installed on machine and make sure you have your Twitter login information set in Twitterific. Start the script, then start iTunes. With each new track a tweet to your Twitter account will appear stating what your are listening to along with a TinyURL displaying the search results of your song from Wikipedia.

--The base for checking if script has already Tweeted
set info to ""
set info_check to ""

--The following is an inf. loop. If iTunes is running it will post to twitter. If iTunes is not it will wait for 30 seconds and check if iTunes is active.

repeat
	
	--Searching for iTunes
	tell application "System Events"
		set num to count (every process whose name is "iTunes")
	end tell
	-- If iTunes process exists, continue.
	if num > 0 then
		tell application "iTunes"
			if player state is playing then
				set who to artist of current track
				set what to name of current track
				set onwhat to album of current track
				set stars to (rating of current track) / 20 as integer
				set info to ("Currently listening to: " & what & " by " & who)
				--Preparing URL, if you want different search engine replace everything after url=? and then format correctly
				set FullURL to ("http://tinyurl.com/api-create.php?url=http://en.wikipedia.org/wiki/Special:Search?search=" & what & "+by+" & who & "/")
			end if
		end tell
		--Else, loop till it does
	else
		repeat
			tell application "System Events"
				set num_mon to count (every process whose name is "iTunes")
			end tell
			if num_mon > 0 then
				exit repeat
			else
				--If you want the script to wait longer before it checks for iTunes, change the following number. It is currently set for 30 seconds.
				delay 30
			end if
		end repeat
	end if
	--If current track does not equal last tweeted track then tweet.
	if info = info_check then
	else
		--Removing all spaces withing the URL
		set {TID, text item delimiters} to {text item delimiters, space}
		set FullURL to text items of FullURL
		set text item delimiters to "+"
		set FullURL to FullURL as text
		set text item delimiters to TID
		--Getting the 'Tiny' version of the URL
		do shell script "curl '" & FullURL & "' | vis"
		set tiny_url to result
		tell application "Twitterrific"
			post update info & " " & tiny_url
		end tell
		set info_check to info
	end if
end repeat

Would you like me to implement native twitter sending, without another application?

Here:

tell application "System Events" to set torun to (name of processes) contains "iTunes"
if torun then
	tell application "iTunes"
		set tosend to false
		if player state is playing then
			set who to artist of current track
			set what to name of current track
			set onwhat to album of current track
			set stars to (rating of current track) as integer
			set tosend to true
		end if
	end tell
	if tosend then
		set stars to stars / 20
		if stars is 0.0 then set stars to "nothing"
		set rating to "I rated this song " & stars & " out of 5."
		set info to "I am listening to \"" & what & "\" by \"" & who & "\" from the album \"" & onwhat & quote & space & rating
		twtfilsend(info)
	else
		display dialog "You are not playing a song in iTunes!" buttons {"OK"} default button 1
	end if
end if
on en(URLs)
	return do shell script "/usr/bin/python -c '" & ¬
		"from sys import argv; " & ¬
		"from urllib import quote; " & ¬
		"print quote(unicode(argv[1], \"utf8\"))' " & quoted form of URLs
end en
on login()
	set username to ""
	set userpass to ""
	try
		set loginuser to (do shell script "defaults read com.twitter.login User")
	on error F
		if F contains " does not exist" then
			repeat
				set username to text returned of (display dialog "Since there is no stored username, please put in your Twitter username below." default answer "" buttons {"Cancel", "OK"} cancel button {"Cancel"} default button {"OK"} with title "Twitter User Name" with icon path to resource "default.icns" in bundle (path to me)) as string
				if username is not "" then
					exit repeat
				end if
			end repeat
			do shell script "defaults write com.twitter.login User " & username
		end if
	end try
	try
		set loginpass to do shell script "defaults read com.twitter.login Pass"
	on error F
		if F contains " does not exist" then
			repeat
				set userpass to text returned of (display dialog "Since there is no stored password, please put in your Twitter password below." default answer "" buttons {"OK"} default button 1 with title "Twitter Password" with icon (path to resource "default.icns" in bundle (path to me)) with hidden answer) as string
				if userpass is not "" then
					exit repeat
				end if
			end repeat
			do shell script "defaults write com.twitter.login Pass " & userpass
		end if
	end try
	if username is "" then
		set username to loginuser as string
	end if
	if userpass is "" then
		set userpass to loginpass as string
	end if
	set returner to {name:username, password:userpass}
	return returner
end login
on twtfilsend(Tweet)
	set username to name of login()
	set userpass to password of login()
	set TwitterUser to username as string -- works with the script
	set TwitterPassword to userpass as string
	set shelltwt to quoted form of Tweet as string
	try
		set lasttweet to do shell script "defaults read com.twitter.login last" -- gets last tweet so it can be a default answer when it askes for your tweet
	on error F
		if F contains " does not exist" then
			set lasttweet to ""
		end if
	end try
	set twtcount to (count the characters of Tweet)
	set meow to 0
	set meow to meow + 1
	if twtcount is greater than 140 then -- twitter's 140 limit rule
		display dialog "The song name is so long that I can't send this to Twitter!" buttons {"OK"} default button 1
		error number -128
	end if
	set TwitterUpdate to "/usr/bin/curl" & ¬
		" --user " & quoted form of (TwitterUser & ":" & TwitterPassword) & ¬
		" --data status=" & quoted form of en(Tweet) & ¬
		" [url=http://twitter.com/statuses/update.xml]http://twitter.com/statuses/update.xml"[/url] -- sets the update shell script
	set twtdata to do shell script TwitterUpdate -- sents the tweet
	if twtdata does not contain "Could not authenticate" then
		display dialog "Your tweet \"" & Tweet & "\" has been successfully sent to twitter!" buttons {"OK"} default button 1 with title "Success!" -- sent sucsessfully
	end if
	if twtdata contains "Could not authenticate" then -- if it could not authenticate
		repeat
			display dialog "An error has occured. Maybe your password is incorrect! I will now reset your preferences, ask for a password, and then try again!" buttons {"OK"} default button 1 with title "Error!" with icon path to resource "default.icns" in bundle (path to me)
			set PreferencePath to (path to preferences folder from user domain as Unicode text) & "com.twitter.login.plist"
			do shell script "rm -rf " & quoted form of the POSIX path of PreferencePath with administrator privileges
			set username to name of login()
			set userpass to password of login()
			set TwitterUser to username as string -- works with the script
			set TwitterPassword to userpass as string
			set TwitterUpdate to "/usr/bin/curl" & ¬
				" --user " & quoted form of (TwitterUser & ":" & TwitterPassword) & ¬
				" --data status=" & quoted form of en(Tweet) & ¬
				" [url=http://twitter.com/statuses/update.xml]http://twitter.com/statuses/update.xml"[/url] -- sets the update shell script
			try
				set twtdata to do shell script TwitterUpdate -- sents the tweet
			on error ertxt number ernumb
				display dialog "An error has occured!\n\t\t\n\t\t" & ertxt & "  (-" & ernumb & ")"
			end try
			if twtdata does not contain "Could not authenticate" then
				display dialog "Your tweet \"" & Tweet & "\" has been successfully sent to twitter!" buttons {"OK"} default button 1 with title "Success!" with icon path to resource "default.icns" in bundle (path to me) -- sent sucsessfully
				exit repeat
			end if
			if twtdata contains "Could not authenticate" then
			end if
		end repeat
	end if
end twtfilsend

To see if an app is running you can use this.


if application "iTunes" is running then
end if

Oh, OK. Here it is AGAIN!

if application "iTunes" is running then
	tell application "iTunes"
		set tosend to false
		if player state is playing then
			set who to artist of current track
			set what to name of current track
			set onwhat to album of current track
			set stars to (rating of current track) as integer
			set tosend to true
		end if
	end tell
	if tosend then
		set stars to stars / 20
		if stars is 0.0 then set stars to "nothing"
		set rating to "I rated this song " & stars & " out of 5."
		set info to "I am listening to \"" & what & "\" by \"" & who & "\" from the album \"" & onwhat & quote & space & rating
		twtfilsend(info)
	else
		display dialog "You are not playing a song in iTunes!" buttons {"OK"} default button 1
	end if
end if
on en(URLs)
	return do shell script "/usr/bin/python -c '" & ¬
		"from sys import argv; " & ¬
		"from urllib import quote; " & ¬
		"print quote(unicode(argv[1], \"utf8\"))' " & quoted form of URLs
end en
on login()
	set username to ""
	set userpass to ""
	try
		set loginuser to (do shell script "defaults read com.twitter.login User")
	on error F
		if F contains " does not exist" then
			repeat
				set username to text returned of (display dialog "Since there is no stored username, please put in your Twitter username below." default answer "" buttons {"Cancel", "OK"} cancel button {"Cancel"} default button {"OK"} with title "Twitter User Name" with icon path to resource "default.icns" in bundle (path to me)) as string
				if username is not "" then
					exit repeat
				end if
			end repeat
			do shell script "defaults write com.twitter.login User " & username
		end if
	end try
	try
		set loginpass to do shell script "defaults read com.twitter.login Pass"
	on error F
		if F contains " does not exist" then
			repeat
				set userpass to text returned of (display dialog "Since there is no stored password, please put in your Twitter password below." default answer "" buttons {"OK"} default button 1 with title "Twitter Password" with icon (path to resource "default.icns" in bundle (path to me)) with hidden answer) as string
				if userpass is not "" then
					exit repeat
				end if
			end repeat
			do shell script "defaults write com.twitter.login Pass " & userpass
		end if
	end try
	if username is "" then
		set username to loginuser as string
	end if
	if userpass is "" then
		set userpass to loginpass as string
	end if
	set returner to {name:username, password:userpass}
	return returner
end login
on twtfilsend(Tweet)
	set username to name of login()
	set userpass to password of login()
	set TwitterUser to username as string -- works with the script
	set TwitterPassword to userpass as string
	set shelltwt to quoted form of Tweet as string
	try
		set lasttweet to do shell script "defaults read com.twitter.login last" -- gets last tweet so it can be a default answer when it askes for your tweet
	on error F
		if F contains " does not exist" then
			set lasttweet to ""
		end if
	end try
	set twtcount to (count the characters of Tweet)
	set meow to 0
	set meow to meow + 1
	if twtcount is greater than 140 then -- twitter's 140 limit rule
		display dialog "The song name is so long that I can't send this to Twitter!" buttons {"OK"} default button 1
		error number -128
	end if
	set TwitterUpdate to "/usr/bin/curl" & ¬
		" --user " & quoted form of (TwitterUser & ":" & TwitterPassword) & ¬
		" --data status=" & quoted form of en(Tweet) & ¬
		" [url=http://twitter.com/statuses/update.xml]http://twitter.com/statuses/update.xml"[/url] -- sets the update shell script
	set twtdata to do shell script TwitterUpdate -- sents the tweet
	if twtdata does not contain "Could not authenticate" then
		display dialog "Your tweet \"" & Tweet & "\" has been successfully sent to twitter!" buttons {"OK"} default button 1 with title "Success!" -- sent sucsessfully
	end if
	if twtdata contains "Could not authenticate" then -- if it could not authenticate
		repeat
			display dialog "An error has occured. Maybe your password is incorrect! I will now reset your preferences, ask for a password, and then try again!" buttons {"OK"} default button 1 with title "Error!" with icon path to resource "default.icns" in bundle (path to me)
			set PreferencePath to (path to preferences folder from user domain as Unicode text) & "com.twitter.login.plist"
			do shell script "rm -rf " & quoted form of the POSIX path of PreferencePath with administrator privileges
			set username to name of login()
			set userpass to password of login()
			set TwitterUser to username as string -- works with the script
			set TwitterPassword to userpass as string
			set TwitterUpdate to "/usr/bin/curl" & ¬
				" --user " & quoted form of (TwitterUser & ":" & TwitterPassword) & ¬
				" --data status=" & quoted form of en(Tweet) & ¬
				" [url=http://twitter.com/statuses/update.xml]http://twitter.com/statuses/update.xml"[/url] -- sets the update shell script
			try
				set twtdata to do shell script TwitterUpdate -- sents the tweet
			on error ertxt number ernumb
				display dialog "An error has occured!\n\t\t\n\t\t" & ertxt & "  (-" & ernumb & ")"
			end try
			if twtdata does not contain "Could not authenticate" then
				display dialog "Your tweet \"" & Tweet & "\" has been successfully sent to twitter!" buttons {"OK"} default button 1 with title "Success!" with icon path to resource "default.icns" in bundle (path to me) -- sent sucsessfully
				exit repeat
			end if
			if twtdata contains "Could not authenticate" then
			end if
		end repeat
	end if
end twtfilsend

Wow guys great work! What took you a matter of only hours was a 2-3 day project for myself. Partially because I am actually working on a Dell Latitude D630 and remotely connecting to a Mac at my college campus to run and debug. My MBP arrives on Friday and just getting myself familiar with the coding. And unfortunately I am unable to download xcode as the machine I’m remotely connecting to is 10.5.x and from what I need you need Snow Leopard to get it? Again thanks for the revision! And so quickly!

Edit+

Just ran the script and get error: Resource not found
and script editor defaults to:

set username to text returned of (display dialog "Since there is no stored username, please put in your Twitter username below." default answer "" buttons {"Cancel", "OK"} cancel button {"Cancel"} default button {"OK"} with title "Twitter User Name" with icon path to resource "default.icns" in bundle (path to me)) as string

I did enter my username and pass.

Oh, sorry. Remove “with icon path to resource “default.icns” in bundle (path to me)) as string”

that worked great, there is also a “with icon path to resource “default.icns” in bundle (path to me)) as string” in the password field of the code that needs to be removed. Works wonderfully now!

Edit+

Noticed after it posts once the script shuts down. Mine preformed a loop. Where would I add that to your script? And it doesn’t post a link to a search result containing your song. I listen to some pretty esoteric music at times and the best way to find them is with our good friend wikipedia.

Other than that worked great!

How long your been scripting now? Can you point me in any direction to learn a little bit more about applescript?

I’ve been scripting for 4 years. This site is THE BEST place for scripting resources and problems. Or I would google search with the word “applescript” and then the keywords. (EXAMPLE: “applescript choose from list multiple selections”)