Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

iTunes, the menu bar, unicode, and GeekTool UNIX
I had always wanted to use the empty space on my menu bar to display the name of the tracks playing in iTuness, without my having to click on or type anything. I looked long and hard, but did not find any free apps that did this specifically. Finally, I came upon the excellent GeekTool, which many of you know of. This program allows you display information in your unused desktop space.

Using GeekTool, I created a transparent, borderless window that was exactly the height of the menu bar. I moved the window on top of the menu bar, selected the "Always on top" option, and voila! I had a setup that would always display text atop the menu bar -- seamlessly and transparently, without looking like a window at all! Then I just needed a simple AppleScript that would get the name of the song currently playing:
 tell application "iTunes"
   set foo1 to name of current track
   set foo2 to player position
   set foo3 to duration of current track
   set foo4 to foo1 & " " & foo2 & "/" & foo3
 end tell
I saved the above script as iTunesName.scpt. Then inside GeekTool, I set my menu-bar-sized window to run the following shell command every 10 seconds:
 osascript /path/to/scripts/iTunesName.scpt
Where, of course, you would replace /path/to/scripts with the location where you saved iTunesName.scpt. It worked very well, except for tracks with international characters in them. For these tracks, the special characters would come up garbled! I tried typing the above command into an xterm, and the same garbled characters came up.

After much research, I found out that for some reason the xterm program (and GeekTool) doesn't process UTF-8, the unicode format returned by AppleScript, by default. However, the program iconv, which comes with OS X, will do the trick. If you know you have tracks with special characters in them, try (shown on two lines with a continuation character):
 osascript /path/to/scripts/iTunesName.scpt | \
 iconv -f utf-8 -t ucs-2-internal
This did the trick! Characters with umlauts, accents, and so on all displayed properly, both in the xterm and in GeekTool! It goes without saying that iconv's application is not limited to iTunes -- you can use it anytime you need to display utf-8 inside an xterm. As an aside, utf-8 will display perfectly inside Apple's Terminal application.
    •    
  • Currently 3.75 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (4 votes cast)
 
[77,568 views]  

iTunes, the menu bar, unicode, and GeekTool | 23 comments | Create New Account
Click here to return to the 'iTunes, the menu bar, unicode, and GeekTool' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
iTunes, the menu bar, unicode, and GeekTool
Authored by: GaryNiger on Jul 06, '04 10:50:39AM

GeekTool can do lots of other cool stuff as well.

On my desktop I have "ps xm -O %mem" to display all running processes and which are hogging the most memory (makes it easy to see what's leaking), and below that, ifconfig en1 to see if I have an IP address (Since my school's wireless network has a tendency to fail).

I also used a friends' script that loads random girls from HotOrNot and called it from GeekTool. Every 10 seconds, a new girl :)



[ Reply to This | # ]
iTunes, the menu bar, unicode, and GeekTool
Authored by: uff on Jul 06, '04 07:22:01PM
Not to one-up you, but this is slightly better. Make ipscript.sh and then set geek tools to run the script from wherever you decide to store it:

#!/bin/sh

/sbin/ifconfig en0 | grep "inet " > /dev/null
if [ $? -eq 0 ]; then
        ifconfig en0 | grep "inet " | awk '{print "LAN: ", $2}'
else
        echo "LAN: disconnected";
fi

/sbin/ifconfig en1 | grep "inet " > /dev/null
if [ $? -eq 0 ]; then
        ifconfig en1 | grep "inet " | awk '{print "WLAN: ", $2}'
else
        echo "WLAN: disconnected";
fi


[ Reply to This | # ]
iTunes, the menu bar, unicode, and GeekTool
Authored by: wallybear on Jul 06, '04 11:41:20AM
A good idea, else if I wonder how you managed to have some space available on the menu bar (I use 1280x1024 and have none... :-( )
I did something similar with GeekTool, but in a small window at the bottom of the screen, and with more infos (track, time, Album, Equalization, Genre). Here's the script:


#!/bin/sh

if ps x | grep iTunes | grep -q -v grep;   then 
  osascript -e 'tell app "iTunes"' -e "set a to current track" 
  -e '" Track: " & name of a as text & " (" & time of a & ")" & 
  "\r\nArtist: " & artist of a & "  Album: " & album of a 
  & "\r\n    EQ: " & return & EQ of a & "  Genre: " & 
  genre of a' -e "end tell" | iconv -f utf-8 -t ucs-2-internal
else
  echo "iTunes inactive" $A
fi

As you can see the scripts checks if iTunes is running. If not, it does not execute the applescript as this would launch iTunes, but displays an informative message.
I call this shell script (I named it "whichTune") from GeekTool, using a small 3-lined translucent window with a really small font size (but readable).
The only drawback is the idle period between calls: as iTunes does not send events when a track changes, I must decide the frequency of checks; I opted for 15 seconds to reduce waste of cycles, but it's all your choice.

[robg adds: IMPORTANT NOTE ... I wrapped the above script for a much narrower display. When entering it, remove the line breaks on the "if" line until the line before "else." Replace each line break with a single space.]

[ Reply to This | # ]
iTunes, the menu bar, unicode, and GeekTool
Authored by: Pausanias on Jul 07, '04 04:58:09PM

Once you get a cinema display (1600x1050 or higher), you'll have an incredible amount of wasted menu bar space. I also use the space to display the name of the senders of incoming messages.



[ Reply to This | # ]
OK, it's not free but ...
Authored by: harmless on Jul 06, '04 12:05:22PM

... do you know Tigger? <http://www.furrysoft.de/?page=tigger>

While I'm the author, and therefore probably biased, I find that a much nicer solution. :)


Andreas



[ Reply to This | # ]
OK, it's not free but ...
Authored by: ahbe on Jul 06, '04 06:10:14PM

I gave Tigger a spin. Once I got it working, it was a slick little app. I really like the plugin ability. However, I had a few comments. First, it took me several tries to get it to even load without locking up. After the first few tries, I thought It might need a reboot. I rebooted and it still didn't work. Then, all of a sudden, about the 5th try, it just fired right up and worked. Go figure. My next problem with Tigger was the inability to remove it from the dock. There needs to be some way to have Tigger start up at logon, and not take up space in my dock. The last problem however was a killer. Tigger is a resource hog. On my first gen 12" PB it was taking up on average 12% to 19% CPU utilization. That's just unaceptable. I've bookmarked your site, and I'll check back later. If you can fix those few problems, I'll buy a copy. -- Ahbe



[ Reply to This | # ]
OK, it's not free but ...
Authored by: harmless on Jul 07, '04 06:42:04AM
First, it took me several tries to get it to even load without locking up. After the first few tries, I thought It might need a reboot. I rebooted and it still didn't work. Then, all of a sudden, about the 5th try, it just fired right up and worked.
That's news to me. Something in the console.log?
My next problem with Tigger was the inability to remove it from the dock. There needs to be some way to have Tigger start up at logon, and not take up space in my dock.
I'm working on this. Expect a new version soon.
Tigger is a resource hog. On my first gen 12" PB it was taking up on average 12% to 19% CPU utilization. That's just unaceptable.
Well, I'm afraid, that's not gonna change soon. It's quite expensive to redraw the ticker view several dozen times a second. OpenGL might help, but I'm not familiar enough with it yet.

Andreas

[ Reply to This | # ]
OK, it's not free but ...
Authored by: Pausanias on Jul 07, '04 05:02:54PM

I like Tigger, but I was looking for a solution without a "ticker." I just want a static display, updated every 20 seconds or so. In general, I don't like to see things moving on my screen unnecessarily. The CPU usage is also a concern---I generally need 100% of both my CPUs.

My suggestion would be to provide an option for an "infrequent refresh" on Tigger.



[ Reply to This | # ]
OK, it's not free but ...
Authored by: harmless on Jul 07, '04 08:53:34PM
I just want a static display, updated every 20 seconds or so.
You are not the first one to ask. :)
Tigger is really meant as a kind of system service to display various bits of information. But I will think about a solution that reduces movement to a minimum.
The CPU usage is also a concern---I generally need 100% of both my CPUs.
I don't think I can reduce it much while keeping the scrolling smooth. Try to experiment with the settings. In case a future version would reduce scrolling, that would reduce cpu time too, of course.

What I can do now is add a renice option. That way Tigger should not take cpu time when other processes need it.

Andreas

[ Reply to This | # ]
iTunes, always on
Authored by: elrac on Jul 06, '04 03:13:55PM
A little problem with this setup is that iTunes will open every time the script is run. I have a similar setup but I have my script check to see if iTunes is running first.

#!/bin/sh
set h = "       2"
if($(test $(ps -x | grep /Applications/iTunes.app/Contents/MacOS/iTunes | wc -l) -eq "       2"))
then
if($(osascript -e 'tell application "itunes"' -e 'return player state is playing' -e 'end tell'))
then
	echo "$(osascript -e 'tell application "itunes"' -e 'return artist of current track' -e 'end tell')
$(osascript -e 'tell application "itunes"' -e 'return name of current track' -e 'end tell')"
else
	echo "Not Playing"
fi
fi

This is my first, and only, code in whatever language that happens to be, so it might not be that efficient. But it works.

-Elrac

[ Reply to This | # ]

iTunes, always on
Authored by: skellener on Jul 07, '04 07:23:40PM

There's always "Synergy"

http://synergy.wincent.com

Has a floater with album info and a control set in the menubar. Really nicely done and no scipting required.



[ Reply to This | # ]
iTunes, the menu bar, unicode, and GeekTool
Authored by: alys on Jul 08, '04 01:47:14AM
What a great idea!
Here's my version of the script, which works basically the same way, but which contains more track information, allows me to easily add or remove that information from the display (by editing the second last line of the script), and adds a bit of punctuation to separate the pieces of information. The code that formats the rating as asterisks could undoubtably be improved, but I didn't think it was really worth the effort of learning the right AppleScript syntax. :) Note that this script still has the problem of starting iTunes if it's not already running, so you'll need to wrap it in shell script as reported in other comments, if you don't normally have iTunes running all the time (alternatively, in GeekTool, put the script in a particular group and only use that group when you're playing music).

tell application "iTunes"
    -- This script extracts information about the track currently running in iTunes, and then
    -- formats it for one-line display (e.g., using GeekTool).
    -- See http://www.macosxhints.com/article.php?story=2004070301135736 for a
    -- detailed description of a similar script that gave me the idea for this one.

    -- this spacer determines how many spaces are between some of the items in the display:
    set spacer to "     "

    -- get some simple information and add spaces and punctuation:
    set myName to name of current track & "; "
    set myArtist to artist of current track
    set myGenre to spacer & "(" & genre of current track & ")"
    set myGrouping to spacer & "{" & grouping of current track & "}"

    -- if the track is in an album, then put a dash before the album name (to separate
    -- it from the information that will be printed before the album):
    set myAlbum to album of current track
    if myAlbum is not "" then
        set myAlbum to " - " & myAlbum
    end if

    -- get the rating, or a message if there is no rating:
    set myRating to round ((rating of current track) / 20)
    if myRating is 1 then
        set myRating to "*"
    else if myRating is 2 then
        set myRating to "**"
    else if myRating is 3 then
        set myRating to "***"
    else if myRating is 4 then
        set myRating to "****"
    else if myRating is 5 then
        set myRating to "*****"
    else
        set myRating to "NO RATING"
    end if
    set myRating to spacer & myRating

    -- get the current position of the track and calculate how far through
    -- the track we are, as a percentage:
    set myPosition to player position
    set myDuration to duration of current track
    set myPercent to spacer & (round (myPosition / myDuration * 100)) & "%"

    -- this final line determines which pieces of information are displayed:
    set output to myName & myArtist & myAlbum & myPercent & myRating & myGrouping
end tell



[ Reply to This | # ]
iTunes, the menu bar, unicode, and GeekTool
Authored by: Neil Gall on Jul 08, '04 05:16:52AM
I created a general purpose shell script which only executes the applescript if the appropriate application is running:

#!/bin/sh
app=$1
scpt=$2
if [[ -n "`ps -x | grep /Applications/${app}.app | grep -v grep`" ]]; then
  osascript $scpt | iconv -f utf-8 -t ucs-2-internal
fi
Then all my GeekTool shell commands look like

~/Scripts/do_script_if_app_running iTunes ~/Scripts/get_current_itunes_track.scpt

~/Scripts/do_script_if_app_running Mail ~/Scripts/check_mail.scpt
etc.

[ Reply to This | # ]
iTunes, the menu bar, unicode, and GeekTool
Authored by: alys on Jul 17, '04 11:42:09PM
That's useful! Thanks!
If you change this line:
if [[ -n "`ps -x | grep /Applications/${app}.app | grep -v grep`" ]]; then
to this:
if [[ -n "`ps -x | grep -i /Applications/.*${app}.app | grep -v grep`" ]]; then
then you don't need to type the application name with the correct upper- and lower-case letters (the -i switch to grep does a case-insensitive search), and you can specify applications that exist in subdirectories under the Applications directory (.* handles that). An unfortunate side effect of the latter change is that if you have applications called (for example) MyBooks.app and Books.app (i.e., two or more applications ending with the same word), then entering "Books" as the application name might launch the wrong application. This isn't a problem for me (at the moment!) but bear that in mind if you add '.*' to your code.

[ Reply to This | # ]
iTunes, the menu bar, unicode, and GeekTool
Authored by: webbix on Jul 21, '04 05:12:01PM
Thanks for the script. With that and another hint on checking for iTunes process I got mine running. I deployed slightly different as I place mine just below the menu bar as I have other data there and no Cinema display.

I did make a few changes the most important of which was to change the percent reading to a rudimentary progress bar. Just easier for me to visualize. Here is the code I subsittuted (also change the stars to take less room by subbing 1*, 2*, etc.):


	-- get the current position of the track and calculate how far through
	-- the track we are, as a percentage:
	set myPosition to player position
	set myDuration to duration of current track
	set myPercent to (round (myPosition / myDuration * 100))
	if myPercent < 10 then
		set myPercent to "]:::::::::"
	else if myPercent < 20 then
		set myPercent to ":]::::::::"
	else if myPercent < 30 then
		set myPercent to "::]:::::::"
	else if myPercent < 40 then
		set myPercent to ":::]::::::"
	else if myPercent < 50 then
		set myPercent to "::::]:::::"
	else if myPercent < 60 then
		set myPercent to ":::::]::::"
	else if myPercent < 70 then
		set myPercent to "::::::]:::"
	else if myPercent < 80 then
		set myPercent to ":::::::]::"
	else if myPercent < 90 then
		set myPercent to "::::::::]:"
	else
		set myPercent to ":::::::::]"
	end if
	set myPercent to spacer & myPercent

Thanks again for the original script!

[ Reply to This | # ]
Lists next 5 songs on desktop...
Authored by: macmath on Jul 09, '04 02:36:52PM

This applescript is an obvious rip-off of the original poster's script, with none of the improvements of the posters above, but used instead of the original authors applescript, it lists the previous song, current song, and the next 4 songs (each with the name of the artist).
[code]
tell application "iTunes"
set foo5 to name of track 5 of playlist 2
set foo5a to artist of track 5 of playlist 2
set foo6 to name of track 6 of playlist 2
set foo6a to artist of track 6 of playlist 2
set foo7 to name of track 7 of playlist 2
set foo7a to artist of track 7 of playlist 2
set foo8 to name of track 8 of playlist 2
set foo8a to artist of track 8 of playlist 2
set foo9 to name of track 9 of playlist 2
set foo9a to artist of track 9 of playlist 2
set foo10 to name of track 10 of playlist 2
set foo10a to artist of track 10 of playlist 2
set foo7 to foo5 & " by " & foo5a & "
" & foo6 & " by " & foo6a & "
" & foo7 & " by " & foo7a & "
" & foo8 & " by " & foo8a & "
" & foo9 & " by " & foo9a & "
" & foo10 & " by " & foo10a
end tell
[/code]

By the way, in writing this, I found out that AppleScript, like Perl considers '\n' to be a command to start a new line.



[ Reply to This | # ]
...for playlist 2.
Authored by: macmath on Jul 09, '04 02:39:19PM

I don't know why the code tags didn't work...

This uses the 2nd playlist, obviously, which for me is the 'Party Shuffle'.



[ Reply to This | # ]
iTunes Track does exactly that
Authored by: sebid on Jul 11, '04 05:13:43AM
Try my iTunes Track app.
It displays a little window with the currently playing track in it. But you can also make the window transparent and move it in front of the menubar. So it does exactly what you wanted.

It also announces a new track by speech.

And the best: it's free!



[ Reply to This | # ]
iTunes, the menu bar, unicode, and GeekTool
Authored by: vman on Oct 09, '04 12:23:32PM
Here's another variation; I mainly wanted to be able to see which tracks are playing on some streams i often listen to (by the way all the existing freeware apps I checked didn't do this) so I've added it. Also because of my limited space left on my menubar I kept it very simple displaying only title, artist & album. Using a small font with geektool it display 2 lines just fitting in the menubar. tnx to all posters
#!/bin/sh
if ps x | grep iTunes | grep -q -v grep;   then 
  osascript -e 'tell application "iTunes"
	set a to current track
	if current stream title is not missing value then
		set b to current stream title
	else
		set b to artist of a & "-" & album of a
	end if
	name of a & "\n" & b
  end tell' | iconv -f utf-8 -t ucs-2-internal
fi


[ Reply to This | # ]
iTunes, the menu bar, unicode, and GeekTool
Authored by: Skeeve on Aug 18, '05 02:24:16AM
Just to add a bit of information that migt be useful for you. If you want to find out whether or not some application, like iTunes in this example is running you can ommit one grep. Usually I see this:

ps x \
| grep /Applications/iTunes.app/Contents/MacOS/iTunes \
| grep -v grep
The last grep to filter out the grep off the result. I think it's much easier to do if you take advantage of character classes. (cite of man grep)

A list of characters enclosed by [ and ] matches any  single  character
in that list; if the first character of the list is the caret ^ then it
matches any character not  in  the  list.   For  example,  the  regular
expression  [0123456789]  matches any single digit.
So try this when your iTunes is running:

ps x | grep [/]Applications/iTunes.app/Contents/MacOS/iTunes
You see: The first slash is in a character class and so grep can't find itself because it does no longer contain "/Appli..." but "[/]Appli..." Next thing to note is that there is a thing like the "HERE"-document in *nix and so in OS X too. (cite of man bash but true for all shells I know of)

Here Documents
This  type  of  redirection  instructs the shell to read input from the
current source until a line containing  only  word  (with  no  trailing
blanks)  is seen.  All of the lines read up to that point are then used
as the standard input for a command.

The format of here-documents is:

              <<[-]word
                      here-document
              delimiter

No parameter expansion, command substitution, arithmetic expansion,  or
pathname expansion is performed on word.  If any characters in word are
quoted, the delimiter is the result of quote removal on word,  and  the
lines  in the here-document are not expanded.  If word is unquoted, all
lines of the here-document are subjected to parameter  expansion,  com-
mand  substitution,  and arithmetic expansion.  In the latter case, the
character sequence \<newline> is ignored, and \ must be used  to  quote
the characters \, $, and `.
If you also know, that osascript can read from standard input if you supply "-" instead of a filename, we can put this all together like this:

#!/bin/sh
if ps x \
| grep [/]Applications/iTunes.app/Contents/MacOS/iTunes\ >/dev/null
then
        osascript - <<'OSASCRIPT'
        tell application "iTunes"
                set foo1 to name of current track
                set foo2 to player position
                set foo3 to duration of current track
                set foo4 to foo1 & " " & foo2 & "/" & foo3
        end tell
OSASCRIPT
fi
Just two last notes about the if-line: 1. the \ is there to make the following blank part of the string to search for 2. >/dev/null is there to suppress the output of grep. We are only interested in grep's return code.

[ Reply to This | # ]
iTunes, the menu bar, unicode, and GeekTool
Authored by: nigeltufnel on Feb 13, '09 05:59:53PM

Here is my adaptation building on the above outstanding work. It results in this output:


iTunes: Song - Artist (Album) | rating | progress%


if there is no album then that field is dropped
if there is no rating then that field too is dropped

it will cause iTunes to open at login which I think is great!


tell application "iTunes"
set trackname to name of current track
set artistname to artist of current track
set albumname to album of current track
if albumname is null then
set albumshow to " "
else if albumname is "" then
set albumshow to " "
else
set albumshow to " ( " & albumname & " ) "
end if
set trackduration to duration of current track
set trackposition to player position
set elapsed to round (trackposition / trackduration * 100)
set myRating to round ((rating of current track) / 20)
if myRating is 1 then
set myRating to "| * "
else if myRating is 2 then
set myRating to "| ** "
else if myRating is 3 then
set myRating to "| *** "
else if myRating is 4 then
set myRating to "| **** "
else if myRating is 5 then
set myRating to "| ***** "
else
set myRating to ""
end if
set myRating to myRating
set output to "iTunes: " & trackname & " - " & artistname & albumshow & myRating & "| " & elapsed & "%"
end tell

---
These go to eleven.



[ Reply to This | # ]
iTunes, the menu bar, unicode, and GeekTool
Authored by: russkey on Apr 28, '09 09:34:43AM

anybody find a way to get geektool to show unicode? doesn't work for russian or japanese. any hints? (p.s. has geektool development stalled? any alternatives at some point?)



[ Reply to This | # ]
iTunes, the menu bar, unicode, and GeekTool
Authored by: iBlock on Feb 21, '10 10:27:23AM
I made a custom AppleScript that displays the Title, Artist and Album. I made it not display the album name if it is more than 35 characters long because of my limited menu bar space.

tell application "iTunes"
	set trackname to name of current track
	set trackartist to artist of current track
	set trackalbum to album of current track
end tell
set albumChar to count trackalbum
if albumChar is greater than 35 then
	set display to trackname & " by " & trackartist
else
	set display to trackname & " by " & trackartist & " from " & trackalbum
end if


[ Reply to This | # ]