Even better Current iTunes Track

It’s nice to show other users what you are listening to, and provide a handy link for them to see more about the artist and track. I noticed that some IM systems don’t like HTML, and I figured out a way to make the link only be sent to a user of an AIM compatible or Jabber server.

Basically, you test the result of serviceClass of contact of the active chat of the first interface controller, and if it is “AIM-compatible” or “Jabber”, then you use HTML, if not, just plain text. You can download it from Adium Xtras: Now Playing in iTunes.

Full Source:

 1 Better itunes adium link script
 2 Includes rating (if present), and links.
 3property message_prefix : «data utxt266B0020» as Unicode text
 4property not_listening_message : "Quiet"
 5property star : «data utxt2605» as Unicode text
 6property google : "http://www.google.com/search?q="
 7property server : ""
 8
 9property html_classes : {"AIM-compatible", "Jabber"}
10
11on substitute()
12    set server to "http://" & my getIP() & ":8080"  Use betterGetIP if on broadband!
13    
14    tell application "System Events"
15        try
16            get process "iTunes"
17        on error
18            return message_prefix & not_listening_message
19        end try
20    end tell
21    
22    tell application "iTunes"
23        if player state is not playing then
24            return message_prefix & not_listening_message
25        end if
26        set theArtist to artist of current track
27        if theArtist is "" then set theArtist to "Unknown Artist"
28        set theTrack to name of current track
29        set theRating to " " & my myRating(rating of current track)
30        set theLocation to location of current track
31        set pth to my encode_URL_string(characters 9 thru end of (POSIX path of theLocation) as string)
32    end tell
33    
34    tell application "Adium" to set accountType to serviceClass of contact of the active chat of the first interface controller
35    if first item of accountType is in html_classes then
36        return html(message_prefix & my myLink(google, theArtist) & " • " & my myLink(server & pth, theTrack) & theRating)
37    else
38        return message_prefix & theArtist & " • " & theTrack & theRating
39    end if
40end substitute
41
42on myRating(theRating)
43    set theResult to ""
44    set theTimes to (theRating - 9) / 20 as integer
45    repeat theTimes times
46        set theResult to theResult & star
47    end repeat
48    return theResult
49end myRating
50
51on myLink(URI, theText)
52    if URI is google then
53        set URI to URI & first word of theText
54        repeat with wd in (rest of every word of theText)
55            set URI to URI & "+" & wd
56        end repeat
57    else if server is "" then
58        return theText
59    end if
60    return "<a href=\"" & URI & "\">" & theText & "</a>"
61end myLink
62
63on html(str)
64    return ("<HTML>" as Unicode text) & str & "</HTML>"
65end html
66
67on getIP()
68    return do shell script "ifconfig ppp0 | grep inet | awk ‘{print $2}’"
69end getIP
70
71on betterGetIP()
72    return do shell script "curl -s http://www.showmyip.com/simple/ | grep GMT | awk ‘{print $1}’"
73end betterGetIP
74
75property allowed_URL_chars : (characters of "/$-_.+!*’(),1234567890abcdefghijklmnopqrstuvwxyz")
76property hex_list : (characters of "0123456789ABCDEF")
77
78on encode_URL_string(this_item)
79    set character_list to (characters of this_item)
80    repeat with i from 1 to number of items in character_list
81        set this_char to item i of character_list
82        if this_char is not in allowed_URL_chars then set item i of character_list to my encode_URL_char(this_char)
83    end repeat
84    return character_list as string
85end encode_URL_string
86
87on encode_URL_char(this_char)
88    set ASCII_num to (ASCII number this_char)
89    return ("%" & (item ((ASCII_num div 16) + 1) of hex_list) & (item ((ASCII_num mod 16) + 1) of hex_list)) as string
90end encode_URL_char
91
92on run
93    tell application "Adium" to activate
94    my substitute()
95end run

Getting the Active Chat in Adium

Getting the active chat in Adium from AppleScript appears at first look to be a little dicey: Adium needs to actually be the active application before the code actually works! For instance, if you open up the Script Editor, and type in the following:

tell application “Adium”     get contact of the active chat of the first interface controller end tell

You will get an empty list: {} To get a result, all you need to include is:

    activate

just before you access something belonging to the active chat. If you are creating an AdiumScript, and are testing it in the Script Editor, chances are you have used the idiom:

on run     my substitute() end run

To get this to work if you are using the active chat, it just needs to be changed to:

on run     tell application “Adium” to activate     my substitute() end run

Happy AdiumScripting!

HTML over various IM systems.

I started out using AIM via .Mac, as that is the default (only?) system for iChat. When I downloaded Adium, I discovered that you can have HTML in your chats. I don’t know (care?) how other clients handle that: but I did find out something just now. Apparently MSN and Yahoo IM don’t handle HTML in messages too well. Jabber (via Google Talk) does this just fine. I may have to code into the /itunes script a check for what account type, and only HTMLify those connections which can handle it.

Google Talk

In case you haven’t heard, Google has ‘released’ their IM system - it’s just a Jabber server, though. There are a couple of review around, and I haven’t read all of them in that much detail. It requires a Gmail account: I have plenty of invites if you want one, just email me. The settings you need are (I use Adium, your client’s names may vary): Account Type: Jabber Jabber ID: your.account@gmail.com Password: •••••••• Connect Server: talk.google.com Port: 5222 Security: Use TLS Encryption (if possible) - I couldn’t get a connection without this, some have said opposite. Others have said that any person with whom you have shared Gmail will be automatically added to your buddy list. I haven’t had anyone added to mine, but then I don’t really Gmail many people. I’ve also created accounts with the other IM players, you can find them under the Contact page.

Boing Boing: Human cannonballs

Boing Boing: Human cannonballs Great timing on this one: there is a Human Cannonball happening at this year’s Royal Adelaide Show. Apparently they do a test where they put some sandbags weighing the same as her (yes, her) into the cannon and test-fire. But some joker left them out in the rain, and the weight was much more. So they test-fired, and then when they really fired her out she overshot the net by 25 metres and had to go to hospital. That’s why it’s exciting, isn’t it? Because deep down, you hope she does hurt herself…