Help with app automation permissions

Hi,

I am trying to make an app that uses Spotify's web API to play songs. For the web API to work, Spotify needs to be running, and my Mac has to be recognized as an active device. For my Mac to be recognized as an active device, I have to play a song for a very short amount of time (under a second).

I want to make my app automatically do that on launch. I already wrote the AppleScript in Automator, and it worked. It successfully launched Spotify, played a song for 0.5 seconds, then hid itself. After writing the code, I tried to implement it into my app to run on startup, but I ran into a problem. The app only started the Spotify app on my mac, and gave me an error that told me it wasn't running.

What do I do? Is this an issue with the permissions of the app, or something else? I have given the app the "Apple Events" entitlement.

This is the error I am getting. Note that the app opens Spotify, after which it gives me this.

Error: { NSAppleScriptErrorAppName = Spotify; NSAppleScriptErrorBriefMessage = "Application isn\U2019t running."; NSAppleScriptErrorMessage = "Spotify got an error: Application isn\U2019t running."; NSAppleScriptErrorNumber = "-600"; NSAppleScriptErrorRange = "NSRange: {31, 8}"; }

This is the function I am trying to use to do the actions with Spotify:

    func runAppleScript() {
        let appleScript = """
        tell application "Spotify"
            activate
            if player state is not playing then
                play track "spotify:track:5XSKC4d0y0DfcGbvDOiL93"
                delay 1
                pause
            end if
        end tell
        
        tell application "System Events"
            tell process "Spotify"
                set frontmost to true
                delay 1
                keystroke "h" using {command down}
            end tell
        end tell
        """
        
        var error: NSDictionary?
        if let scriptObject = NSAppleScript(source: appleScript) {
            scriptObject.executeAndReturnError(&error)
        }
        
        if let error = error {
            print("Error: \(error)")
        }
    }

Any help is appreciated. Thank you in advance.

Answered by Aivolook in 820794022

I got it fixed. Claude gave me a working answer. All I needed to do was add:

com.apple.security.temporary-exception.apple-events

With items

com.spotify.client

And

com.apple.systemevents

To the entitlements.

Accepted Answer

I got it fixed. Claude gave me a working answer. All I needed to do was add:

com.apple.security.temporary-exception.apple-events

With items

com.spotify.client

And

com.apple.systemevents

To the entitlements.

Help with app automation permissions
 
 
Q