2

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'm leaving a comment instead of an answer because I can't test anything, given that I don't have Sound Studio. Firstly, get rid of the tell application "Finder" block. It's not doing anything useful. Launching Sound Studio is taken care of by the activate command in the next block. Secondly, is there any reason for using a reference to to utilise the file path ? If not, you can take it out. open commands in other applications usually require an alias class of file object. To make the file path into an alias object, try this: set mfp to POSIX file music_file_path as alias. ➦ – CJK Mar 17 '18 at 23:14
  • ➥ It's best to do this outside of any application tell block, although in this instance, it shouldn't really matter if you're not going to use mfp in any other application tell block. Regarding how to set the metadata, you should start by reading the Sound Studio AppleScript dictionary: to do this, when in Scipt Editor, press ⌘⇧L to bring up the Library window. In Finder, drag the Sound Studio.app app icon onto the Library window of Script Editor. Then double-click the "Sound Studio" entry that will be added to the list of available dictionaries. – CJK Mar 17 '18 at 23:32
  • I am familiar with the Sound Studio AppleScript dictionary. What I don't comprehend is how to get a window in Sound Studio actually to OPEN. I tried the suggestion of set map to POSIX file music_file_path as alias, followed by open mfp, but no window actually opens. If someone could provide some sample code as to how one would actually open a document that would open a window, and how one would set any piece of the metadata, then I would imagine save ("yes" as the option to force a save without forcing an Ask), and close mfp, if would be much appreciated. – Bill G Mar 20 '18 at 7:31
0

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.

  • This does the trick! The tell the metadata of D to set [] to [] was what I didn’t know how to do, hence the reason I didn’t fill in specific code there. Would have never guessed that tell … was the way to accomplish this. AppleScript is the COBOL of our times. There does seem to be some quirkiness WRT Sound Studio, in terms of the lack of documentation for open and the inexplicable stalling behavior. One might also expect the default open permissions to be read-only, not read/write. I’ll email them as well about these quirky things. Thanks again for the help! – Bill G Mar 20 '18 at 20:35
  • Well ... ALMOST does the trick. Debugging the script a bit more (Late Night SW's Script Debugger [latenightsw.com] (which is awesome) seems to indicate that the metadata for 'disc number', 'disc total', 'track number' and 'track total' is having trouble being retrieved (error -10000), a Sound Studio app problem. I'm guessing any subsequent attempt to set those values is (silently) failing. My subsequent attempt to close and save the file is also (stubbornly, silently) failing as well. I have an email into the Sound Studio folks to try and figure this out. – Bill G Mar 22 '18 at 19:34

You must log in to answer this question.

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