MacOS Automator workflow to populate iTunes info from file path

Just in case someone has the same need, here is an Automator workflow to update the metadata of a music (or video) file based on the file’s path (for non-Mac users, Automator is a graphical task automation tool in MacOS).

Here the context: I recently bought my first Mac, a home desktop. That’s where my MP3 reside now, carefully arranged in folders by artist name. I made sure to plop them in the default “Music” folder, to make  iTunes happy. Or so I thought, until I actually started iTunes and saw it attempt to copy every single MP3 file in its own directory. Pretty stupid, but easily fixed via a config setting. The next issue is that, within iTunes, you cannot organize your music based on the directory structure. All it cares about is the various metadata fields. You can’t even display the file name or the file path in the main iTunes window.

Leave it to Apple to create a Unix operating system which hates files.

The obvious solution is to dump iTunes and look for a better music player. But there’s another problem.

Apparently music equipment manufacturers have given up on organizing your digital music and surrendered that function to Apple (several models let you plug an SD card or a USB key, but they don’t even try to give you a decent UI to select the music from these drives). They seem content to just sell amplifiers and speakers, connected to an iPod doc. Strange business strategy, but what do I know. So I ended up having to buy an iPod Classic for my living room, even though I have no intention of ever taking it off the dock.

And the organization in the iPod is driven by the same metadata used by iTunes, so even if I don’t want to use iTunes on the desktop I still have to somehow transfer the organization reflected in my directory structure into Apple’s metadata fields. At least the artist name; I don’t care for albums, albums mean nothing. And of course I’m not going to do this manually over many gigabytes of data.

The easy way would be to write a Python script since apparently some kind souls have written Python modules to manipulate iTunes metadata.

But I am still in my learn-to-use-MacOS phase, so I force myself to use the most MacOS-native solution, as a learning experience. Which took me to Automator and to the following workflow:

OK, I admit it’s not fully MacOS-native, I had to escape to a shell to run a regex; I couldn’t find a corresponding Automator action.

I run it as a service, which can be launched either on a subfolder of “Music” (e.g. “Leonard Cohen”) or on a set of files which are in one of these subfolders. It just picks the name of the subfolder (“Leonard Cohen” in this case) and sets that as the “artist” in the file’s metadata.

Side note: this assumes you Music folder is “/Users/vbp/Music”, you should replace “vbp” with your account user name.

For the record, there is a utility that helps you debug workflows. It’s “/System/Library/CoreServices/pbs”. I started the workflow by making it apply to “iTunes files” and later changed it to work on “files and folders”. And yet it didn’t show up in the service list for folders. Running “pbs -debug” showed that my workflow logged NSSendFileTypes=(“”); no matter what. Looks like a bug to me, so I just created a new workflow with the right input type from the start and that fixed it.

Not impressed with iTunes, but I got what I needed.


I’ve improved it a bit, in two ways. First I’ve generalized the regex so that it can be applied to files in any location and it will pick up the name of the parent folder. Second, I’m now processing files one by one so that they don’t all have to be in the same folder (the previous version grabs the folder name once and applies it to all files, the new version retrieves the folder name for each file). This way, you can just select your Music folder and run this service on it and it will process all the files.

It’s pretty inefficient and the process can take a while if you have lots of files. You may want to add another action at the end (e.g. play a sound or launch the calculator app) just to let you know that it’s done.

In the new version, you need to first create this workflow and save it (as a workflow) to a file:

Then you create this service which references the previous workflow (here I named it “assign artist based on parent folder name”). This service is what you invoke on the folders and files:

I haven’t yet tried running more than one workflow at a time to speed things up. I assume the variables are handled as local variables, not global, but it was too late at night to open this potential can of worms.


Filed under Apple, Automation, Everything, Off-topic

13 Responses to MacOS Automator workflow to populate iTunes info from file path

  1. Vega

    Or, instead of making the nerdiest unnecessary contortions I have ever seen, you could have just dragged all of your mp3s into the itunes window. Then itunes would have neatly arranged them according to your metadata. And, as a geek special, it would have also put them in neat folders arranged by band, with album subfolders…

    I fail to understand why some geeks insist that ‘their’ data abstraction (=OS filesystem) is superior to a far more versatile abstraction that the relevant software (itunes) uses.

  2. @vambenepe

    Hi Vega,

    I’ll grant you that these are “nerdy contortions” and that there are existing alternatives (e.g. Oddbjørn Steffensen pointed me to Enqueue and MusicBrainz Picard). As I wrote in the post, part of my motivation is to learn MacOS tools. Whether this workflow is useful or not, it showed me how to return to previous values (by storing/retrieving them) and how to loop (by referencing an external workflow). In general, there aren’t very many Automator references I could find, so here’s my small contribution to other people trying to bend it to their will. What I really hoped to find was some kind of mapping from common programming language structures (e.g. control loop, parsing, etc…) to Automator actions. But I guess the people who would find these mappings useful are the people who don’t really have a need for Automator in the first place…

    BTW, your proposed “solution” wouldn’t work for me as “neatly arranged them according to your metadata” isn’t so neat when the metadata it all over the place. That’s my issue, that as of now the actual organization is reflected in the directory structure, not the metadata. Whether that’s good or bad that’s a fact and what I have to deal with. Based on the current messy metadata, having iTunes “put them in neat folders arranged by band, with album subfolders” would mean transforming my existing neat folders into a mess of inconsistently named folders.

    As to whether one should rely on filesystem or some app’s metadata structure, let me put it that way: For files that I plan to hang on for a long time, you bet I trust the filesystem more than any metadata. So, I’ll keep organizing my music with folders named after the artist and I’ll keep sorting my photos in folders named YYYYMMDD-description, no matter what metadata Lightroom, Picassa or others would rather rely on.

  3. Since around 2001 I switched to metadata based music/photos rather than folder based. I began to realise I liked to be able to sort (or search) my music by composer, or year, or track length. None of this is possible within a file system. So you still need an application (be it Apple’s software or Winamp or whatever) to allow for that. Once you have this music management application, it seemed redunant to manually organise the folder as well as the internal data. It felt a little like I was doing the same thing twice.

    “I trust the filesystem more than any metadata”
    I’m curious about this statement. Have you had or heard of any specific bad experiences with things going wrong with a file’s metadata? Maybe I’m being naive by trusting Winamp/iTunes/Aperture/Lightroom/Picasa, becase I like you plan to keep my photos/music for a long time.

    It just feels like a lot of work for no gain. Kind of like removing power steering from your car because “what if it fails?”

  4. Vega

    MP3 Metadata has been standardized since the beginning of the MP3 format. That’s not gonna change. What your difficulties show is the danger of not adhering to standardized methods of transporting information about your music (your proprietary directory system as opposed to metadata). Also, once you have imported your music into iTunes, who cares which directory format the software uses? Just build playlists, smart playlists and everything you need in iTunes, which will then even enable you to benefit from features like genius. Again, why do you believe one data abstraction (OSX file system) is better than the other (iTunes song management)? Just use the Software the way it is intended, your life will
    be easier (and more future proof).

  5. Kelstar

    Wow! What an unbelievable, OCD-ridden waste of time.

  6. Right. mp3 files have an ID3 (depending on where you got them) container of metadata: already built in.

    iTunes extracts the metadata from the ID3 container, stores it in it’s database as individual metadata items and allows you to sort or otherwise organize your music based on the values.

    I wish more file types had standardized metadata like this (and jpeg’s Exif) and I wish more filesystems and data storage interfaces leveraged metadata to help you find stuff. We will get there, but legacy habits die hard….

    — mark

  7. Dogbertd

    Nice work, and I fully understand why you want to control your data, and not hand control over to iTunes, which is one part of the Mac OS I really hate. I have my music organised in the same way as you, but opted for the simpler process of telling iTunes *not* to organise my data and then using Add Folder to make itunes pick up my stuff. This way I can access everything (or at least those formats iTunes likes – no FLAC!) and still keep the filesystem the way I want it.

    Anal? OCD? Perhaps – but it’s my stuff, my computer and I want it the way *I* want it.

  8. Having worked as a trainer for a well known “electric fruit company” for a number of years I am very familiar with directory structure obsessives. In my experience they are almost exclusively older men and sooner or later turn up, crestfallen, because they have screwed up their own directory structure or worse, screwed up a managed directory structure. iTunes creates a basic (and obvious) structure from my own organisation within iTunes. I am very happy with that. I don’t have to think about it.

    All that being said, iTunes (application and store) doesn’t have a good inherent structure. It uses too few metadata so finding stuff and imposing order is far more labour intensive than it should be.

  9. Ivan

    Although the mp3 meta-data has been standardised for a long time I too do not fully trust it.

    Although I do like the standardised layout of files (music > artist > album) and choose to have text files of lyrics with the same name as the song (file extension txt instead of mp3 of course) and the appropriate album art stored there too.

    It means my music is easily found, problems easily solved, different standards could be quickly accomodated.

    Trusting the likes of Apple or Microsoft to stick to standards seems foolhardy considering their history (embrace, extend, extinguish) so belt and braces seems a good idea and I have some sympathy with Mr Vambenepe.

    I am also intrigued that he chose Automator over Applescript, but he’s got the job he wanted doing done and learnt something too!

  10. Alex

    I believe you man. I hate things rearranging my music, and use musicbrainz picard to tag stuff consistently. That said I often play or organise my music through different players and systems, and a consistent directory structure is the only thing you can enforce between these.

    Ditch iTunes IMO.

    Can you use strict; using automator?

  11. @vambenepe

    Thanks all for the comments. Since they’re all about my decision to rely on the filesystem rather than iTunes metadata (rather than on the real point of this post which is to share Automator scripts), I have responded in a new blog post:
    Why I don’t use iTunes metadata

  12. Vega

    Your link has a ” after the URL, currently leads to a 404 page.

  13. @vambenepe

    Thanks Vega, I fixed it.