0

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
braxton
  • 16
  • 2
  • RE: "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." -- I do not have **Spotify**, so anything specific to it I cannot help you with. However, your `if` _statements_ are not doing anything and they are not even valid _statements_ in that they do not provide any mechanism to even determine if a _key press event_ occurred! Additionally, as written your **AppleScript** _code_ does not even compile and therefore cannot even be executed. – user3439894 Dec 29 '20 at 20:48
  • Does not **Spotify** already provide _keyboard shortcuts_ for these actions? – user3439894 Dec 29 '20 at 20:50
  • From a little googling, **Spotify** works with the _media keys_ on a **Mac**, so what are you actually trying to accomplish here that you can't with the _media keys_? – user3439894 Dec 29 '20 at 21:51
  • Thanks for letting me know, @user3439894! I'm fairly new to writing code and AppleScript, which is why the statements are invalid. What would I need to do to make them valid statements? I'm trying to use a keyboard shortcut to control Spotify as opposed to the media keys. Spotify must be in focus for that to happen, which is why I'm trying to create a script that prevents that. – braxton Dec 30 '20 at 21:12

1 Answers1

1

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).