Basic keystroke script works in Script Editor, but not when launched

I made a script to launch and then play/pause Spotify:

tell application "System Events"
	tell application "Spotify" to activate
	delay 0.2
	key code 49 --spacebar
	delay 0.2
	set visible of process "Spotify" to false
	delay 0.2
end tell

When I run the script in Script Editor, it works fine. However, when I launch it (I use BetterTouchTools), it just launches Spotify, and then nothing happens.


Also, to avoid creating another thread…
I’m trying to do the same thing for skipping songs—to open Spotify, press the next button, and then hide the app—but this doesn’t work at all.

This is my script:

tell application "System Events"
	tell application "Spotify" to activate
	delay 0.3
	click at {689.48, 750.57}
	delay 0.3
	set visible of process "Spotify" to false
	delay 0.3
end tell

What am I doing incorrectly?

  1. Your first script replace with this:
tell application "Spotify" to playpause
  1. Your second script replace with this:
tell application "Spotify" to next track

Those are much better. Thanks. However, the scripts work fine in the Script Editor, but when I try to launch them (with BetterTouchTools), nothing happens.

I think there is a setting preventing scripts from working correctly.
(Even though I’ve added BetterTouchTool and Spotify to Accessibility in “Security & Privacy.”)

What do I need to make sure is set up for scripts to work?

KevS. I don’t use BTT and instead use a somewhat similar utility called Fastscripts. To get it to work with GUI scripting in Catalina, I did the following in Security & Privacy:

  • add Fastscripts to Accessibility
  • add Fastscripts to Full Disk Access
  • add System Events (which is found in /System/Library/CoreServices/) to Full Disk Access

I’m not sure if this will work but it’s easy to try and is easily undone. Perhaps someone who uses BTT will be able to better help.

I was able to launch the scripts using another app (they all work now). The first time I ran them, an OS window appeared asking to allow it to control System Events, and it was added to “Automation” in “Security & Privacy.”

There doesn’t seem to be any “+” button or any other way to add apps, so that I can add BetterTouchTool, though…

I don’t know why you are so stuck with GUI scripting solution. In my opinion, it would be better if you in Security & Privacy would grant your application permission to control not System Events, but Spotify.

I’ll try to re-organize the settings to make it work.

Thanks for help, KniazidisR.