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
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
– Better itunes adium link script
– Includes rating (if present), and links.
property message_prefix : «data utxt266B0020» as Unicode text
property not_listening_message : "Quiet"
property star : «data utxt2605» as Unicode text
property google : "http://www.google.com/search?q="
property server : ""

property html_classes : {"AIM-compatible", "Jabber"}

on substitute()
    –set server to "http://" & my getIP() & ":8080" — Use betterGetIP if on broadband!
    
    tell application "System Events"
        try
            get process "iTunes"
        on error
            return message_prefix & not_listening_message
        end try
    end tell
    
    tell application "iTunes"
        if player state is not playing then
            return message_prefix & not_listening_message
        end if
        set theArtist to artist of current track
        if theArtist is "" then set theArtist to "Unknown Artist"
        set theTrack to name of current track
        set theRating to " " & my myRating(rating of current track)
        set theLocation to location of current track
        set pth to my encode_URL_string(characters 9 thru end of (POSIX path of theLocation) as string)
    end tell
    
    tell application "Adium" to set accountType to serviceClass of contact of the active chat of the first interface controller
    if first item of accountType is in html_classes then
        return html(message_prefix & my myLink(google, theArtist) & " • " & my myLink(server & pth, theTrack) & theRating)
    else
        return message_prefix & theArtist & " • " & theTrack & theRating
    end if
end substitute

on myRating(theRating)
    set theResult to ""
    set theTimes to (theRating - 9) / 20 as integer
    repeat theTimes times
        set theResult to theResult & star
    end repeat
    return theResult
end myRating

on myLink(URI, theText)
    if URI is google then
        set URI to URI & first word of theText
        repeat with wd in (rest of every word of theText)
            set URI to URI & "+" & wd
        end repeat
    else if server is "" then
        return theText
    end if
    return "<a href=\"" & URI & "\">" & theText & "</a>"
end myLink

on html(str)
    return ("<HTML>" as Unicode text) & str & "</HTML>"
end html

on getIP()
    return do shell script "ifconfig ppp0 | grep inet | awk ‘{print $2}’"
end getIP

on betterGetIP()
    return do shell script "curl -s http://www.showmyip.com/simple/ | grep GMT | awk ‘{print $1}’"
end betterGetIP

property allowed_URL_chars : (characters of "/$-_.+!*’(),1234567890abcdefghijklmnopqrstuvwxyz")
property hex_list : (characters of "0123456789ABCDEF")

on encode_URL_string(this_item)
    set character_list to (characters of this_item)
    repeat with i from 1 to number of items in character_list
        set this_char to item i of character_list
        if this_char is not in allowed_URL_chars then set item i of character_list to my encode_URL_char(this_char)
    end repeat
    return character_list as string
end encode_URL_string

on encode_URL_char(this_char)
    set ASCII_num to (ASCII number this_char)
    return ("%" & (item ((ASCII_num div 16) + 1) of hex_list) & (item ((ASCII_num mod 16) + 1) of hex_list)) as string
end encode_URL_char

on run
    tell application "Adium" to activate
    my substitute()
end 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…