applescriptapple-music

Cannot set track details in macOS Music with AppleScript


I record music at home and regularly need to add tracks created with GarageBand to Music. This requires me to manually set the artist, album, genre, year, & artwork so I'm trying to write an AppleScript to automate the process. Using add to add the track works fine but when I try to set any of the details I get a -54 permissions error. I've tried using set within the tell application "Music" block and as a nested tell on the track itself, and exporting the script as an app and giving it permissions for Music in system settings, but I get the -54 every time. Can anyone help?

macOS 15.6.1


Solution

  • You’re hitting error -54 because Music hasn’t finished importing the file when you try to set metadata. After using add, wait a moment and then target the library track (not the temporary file track)

    Adding a delay (or retry loop) after import usually resolves the -54 error.

    tell application "Music"
        set addedTracks to add POSIX file "/path/to/song.m4a"
        set dbid to database ID of (item 1 of addedTracks)
        delay 3
        set libTrack to some track of library playlist 1 whose database ID is dbid
        tell libTrack
            set artist to "My Artist"
            set album to "My Album"
            set genre to "My Genre"
            set year to 2024
        end tell
    end tell