AppleScript fun: iTunes/Cog current music in Skype status

Skype Current Music
AppleScript is Power! AppleScript is Fun! AppleScript will take care of putting my current music from iTunes and/or (most important) Cog to my Skype status.
Yes, there are tons of programs that will do it for iTunes, but not for Cog. This script will do it for both, and you can have both running at same time – if iTunes is running but isn’t playing, Cog status will be posted. Cool, huh? (-:

The code is based on the snippets provided by some good people in these posts:
http://www.awaken-dreamer.org/2008/03/13/subtlestatus-itunes-cog-script/
http://rdonato.altervista.org/?p=13#more-13

Thanks people!

Note: code updated on Tuesday 26-th of October – now your custom status message will be shown before music. Music will always be added after it (even if tracks change and music info is already in mood message it will be handled properly).
Updated on 30-th of October! Support for titles of streams added (webradios etc).
Updated on 9-th of January 2011: fixed bug with iTunes track title being periodically replaced with Cog track title in case both apps are running but iTunes is currently playing. Updated screenshots.


Here I wanted to brag a little about how cool AppleScript is and how much power on automating tasks it brings to the OS X, and how sorry I am for not knowing much of it and not having time to dig deeper, but I guess this sentence says it all already (-:
The only bragging I’ll do is about Cog: I use it much more than iTunes because of FLAC and Monkey’sAudio (APE) formats support, and, most important, CUE-sheets support.

So, “give us the code” the masses demand, and we’re happy to do so:

set track_info to the ""
set old_track_info to the ""

set music_str_start_marker to the "[Music]:"
set skype_mood_text_starter to the "PROFILE MOOD_TEXT "

repeat
	set update_required to false
	set current_mood_messaage to the ""
	tell application "System Events"
		if exists process "Skype" then
			tell application "Skype"
				set current_mood_messaage to the send command "GET PROFILE MOOD_TEXT" script name "CogCurrentMusicSkypeStatus"
				
				if current_mood_messaage is equal to skype_mood_text_starter then
					set current_mood_messaage to the ""
				else if current_mood_messaage starts with skype_mood_text_starter then
					set current_mood_messaage to the text ((length of skype_mood_text_starter) + 1) thru -1 of current_mood_messaage
				end if
				
				if current_mood_messaage contains music_str_start_marker then
					set marker_offset to the offset of music_str_start_marker in current_mood_messaage
					if (marker_offset = 1) then
						set current_mood_messaage to the ""
					else
						set current_mood_messaage to the text 1 thru (marker_offset - 1) of current_mood_messaage
					end if
				else
					set update_required to true
				end if
				
				-- trim spaces
				repeat until current_mood_messaage does not start with " "
					set current_mood_messaage to text 2 thru -1 of current_mood_messaage
				end repeat
				
				repeat until current_mood_messaage does not end with " "
					set current_mood_messaage to text 1 thru -2 of current_mood_messaage
				end repeat
				
			end tell
		end if
	end tell

	set track_info to the ""
	tell application "System Events"
		if exists process "iTunes" then
			tell application "iTunes"
				(* Grab info only if iTunes is playing *)
				if player state is playing then
					if class of current track is URL track then
						set radio_title to name of current track
						set track_title to current stream title
						set track_info to the current_mood_messaage & " " & music_str_start_marker & " " & track_title & " (Streaming from " & radio_title & ")"
						
					else
						set this_title to name of current track
						set this_artist to artist of current track
						set this_album to album of current track
						set this_year to year of current track
						set track_info to the current_mood_messaage & " " & music_str_start_marker & " " & this_artist & " - " & this_title & " (\"" & this_album & "\" '" & this_year & ")"
					end if
				end if
			end tell
		end if
	end tell
	
	if (track_info = "") then
		tell application "System Events"
			if (exists process "Cog") then
				tell application "Cog"
					if (exists title of the currententry) then
						set this_title to the title of the currententry
						set this_artist to the artist of the currententry
						set this_album to the album of the currententry
						set this_year to the year of the currententry
						set track_info to the current_mood_messaage & " " & music_str_start_marker & " " & this_artist & " - " & this_title & " (\"" & this_album & "\" '" & this_year & ")"
					end if
				end tell
			end if
		end tell
	end if
	
	if (not track_info = old_track_info or update_required) then
		set old_track_info to the track_info
		tell application "System Events"
			if exists process "Skype" then
				tell application "Skype"
					send command "SET PROFILE MOOD_TEXT " & track_info & "." script name "CogCurrentMusicSkypeStatus"
				end tell
			end if
		end tell
	end if
	delay 10
	
end repeat

And here’s a little screenshot

Skype iTunes current music screenshot
Advertisements

15 thoughts on “AppleScript fun: iTunes/Cog current music in Skype status

  1. Very useful for me although i do not know very much about appleScript; I am using it in livecode to create an application that displays the title & the artist hile the screen is dark (no screensaver).
    Thank you it was just what i’ve been looking for

    • Hi
      AppleScript doesn’t need any “installation”, you can run it using “AppleScript Editor” application.
      To make it more convenient to run it often I suggest adding it to script menu (it’s this thing: http://www.devdaily.com/blog-files/mac-osx-applescript-menu.jpg ).
      Do the following:
      1. Open “AppleScript Editor” Application (it should be in OS X by default). In menu select “AppleScript Editor” -> “Preferences”, (you’ll see window similar to this http://www.twdesigns.com/how_to/AppleScript04.jpg), make sure checkbox “Show Script menu in menu bar” is checked, close “AppleScript Editor”.
      2. In apple script menu (the http://www.devdaily.com/blog-files/mac-osx-applescript-menu.jpg thing) choose “Open Scripts Folder” -> “Open User Scripts Folder”. Finder window will open at location like /Users/yourusername/Library/Scripts – save/copy the script I provided in my post to this folder in a file with extension .applescript or .scpt (i.e. iTunes n Cog Current Music Skype Status.scpt).
      3. In apple script menu you should now have option corresponding to the filename of script file (i.e. iTunes n Cog Current Music Skype Status in my case) – click it and script will run.
      To stop the script open ActivityMonitor (this is also default OS X application – http://main.makeuseoflimited.netdna-cdn.com/wp-content/uploads/2008/09/activity-monitor2.png ), find the “AppleScript Runner” process and make it quit.

      I haven’t done any stuff to make script auto-start at startup yet, but it is also possible, so you can probably google it if you wish.

      Hope this helps.

      Regards,
      Mykola.

  2. Pingback: Der Musik Player Cog und ‘now playing’ in Skype

  3. Pingback: MusikPlayer Cog und ‘now playing’ in Skype

  4. Thanks for the script!
    But is there a way to have script running in the background? I don’t like this AppleScript editor to stay in dock all the time (I can’t quit the app since it says “the script is running”)

  5. Sorry, nevermind my post, I got it :)
    I didn’t know I had to save it as special script file in order to get it runned automatically without Editor popping up everytime!
    Thanks again ;)

    • Hi, Anton,

      Glad to see you’ve resolved the problem already. You may also check my reply to comment from Rocky above – it has some points on the subject of running AppleScript scripts.
      Sorry for late reply.

      Regards,
      Mykola.

  6. Hi,
    thank you for your script! Trying to use it, but in my skype status it looks like “COMMAND_PENDING [Music]: The Tallis Scholars – Tomas Luis de Victoria / Introitus (“Requiem (CD 1)” ‘2005).” – any idea what the hell is this “command_pending”, huh? :)

    • This happens sometimes on startup of Skype, haven’t figured it out completely yet. Quick fix – just clear your status message, and it’ll be populated correctly.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s