Mimsy Were the Borogoves

Hacks: Articles about programming in Python, Perl, Swift, BASIC, and whatever else I happen to feel like hacking at.

42 Astoundingly Useful Scripts and Automations for the Macintosh

Work faster and more reliably. Add actions to the services menu and the menu bar, create drag-and-drop apps to make your Macintosh play music, roll dice, and talk. Create ASCII art from photos. There’s a script for that in 42 Astounding Scripts for the Macintosh.

Preflight iTunes Imports

Jerry Stratton, October 12, 2005

So, uh, once you are using an AppleScript to import your music automatically, you can also add checks to make sure that iTunes is doing what you expect. For example, in looking over the new iTunes 6 to see if it has any obvious new functions, I noticed that my encoder was set to MP3 “good” (128 kbps) rather than my preferred encoder, AAC.

While experimenting with iTunes some time back, I set the encoder to MP3 and then forgot to switch it back. Last weekend’s albums were all imported as MP3 instead of AAC (and at 128 kbps instead of my preferred 192 kbps for MP3s). This isn’t nearly as bad as the time I experimented with voice encoding and set it down to 96 kbps mono several years ago, but it is something that is very easily preflighted in an AppleScript.

[toggle code]

  • on open theFolders
    • tell application "iTunes" to set theEncoder to the format of the current encoder
    • if theEncoder is not "AAC" then
      • display dialog "iTunes is not set to use AAC"
      • return
    • end if
    • --the rest of the import goes here
  • end open

This checks to ensure that the current encoder is what I want it to be. You of course will want to switch that to your preferred encoder.

Now, that does not check that the bit rate is okay. There is no way currently in iTunes (that I can find) to get the bit rate of the current encoder. But we can check the bit rate of the imported tracks after we import them. In the “on importAlbum” handler, add to the section where we set information:

[toggle code]

  • tell application "iTunes"
    • set newTrack to item 1 of (convert songFile)
    • --set the track's information
    • set newTrack's name to songTitle
    • set newTrack's album to albumName
    • set newTrack's track number to trackNumber
    • --get any information we need
    • set newBitRate to newTrack's bit rate
  • end tell
  • if newBitRate is not 128 then
    • display dialog "The bit rate is not 128. You might want to cancel."
  • end if

Replace 128 with your preferred bit rate. Preflighting and post-flighting your AppleScript workflows is always a great idea. This is one area where automating tasks can save a lot of time that would be taken up having to redo several tasks. When you have an automated task, you should only ever make a mistake once. After you make a mistake, you can add that to your list of things to automatically check for.

In response to Importing vinyl into iTunes: This script takes songs split by marker from SoundStudio and converts them directly into iTunes, setting track number, track total, and album name along with the song title.

May 7, 2006: Run a script on inserting an audio CD

Preflighting vinyl import was pretty easy, because for vinyl import, I was already using an AppleScript to do the import. However, I have the same problem when importing CDs: if I recently converted a podcast to MP3 mono and forgot to change it back, I’ll end up accidentally importing CDs as low quality mono rather than high quality stereo.

It isn’t that big of a deal, because iTunes will recognize when you re-import a CD you’ve imported before. It will ask if you want to replace the existing files, and if you say yes, it will copy all of the information over to the new, better files: play count, last played, names, ratings, even album cover if you’ve put one onto the tracks.

But it would be nice to get a warning when I’m about to import an audio CD at poor quality similar to the warning I get when I’m about to import vinyl at poor quality. Fortunately, there is an easy way of running our own script whenever we insert an audio CD.

Back when you first got your Macintosh, you might remember that sometimes, on inserting media, the Mac would ask you what you want to do with that media. The Mac then stores your preference for future use. You can reset these preferences in your System Preferences. Choose “CDs & DVDs” from the Hardware row. You can choose one of your scripts for what to do “When you insert a music CD”.

The script must be saved as File Format script, not as an application.

[toggle code]

  • on run
    • tell application "iTunes" to set theEncoder to the format of the current encoder
    • if theEncoder is not "AAC" then
      • display dialog "iTunes is not set to use AAC"
      • return
    • end if
  • end run

Obviously, if you prefer MP3 you’ll change that as your encoding format. Unfortunately, I can still find no way to check the bit rate.