Jump to content

Can Alfred send keystrokes to applications?


Recommended Posts

17 hours ago, Toontje said:

I want to send keystrokes to OBS Studio to switch scenes. Is this possible?

 

Have a look at whether it can be scripted instead, which would be a more reliable way to control it. I have a vague recollection that it can be, from looking into it a few years ago. :)

Link to comment
  • 1 year later...

I'm also looking for a way to send keys to processes like BetterTouchTool can do. Using System Events in AppleScript does need the process to be frontmost. Maybe someone knows how to use AppleScriptObjC to solve this? Maybe something using CGEventCreateKeyboardEvent.

Link to comment

Well, this swift script seems to work (it sends Cmd+Up to Roon), but it only works when the Command Line Tools are installed.

 

#!/usr/bin/env swift
import Cocoa

func sendShortcutToProcess(bundleIdentifier: String, keyCode: CGKeyCode, modifierFlags: CGEventFlags) {
    if let targetProcess = NSRunningApplication.runningApplications(withBundleIdentifier: bundleIdentifier).first {
        let pid = targetProcess.processIdentifier
        guard let eventSource = CGEventSource(stateID: .hidSystemState) else {
            print("Failed to create event source")
            return
        }
        
        let keyDownEvent = CGEvent(keyboardEventSource: eventSource, virtualKey: keyCode, keyDown: true)!
        keyDownEvent.flags = modifierFlags
        keyDownEvent.postToPid(pid)
        
        let keyUpEvent = CGEvent(keyboardEventSource: eventSource, virtualKey: keyCode, keyDown: false)!
        keyUpEvent.flags = modifierFlags
        keyUpEvent.postToPid(pid)
    }
}

// Usage: Call the function with the desired bundle identifier, key code, and modifier flags
sendShortcutToProcess(bundleIdentifier: "com.roon.Roon", keyCode: 126, modifierFlags: .maskCommand)

 

Link to comment
54 minutes ago, vitor said:

 

Unless you compile it to a binary.

 

Having the Command Line Tools isn’t an issue, though.

Ah, good idea. But then changing scripts is more complicated and compiling Is not enough. I have to codesign and notarize it. And I don't know how to compile universal binaries. Sorry, I'm no developer, I don't want it complicated. 😁 It seems that a corresponding makefile will be bigger than the simple swift script. 

Link to comment

Many thanks for that. I'll probably not now have the chance to try it until tomorrow morning (UK time) but will definitely have a look at it. It was something I was thinking about only the other day but I'm much less of a programmer than you clearly are!

 

Stephen

Link to comment
  • 2 months later...

Welcome the forum.

 

I use that workflow daily and it works perfectly for me so we have to try to discover why it's not working for you.

 

To do that:

  • make sure Roon is running;
  • turn on the debugger;
  • run the workflow and try to change the Roon volume.

Copy and paste the dbugger output into a new post in this thread so that we can take a look at it.

 

It might be helpful at the same time to confirm your macOS, Alfred and Roon version numbers.

 

Stephen

Link to comment

So I tried editing the workflow to add a Play/Pause Roon. 

 

My thinking was that keycode 49 would send the spacebar to Roon.

 

I copied the workflow block, changed the keycode sent to 49 and updated the on-screen message.

 

When I run it, I get the on-screen message so it's triggering, but nothing happens with Roon.

 

Any ideas?

 

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...