iTunes Script

I’m looking for some guidance on scripting iTunes. I have playlists that change, and my car stereo is stubborn, so I’m trying to fix it. This is what I want to do:

  • My master playlist for the car gets broken down into 5 individual smart playlists (long tunes, band 1, band 2, band 3, other, etc.).

  • Tunes in these playlists have VBR (variable bit rates), and I need to export them to a designated Finder folder so I can convert them into a fixed bitrate of 320 kbps. THAT PART I can do manually, the conversion. I am trying to export those tunes that are VBR, without changing the master tune in iTunes.

  • Then I need to export the normal fixed bitrate tunes to those same destination folders, to make a complete playlist with subfolders.

I’ve had a look at the iTunes library (iTunes 12.8.2.3 for El Capitan) and I’m confused about how to get smart playlist names, how to identify a smart playlist, how to identify VBR files, and how to copy those files to a destination folder that I made.

Any insight appreciated how or if I can get this accomplished.

Cheers

Hi,

First post here, hope this is OK.

I’m assuming that VBR files are those encoded using AAC, referred to in Finder as “Apple MPEG-4 audio”. Please correct me if I’m wrong!

The following script was written for the newer Music.app but I think you should be able to substitute “iTunes” without any further changes.

It identifies your smart playlists, lists them by name, asks you to choose one and then uses Finder to duplicate all Apple MPEG-4 audio tracks from that playlist to a destination folder of your choice. It doesn’t touch .mp3 files or “protected” files downloaded using an Apple Music subscription (as opposed to purchased from the Apple Music Store).

tell application "Music"
	activate
	set smartPlaylists to name of every user playlist whose smart is true -- make a list of every smart playlist
	set targetPlaylist to (choose from list smartPlaylists with prompt "Choose a playlist to duplicate:") -- gets name of chosen smart playlist
	if targetPlaylist is false then -- dialog was cancelled
		return
	else
		set targetPlaylist to targetPlaylist as string
	end if
	
	set destinationFolder to (choose folder with prompt "Choose a folder to duplicate AAC files:") -- specify destination for duplicates
	set trackLocations to location of every file track of playlist targetPlaylist -- make list of every track location in the chosen playlist
	
	
end tell

tell application "Finder"
	activate
	repeat with eachFile in trackLocations
		set trackKind to kind of eachFile
		if trackKind is "Apple MPEG-4 audio" then --it's presumably a VBR track
			try
				duplicate eachFile to destinationFolder
			on error errorNumber
				if errorNumber is -15267 then -- item already exists, skip
				end if
			end try
		end if
		
	end repeat
	display dialog "Finished duplicating AAC files" buttons {"Cool"} default button 1 giving up after 10
end tell

(You may want to mute your system volume before running it, as it makes a bit of a racket when duplicating :slight_smile: ).

It needs more error checking to make it completely bombproof (it doesn’t like playlists with no members, for example) but I hope it shows the basics of what you want to achieve.

Cheers,

H

Model: 2018 MacBook Pro running macOS Big Sur
AppleScript: 2.7
Browser: Safari 605.1.15
Operating System: macOS 10.14

Hey thanks for the post. Great script. When someone else writes AS, it is so clear. Heh.

I’ve used a test array for my source files, but the file type qualifier isn’t taking.

The MP3 files are MPEG-1, Layer 3, both normal fixed bitrate and VBR.

bit rate: 320 kbps (VBR)
bit rate: 320 kbps

That’s the only difference I see. So I’m not sure how I can trip that conditional into “VBR” or “normal”.

Any ideas?

Cheers

Just got properties of two tracks and there is absolutely NO indication of any VBR on either.

The bit rate shows up as the same 320 as the other. This might be tougher than it looks.

You should get exif metadata of audio file for this task>

  1. Download and install Command Line Interface (CLI) for MediaInfo from Here.
  2. The script proposed by hubert0, with little tweaking from my side:

property mediaInfoPath : "usr/local/bin/mediainfo "

-- specify destination for duplicates
set destinationFolder to (choose folder with prompt "Choose a folder to duplicate VBR encoded files:")

tell application "Music"
	activate
	-- get the list of every smart playlist
	set smartPlaylists to name of every user playlist whose smart is true
	-- get name of chosen smart playlist
	set targetPlaylist to (choose from list smartPlaylists with prompt "Choose a playlist to duplicate:")
	if targetPlaylist is false then return -- dialog was cancelled
	set targetPlaylist to targetPlaylist as string
	-- get the list of every track location (aliases) in the chosen playlist
	set trackLocations to location of every file track of playlist targetPlaylist
end tell

repeat with eachFile in trackLocations
	set audioFile to quoted form of (POSIX path of eachFile)
	set BitRateMode to do shell script (mediaInfoPath & "--Inform=\"Audio;%BitRate_Mode%\" " & audioFile)
	if BitRateMode is "VBR" then try -- Maybe, item already exists, try block is need
		tell application "Finder" to duplicate eachFile to destinationFolder
	end try
end repeat

display dialog "Finished duplicating VBR-encoded files" buttons {"Cool"} default button 1 giving up after 10

Hi. I see a couple ways to workaround the issue without resorting to downloading add-ons.

#1) There should be unutilized fields in iTunes’ file representations; e.g.comment, volume adjustment, season number, etc. View your library by song, then sort by bit rate; the VBR files will be contiguous, and you can select and edit them as a group (Command+I) to insert a VBR/CBR identifier that a script can target.

#2) [The initial code for this method was redacted. There appears to be a misreporting in iTunes’ files’ duration and further testing proved erroneous.]

Nice! A useful addition to the toolkit :cool:

OK, great solution. I’m finding that the iTunes bitrates are mis-represented. Get Info on a tune updates the bitrate. Been chasing that stuff.

But further testing means I have the proper files where they need to be. Just so that people know, a VBR file can also show 320 kbps. So the analytical approach might be false. Also, the hit list is minimal, so not achieving anything. Conversion in the Finder by a third app works just fine.

Onto the next question.

Thanks for all the help!

What is the reason for the conversion? Will your car player not play them?

Is there any reason for you to NOT HAVE TO convert the files that are already CBR?
(since your exporting files and keeping them completely separate from your iTunes library files)

IE Export all the playlist track files to a folder, along with a M3U playlist file.
Convert ALL the tracks as your would if the were VBR. (if the tracks were already CBR
your converter should not really do anything)
This way you don’t have to worry about your “filtering out the VBR tracks”…

I highly recommend getting your Library Maintenance done ahead of all these tasks.
Fix the VBR files and convert them once and for all. Then done.
(Also convert your files before you import them).

Here’s some handy iTunes Script Menu scripts from Doug’s Scripts

He’s using iTunes and iTunes doesn’t support FLAC.
It will support WAV but they don’t support ID3 rah embedding.

As a DJ I get FLACs and convert them to 520 m4a files
Using dBAmp Power Convertor. It’s the only converter
I’ve found that will convert to a 520 m4a all others (including XLD
Which is an awesome program) will only convert to a max 320 m4a

I have VBRs in iTunes from various sources. They’re stored on a USB3 RAID, and my first step is an export to VBR and normal parent directories, based on the internal playlists. So once exported, I use MediaHuman Converter for the VBR directories, which doesn’t take long. I then consolidate into the normal directory per child playlist, and I’m ready to copy to a 32 GB thumb drive as is.

Boom. Lovely solution.

I’m back. That mediainfo shell script is tossing errors, and I’m not sure why.

tell application "iTunes"
	do shell script "/usr/local/bin/mediainfo --Inform=\"Audio;%BitRate_Mode%\" '/Volumes/Tug/storMusic/Hits - 1984 - From a Funky House Perspective/Hits - 1984 - From a Funky House Perspective/63 Kansas City Milkman.mp3'"
		--> error number -10004
end tell

errAEPrivilegeError is error -10004

I’m assuming that means Apple Events Privilege Error, but the privs are for me (Rich) Read and Write.

And where does iTunes? Command of Standard Additions do shell script has nothing to do with this application. Remove the tell block so that you don’t have the strangest errors with script execution.

Excellent. Thank you. I thought because of the nature of the instruction, it wouldn’t matter that it was wrapped.