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")
        let keyDownEvent = CGEvent(keyboardEventSource: eventSource, virtualKey: keyCode, keyDown: true)!
        keyDownEvent.flags = modifierFlags
        let keyUpEvent = CGEvent(keyboardEventSource: eventSource, virtualKey: keyCode, keyDown: false)!
        keyUpEvent.flags = modifierFlags

// 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
2 minutes ago, Tekl said:

but it only works when the Command Line Tools are installed.


Unless you compile it to a binary.


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

Link to comment
6 minutes ago, Tekl said:

it sends Cmd+Up to Roon

A simple workflow that allows volume control of Roon from Alfred without bringing the Roon app to the front would be of interest, I'm sure, to some of us. 😉



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!



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