Skip to content

Instantly share code, notes, and snippets.

@montchr
Last active December 3, 2024 11:25
Show Gist options
  • Save montchr/7306305 to your computer and use it in GitHub Desktop.
Save montchr/7306305 to your computer and use it in GitHub Desktop.
A script for TextExpander that displays info about the currently playing track in iTunes. Borrowed and modified from Andrew Harrison's script: http://andrew.harrison.org/uploads/2008/12/itunes.txt
-- A nice applescript for displaying what's playing in iTunes as follows:
-- Song by Artist on Album
-- 00:00/00:00
-- Original by Andrew Harrison • [email protected] • http://andrew.harrison.org
-- Modified slightly by Chris Montgomery <[email protected]>
-- Some parts from Doug's Applescript for iTunes. see below.
-- First things first, we're going to check if iTunes is running:
tell application "System Events"
if name of processes contains "iTunes" then
-- Now we know it's running, let's see if it's playing anything or if it's paused:
tell application "iTunes"
if player state is playing or player state is paused then
-- If it's playing, let's get all the info we're going to need:
-- Get Track Title
set currtrack to name of current track
-- Get Track Artist
set currartist to artist of current track
-- Get Track Album
set curralbum to album of current track
-- Get Track Rating
set currrate to round (rating of current track) / 20
-- Get Track Position
set timeVal to player position
-- player position returns number of seconds, so let's make it hours/minutes/seconds.
-- the seconds -> hh:mm:ss script is from Doug's Applescripts for iTunes
-- (http://dougscripts.com/itunes/scripts/scripts02.php?page=3#replaylastbit)
-- I found the script via iPodLounge.
set numHours to (timeVal div hours)
set timeVal to timeVal - (numHours * hours)
set numMinutes to (timeVal div minutes)
set numSeconds to round (timeVal - (numMinutes * minutes))
-- build a zero-padded string
set timeStr to "" as string
-- format the hours. only show if time is 1 hour or more
-- the if > 0 part was added by me for greater functionality
if (numHours > 0) then
if (numHours < 10) then set timeStr to "0"
set timeStr to (timeStr & numHours)
set timeStr to (timeStr & ":")
end if
-- format the minutes
if (numMinutes < 10) then set timeStr to (timeStr & "0")
set timeStr to (timeStr & numMinutes)
-- format the seconds
set timeStr to (timeStr & ":")
if (numSeconds < 10) then set timeStr to (timeStr & "0")
set timeStr to (timeStr & numSeconds)
-- thanks Doug!
-- Now, we have all the info, so let's get some output happening here
-- First, let's make a string called title info, that looks like
-- Track by Artist on Album
set titleinfo to (currtrack & " by " & currartist & " on " & curralbum)
-- next, we'll make a string called trackinfo, which will be
-- the length, rating and track number of what's playing.
-- first, get the total time of the track
set timetotal to time of current track
-- if the total time is less than 5 digits [four plus a colon]
-- add a 0 to the front so it's the same format as the current time.
if length of timetotal < 5 then
set timetotal to "0" & timetotal
end if
-- then, get the track number in the playlist of whatever's playing
set tracksleft to index of current track
-- then count the total number of tracks in the playlist
set trackstotal to count every track in current playlist
-- and output it all like such:
-- (# stars) 00:00/00:00, # of #
-- set timeinfo to "(" & currrate & " stars) " & timeStr & "/" & timetotal & ", " & tracksleft & " of " & trackstotal
-- Modified by Chris Montgomery <@montchr> 2013-11-03 19:55:38
-- 00:00/00:00
set timeinfo to "*" & timeStr & "/" & timetotal & "*"
-- because we have growltunes running, and it pops up with that
-- music video thing at the beginning of each song, we're going
-- to make the output blank for the first 5 seconds of the song
-- because it would be block by the growl thing anyway
if player position < 4 then
set output to ""
else
-- for the rest of the song, though, we want the output to be all the
-- stuff we've already organised. it will be formatted like this:
-- Song by Artist on Album [line break: "/r"]
-- (# stars) 00:00/00:00, # of #
-- if we set up geektool to update this once a second, the first counter
-- will go up each second.
set output to titleinfo & "
" & timeinfo
end if
end if
end tell
end if
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment