I have an Applescript that reports the currently playing song name, album, song number, and total number of songs on the album. If the album comes from a single CD or if the song comes from CD 1 of a multi-CD album, everything works, and I get the following:
Song 2 of 30
However, if the album comes from a multi-CD set, then the song number is incorrect for songs from CD 2, CD 3, etc. Here's an example: L'Elisir d'Amore has two CDs, and in iTunes, it is entered as such (e.g., CD 1 of 2 and CD 2 of 2). The script correctly reports there are 30 songs (16 on CD 1 and 14 on CD 2). When playing a song from CD 1, everything is accurate. When playing a song from CD 2, the song number begins again at 1, so the very last song appears as
Song 14 of 30
because it is the 14th song on CD 2. Is there a way to get the script to display Song 30 of 30 for the last song on CD 2 without some kludge of figuring out the number of CDs, then counting up the songs in CD 1 and then adding to the track number of the song in CD 2? This kludge gets quite ugly for albums spanning as many as 14 CDs.
Here's the simplified bit of code:
tell application "iTunes"
-- code to check that there is a current song
set z_album to album of current track as string
set z_num to count of (every track whose album is z_album)
set z_song to name of current track as string
set z_trk to track number of current track
log z_song & " (" & z_trk & " of " & z_num & ")"
end tell
Best Answer
Personally I do not have much of a need, if any, to automate iTunes with AppleScript. In the past I've found iTunes to be one of the more difficult apps to automate. Nonetheless, I took a look at your code, the AppleScript Dictionary for iTunes, a few web searches that basically yielded nothing noteworthy, and some failed programmatic attempts to easily get a list of discs and
track count
for each disc of analbum
.I'll assume the kludge context in your question is because there is no easy direct way to programmatically ask an
album
for itsdisc count
andtrack count
per disc to do the math with, in a straight forward manner. So with that... here's my kludge, which was tested and works as desired on my iTunes Library:The example AppleScript code, shown above, does the following:
current track
to the variabletheCurrentTrackProperties
.disc number
property in the variable,theCurrentTrackProperties
, is not greater than 1.disc number
property is not greater than 1, it's straight forward processing and logs the desired information.disc number
property is greater than 1, then theelse
branch preforms the kludge to do the math, and logs the desired information.What the kludge does:
disc number
property of thecurrent track
,theDiscNumber
, is used to determine how many discs thetrack count
is needed for.track
of everydisc
in thealbum
,theAlbumsTracksProperties
.theAlbumsTracksProperties
, is processed in arepeat
loop to which the output of is held in the variabletheAlbumsDiscNumberTrackCountInfo
.theAlbumsDiscNumberTrackCountInfo
, is a list of thedisc number
andtrack count
from everytrack
in thealbum
.track count
of eachdisc number
in thealbum
.do shell script
command usessort
andsed
to process the list, the value of the variabletheAlbumsDiscNumberTrackCountInfo
.sort -n -k1 -u <<<
-n
compare according to string numerical value-k1
start a key at POS1-u
without -c, output only the first of an equal run<<<
directs input from the command line, not a filetheAlbumsDiscNumberTrackCountInfo
the list as command line input directed tosort
| sed '/^$/d'
|
pipe the output of thesort
command tosed
sed '/^$/d'
delete blank linestheAlbumsDiscNumberTrackCountInfo
is a concatenation with alinefeed
at the end of eachrepeat
loop, it becomes the first line of output from thesort
command and is removed withsed
, so as not to have to account for the leading blank line later in the script. If it was left as a part of value of the variabletheAlbumsDiscNumberTrackCountInfo
, that's (re)set by the return of thedo shell script
command, thenword 2 of paragraph i
would have to beword 2 of paragraph (i + 1)
in order to skip the leading blank line. So it makes more sense to remove it before it ends up as part of the final value of the variabletheAlbumsDiscNumberTrackCountInfo
.theAlbumsDiscNumberTrackCountInfo
, now only containing a list ofdisk number
with its correspondingtrack count
, one line for each disc in thealbum
, it's now easy to do the math, whether thealbum
contains 2 discs or as many as 14 discs, or more.repeat
loop, preforms the math necessary to add up thetrack count
of eachdisc number
preceding thedisc number
thecurrent track
is from, and adds them together, and then logs the desired information.That said, if anyone knows an easy direct way to programmatically ask an
album
for itsdisc count
andtrack count
per disc to do the math with, in a straight forward manner to avoid a kludge, please post an answer.