Send keyboard command to background application - while keeping it in the background?

Forgive the noobishness - my first time with KM. This is what I'm trying to accomplish: I have a Bluetooth footswitch. I want the footswitch to trigger a specific command in Apple Music (in this case, the Rewind command (Opt-Cmd-Left Arrow) while music is playing, but without bring Music to the foreground. I'm working on doing bass guitar notation in OneNote. I want OneNote to remain in the foreground, while the command is sent to Music in the background. I've successfully set up the switch to trigger the rewind command, but it also brings Music to the foreground. I'm trying to avoid having to toggle back to OneNote manually every time I hit the rewind switch (the entire point of the footswitch and KM). I've tried adding a third action (Apply Modifier to Apple Music > Type the Option-Command-Left Arrow Keystroke > Show Microsoft OneNote), but that doesn't work. Music remains in the foreground. What am I doing wrong? Any help appreciated.

You can try the following "Type A Keystroke" action with send To "Music" app

1 Like

Unfortunately this didn't work. It works when Music is the foreground application, but if OneNote is in the foreground the footswitch simply sends an arrow-up command to OneNote. It doesn't send the designated keystroke to Music in the background. :slightly_frowning_face:

1 Like

Hi @RedHotFuzz, instead of using the Type Keystroke Action try this Execute AppleScript Action.

Just tested and this works for me (even when Music App is in the Background):

EXAMPLE Music App Rewind.kmmacros (26.2 KB)

tell application "Music"
  rewind
end tell
1 Like

"I've successfully set up the switch to trigger the rewind command, but it also brings Music to the foreground."

How did you setup the switch to trigger that command ? That switch can be configured to accept command line program , or open url through some kind of Config screen ?

Awesome, this works, but...the "rewind" command puts the song into a continuous backward scrub. I want it just to rewind 5 seconds, which is what Opt-Cmd-Left Arrow does when Music is in the foreground. Is this possible using Apple Script?

I can add an AppleScript command (resume) to another of the switches to stop the rewind process and begin playing from that spot, but a set 5-second rewind with a single click would be ideal if possible.

The switch I'm using is an AirTurn BT200S-4, which has 4 switches (there are also 2-switch and 6-switch models). The switch connects via Bluetooth, using the free AirTurn Manager software they provide. Once connected, I simply chose This Device Key in the triggers pulldown and clicked the switch I want to use, causing the switch name to appear in the field. In this case the switch is BT200S-4 v2.3.1 1377 Up Arrow, which is the leftmost of the 4 switches on this model.

Ah yes. The "rewind" AppleScript seems to send the track all the way back to beginning. I will try and see if I can help you get it to just rewind 5 seconds.

Okay @RedHotFuzz, try this. It works for me, to just rewind the track 5 seconds:

EXAMPLE Music App Rewind v2.kmmacros (26.2 KB)

tell application "Music" to set player position to (player position - 5)
1 Like

As a general answer to the question (Better specific answers have been provided with AppleScript) you can add a command to return to the previous app as the last step.

Activate Last App

So you can duck into another app, do something, and return to the app from whence you came.

3 Likes

BRILLIANT, THANK YOU!!!

For future reference for those who may find this thread and are using the same hardware (AirTurn BT200 footswitch), here's what the full working solution looks like:

Thank you so much for everyone that helped out, especially Zabobon for the AppleScript solution.

1 Like

Glad to help. Keyboard Maestro is an extremely versatile App. There is nearly always more than one way of solving a problem with it. The simplest solution is usually the best - using native Keyboard Maestro Actions. (And of those Actions "Select or Show a Menu Item" is so often the one.)

But the fact that you can "bolt in" snippets of AppleScript (as well as other programming languages) makes it super-powerful and fun to use.

For testing and tweaking AppleScript, I love Script Debugger. It allows you to play with the AppleScript to get it right as well as listing all the AppleScript Commands that each Mac App has.