AppleScript: How to add m4a meta-data using Sound Studio? I'd like to automate the adding of metadata to various .m4a files using Sound Studio.  I have a Python script that will run thru all the files I care about and pass along useful data to an AppleScript that can actually manipulate the metadata.  But I'm not sure what the best way is to actually do the metadata manipulation in AppleScript.

I can get all the metadata into the AppleScript script by passing the items into the script via arguments, but I don't know how to open the file and set the metadata items I want to set.  Seems like it should be easy but all the attempts I've made so far have failed.  I'm sure I'm not understanding the correct way to approach the problem.  I have this so far:

on run argv
    -- Arguments:
    --  1   music_file_path   full path to the file to open
    --  2   artist               name of artist
    --   3   album_name    name of the album
    --    4   track_name         just the name of the track
    --    5   disc_number        which disc (1, 2 ...)
    --    6   track_number      which track on the disc
    --    7   release_year       the year when the track was released

    if (count of argv's items) ≠ 7 then
        display dialog "Wrong number of arguments!" with title "Add Meta Data To Sound Studio File" buttons {"Bummer"} giving up after 1
        return
    end if

    set music_file_path to item 1 of argv
    set artist_name to item 2 of argv
    set album_name to item 3 of argv
    set track_name to item 4 of argv
    set disc_number to item 5 of argv
    set track_number to item 6 of argv
    set release_year to item 7 of argv


    tell application "Finder"
        launch "/Applications/Sound Studio/Sound Studio.app"
    end tell -- Finder

    tell application "Sound Studio"
        activate
        set mfp to (a reference to POSIX file music_file_path)
        open mfp
        -- Set open'ed file's meta data to what we want
        save
        close
    end tell -- Sound Studio.app
end run


Any ideas on how to get this to work?
I downloaded a trial version of the latest Sound Studio and had a go.  The AppleScript dictionary was all that was required.  With the exception of the open command, all other information needed to compose the script came from the documentation within Script Editor.

The absence of an entry for the open command is a bit mysterious and I have emailed them about it.  I also did notice a "bug"(?) in the software in the manner in which it responds to open, although it technically can't be classified as a bug given that it's not documented as being a command that's supported.  However, the only other command that would be used for opening files and creating new documents would be the make command, but, having played around with that, I'm fairly confident that open is the correct command to use, and they just failed to document it properly, whilst also overlooking its occasionally erratic behaviour.

Anyway, here's the script, with my test file data taking the place of what will be sent through to the script from your Python script:

    # Script:   Add Metadata To Sound Studio File.applescript
    # Author:   CJK
    # Date:     2018-03-20
    #-------------------------------------------------------------------------------
    # @arguments:
    #       %argv%      an indexed list containing 7 items:
    #  
    #           1) full path to the file to open
    #           2) name of artist
    #           3) name of the album
    #           4) just the name of the track
    #           5) which disc (1, 2 ...)
    #           6) which track on the disc
    #           7) the year when the track was released
    #-------------------------------------------------------------------------------
    set argv to [¬
        "/Users/CK/Music/Nelly Furtado/Folklore/04 Try.m4a", ¬
        "Nelly Furtado", ¬
        "Folklore", ¬
        "Try", ¬
        "1", ¬
        "4", ¬
        "2003"] -- remove this variable declaration

    # on run argv -- uncomment this handler

        if (count argv) is not 7 then return display dialog ¬
            "Wrong number of arguments!" with title ¬
            "Add Metadata To Sound Studio File" buttons ¬
            {"Bummer"} giving up after 1


        tell application "Sound Studio"
            activate

            set music_file_path to the first item of argv

            set D to open (music_file_path as POSIX file)

            tell the metadata of D to set [¬
                artist, ¬
                album, ¬
                title, ¬
                disc number, ¬
                track number, ¬
                year] to the rest of argv

            save D
            close D

            --quit
        end tell

    # end run -- uncomment this line
    #----------------------------------------------------------------------------END


There's not a huge amount to say about the script: it opens Sound Studio, opens an audio file, assigns some values to the metadata properties, saves the file, then closes the file.  Optionally, you can quit the application at this point as well.

Regarding the opening of the audio file, Sound Studio seems content to be given a POSIX file reference rather than having to coerce it into alias class.  However, I did notice a couple of oddities:

❶・When I tried to define the variable music_file_path outside of the tell block, it behaved as you mentioned and simply didn't open the file, but also didn't return an error.  The script stalled until it timed out.

❷・It also didn't like me expressing the file reference as POSIX file music_file_path, so I had to write it as music_file_path as POSIX file.  Again, it would stall the script, rather than complaining that the file reference was wrong.

❸・If I corrected both of these issues and tried to run the script straight after a previous run had stalled it, the script would stall once again.  So I had to kill the Sound Studio process from Terminal by issuing a killall "Sound Studio".  Then when I ran the script, it worked quite happily, and continued to work happily with each successive run after run.  When I went to screw with the script again to purposely cause a stall, it wasn't surprising that I once again had to go back into Terminal to do a killall.

So I think it's fairly safe to say that this is an issue with Sound Studio and not with the script.  I've emailed them about it, but I think you wouldn't be wrong to email them as well: the more reports they receive from disgruntled customers who want AppleScripting technology to be properly implemented and supported, the more incentive they'll have to do so.

I did consider putting a clause in the AppleScript for it to issue its own shell command when it detects that the application had stalled trying to open a file.  The AppleScript could do a killall, but that didn't sit very well with me for some reason.  The Sound Studio application itself didn't stall, and continued to be operational during testing.

If you do want me to put some sort of block in the script to handle any instances where it might stall, let me know and I can add an edit.  But it wouldn't be what I would call "clean cut code", and as long as you don't try and change how and where the file reference is declared in the script, it should run fine.