Monday, February 22, 2021

Play my slides, please!

Did you ever need to take notes while you watch your slides play, but you don't want to switch back and forth between your Keynote and your text editor? Here you go... Save this to your Mac as main.swift - no other name will work, then run it in a terminal like so:

$ swift main.swift


import Cocoa

var pid = pid_t(-1)

let bid = "com.apple.iWork.Keynote"

for x in NSWorkspace.shared.runningApplications {
    if x.bundleIdentifier == bid {
        pid = x.processIdentifier
        break
    }
}

guard let keynote = NSRunningApplication(processIdentifier: pid) else {
    print("Can't find Keynote")
    exit(-1)
}

let src = CGEventSource(stateID: CGEventSourceStateID.hidSystemState)

let key: UInt16 = 0x79

guard let keydown = CGEvent(keyboardEventSource: src, virtualKey: key, keyDown: true) else {
    fatalError("Can't get keydown")
}

guard let keyup = CGEvent(keyboardEventSource: src, virtualKey: key, keyDown: false) else {
    fatalError("Can't get keydown")
}

while (true) {
    keydown.postToPid(pid)
    usleep(10_000)
    keyup.postToPid(pid)
    usleep(15_000_000)
}

No comments: