0

I am trying to automate some changes in m4a metadata and would prefer doing it through AppleScript other than popular solutions like mutagen.

How can I address m4a files metadata? I need not only setting metadata to specific values, I need to read these values first.

Basically what I need to do is, set TRACK_NAME to ARTIST + " - " + TRACK_NAME, and then TRACK_NAME to ALBUM_ARTIST. This is required for my media player to not spread compliations all over the library. It doesn't know any better, unfortunately.

  • You said "This is required for my media player to not spread compliations all over the library" however you haven't mentioned what the "media player" is and whether or not you're referring to hardware or software in this context. Is it iTunes you're referring to or what? – user3439894 Aug 25 '15 at 13:41
  • Do you use iTunes? Because it would be easier to just use iTunes to set the metadata, if you'ren't unless you feel comfortable with AppleScript. Also do you want to change the file name which you see in Finder or the Track Name which you see in the media players? – user14492 Aug 25 '15 at 14:42
  • Media player I am using is an iBasso DX50, and its software does not understand "Part of a Compilation" flag and also does not make use of "Album Artist" field. Thus if you copy a compilation album on this player, your onboard library gets messed up. – blackeyeliner Aug 26 '15 at 7:23
  • I am using iTunes on my Mac to manage my ALAC media library, and all the tags are done in a correct way. iTunes does understand compilations, so no problem there. – blackeyeliner Aug 26 '15 at 7:24
  • I am comfortable with any kind of programming language and I've already implemented this via python's mutagen, but I think that AppleScript would be a more elegant solution here, as it's a native automation language - I just don't have experience with AppleScript specifically. So ideally what I want this script to do is to: - Copy selected files from iTunes to a different folder (files selected in iTunes window, folder is fixed) - this step can be omitted, but if possible, I'd automate this as well. - For copied files, change m4a metagada in a way described in the original post. – blackeyeliner Aug 26 '15 at 7:28
1

Here is a quick something to get you started. If you also want the filename changed too, then it is easy to add in the copy section...

set dpath to "/some/path/some/where/"
set qdpath to quoted form of dpath

# Choices of apps to use depending what files you have
# mp3
set tapp3 to "/usr/local/bin/id3v2 -t "
# m4a
set tapp4 to "/usr/local/bin/mp4tags -s "

tell application "iTunes"
    set iSel to selection

    repeat with trk in iSel
        set aloc to location of trk
        set loc to quoted form of (POSIX path of aloc)
        set tname to name of trk
        set tartist to artist of trk
        tell application "Finder"
            set fname to name of file (aloc as alias)
            set fext to name extension of file (aloc as alias)
        end tell


        # Copy
        try
            do shell script "cp " & loc & " " & qdpath
        on error eStr
            display dialog eStr
            return
        end try


        # New path
        set qnewfile to quoted form of (dpath & fname)


        # Choose right tool...
        if fext is "mp3" then
            set tapp to tapp3
        else if fext is "m4a" then
            set tapp to tapp4
        else
            display dialog "Unknown file type. Don't know what to use..."
            return
        end if


        # Tag 
        try
            do shell script tapp & "'" & tartist & " - " & tname & "' " & qnewfile
        on error eStr
            display dialog eStr
            return
        end try
    end repeat
end tell

Since you don't like using command line tools and Applescript doesn't have built in support for audio tags, you are left with iTunes. So here is a version of the above with just using iTunes,

set dpath to "/some/path/"
set qdpath to quoted form of dpath
set listname to "MyList"
set adpath to (POSIX file dpath as alias)

# Copy selected itunes files
tell application "iTunes"
    set iSel to selection

    repeat with trk in iSel
        set tloc to location of trk
        set atloc to quoted form of (POSIX path of tloc)
        set tname to name of trk
        set tartist to artist of trk

        tell application "Finder"
            set fname to name of file (tloc as alias)
            set fext to name extension of file (tloc as alias)

            try
                duplicate file tloc to folder adpath with replacing
            on error eStr
                display dialog "Copy failed for track: " & tname & return & return & eStr
                return
            end try
        end tell
    end repeat
end tell


# Get audio files in selected folder 
#   -- alas no recursive search results without a lot more code... :(
set afiles to {}
tell application "Finder"
    repeat with ext in {".mp3", ".m4a"}
        set l to (every file in adpath whose name contains ext)
        repeat with f in l
            set end of afiles to (f as text)
        end repeat
    end repeat
end tell


# Add to iTunes and change Tags...
#
tell application "iTunes"
    if not (user playlist listname exists) then
        make new user playlist with properties {name:listname}
    end if

    set view of window 1 to playlist listname

    # Add and change tracks' names
    repeat with afile in afiles
        try
            set tid to (add afile to playlist listname)
        end try

        set tname to name of tid
        set aname to artist of tid

        set name of tid to ((aname & " - " & tname) as text)
    end repeat

    # Clean up now?
    set r to display dialog "Done. " & return & return & ¬
        "Remove tracks and list now?" as text buttons {"No", "Yes"} with icon note

    if button returned of r is not "Yes" then
        return
    end if


    # Remove playlist tracks from library; will remove from playlist(s) too
    #   -- WARNING: If track added multiple times all entries will be removed!! 
    repeat with t in (every track in playlist listname)
        try
            set pid to persistent ID of t

            # Thanks Doug Adams, it was driving me nuts....
            delete (some file track of library playlist 1 whose persistent ID is pid)
        end try
        delay 1
    end repeat

    delay 30

    # Remove all tracks from playlist v2
    try
        # Fast mass deletion - Thx to http://apple.stackexchange.com/a/56463
        # but only removes from playlist.. they remain in the library!
        #delete tracks of playlist listname
    end try

    try
        delete playlist listname
    end try
end tell
  • Yes, this was exactly what I needed! I'd probably try to do it without shell commands, but it works this way as well. My main concern was how to read metadata because the file is external to iTunes library, but starting with library it gets really easy. Thank you! – blackeyeliner Aug 27 '15 at 7:30
  • @Anton Danilov The mp4v2 package (where mp4tags comes from) includes a tool to read tags as well. – Vic Aug 27 '15 at 11:05
  • thanks, I've found out that already as well, mp4v2, mutagen, a lot of options are out there - my idea was to be as "apple-style" as possilbe, however I understand that apple-style is not to use iBasso player and buy iPod - SQ-wise, this is not my path unfortunately :) Thank you! – blackeyeliner Aug 27 '15 at 20:25
  • @blackeyeliner I've updated my answer with a version using Finder and iTunes, so no command line tools at all. – Vic Aug 31 '15 at 18:23
  • So as far as I understood from the code, it first copies files to external location, then adds the files back to iTunes, changes tags and deletes them, right? – blackeyeliner Sep 5 '15 at 9:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .