Reputation: 32
I'm creating an AppleScript for my Mac that will tell Spotify to skip or go back a track based on keyboard shortcuts. I believe I have the code correct but cannot get the script to work properly and think it may be due to the if-then statements' positioning.
Any tips on what I can do to make this work?
tell application "Spotify"
activate
if keystroke "e" using {command down, option down, control down} then
tell application "Spotify"
next track
end if
end tell
if keystroke "r" using {command down, option down, control down} then
tell application "Spotify"
previous track
end if
end tell
Upvotes: 0
Views: 1371
Reputation: 11
Detecting keypresses in AppleScript is a bit difficult in my experience, so I recommend you:
1)Write and test individual scripts for each shortcut press. That would be one script for:
tell application "Spotify"
next track
end tell
...and one for:
tell application "Spotify"
previous track
end tell
2)Open Automator and create a new Quick Action (you'll do this twice). Set it to Workflow receives "no input" in "any application". That will work when Spotify is not in focus. If you select Spotify, it will only work when Spotify is in focus.
Then on the left search the Actions for "Run AppleScript", then drag it to the right. Insert the code from step 1 as prompted. Save with a useful name like "Spotify Next Track".
3)Go to SystemPreferences->Keyboard->Shortcuts->Services and scroll down to the "General" dropdown, where you can assign a shortcut to each quick action (a modifier key is required, so "e" will not work, but cmd+e will).
Note that your in-focus application will tell Spotify to change tracks. I tested with Chrome in focus and it worked after I granted permission (only happens the first time).
Upvotes: 1