Shortcut is failing to run via Scripting Bridge

I've taken the code from the WWDC video and are trying to run a shortcut via ScriptingBridge:

import ScriptingBridge

@objc protocol ShortcutsEvents {
  @objc optional var shortcuts: SBElementArray { get }
}

@objc protocol Shortcut {
  @objc optional var name: String { get }
  @objc optional func run(withInput: Any?) -> Any?
}

extension SBApplication: ShortcutsEvents {}
extension SBObject: Shortcut {}

and

guard
    let app: ShortcutsEvents = SBApplication(bundleIdentifier: "com.apple.shortcuts.events"),
    let shortcuts =  app.shortcuts else { throw "error" }

print("getting short")
guard let shortcut = shortcuts.object(withName: shortcutName) as? Shortcut else { throw "error" }

print("running short", shortcut)
let res = shortcut.run?(withInput: input)

print("shotcut result", res ?? "nil")

While I am getting the shortcut object just fine

running short <SBObject @0x6000035c8e40: <class 'srct'> "append" of application "Shortcuts Events" (16450)>

running it, just does nothing and returns nil

shotcut result nil

Running the shortcut via cli works as expected.

I (hopefully) have set the entitlements correctly

but I am feeling a little stumped not to even get an error at all.

What am I doing wrong there?

The key in the entitlements dictionary should match the bundle ID of the Shortcuts Events app that's bundled with macOS. Can you try using com.apple.shortcuts.events instead of com.apple.shortcuts-events?

Are there any updates on this? I am experiencing exactly the same issue.

Got it, for it to work you need to:

  1. Under Hardened Runtime capabilities, enable Apple Events.
  2. Add a new NSAppleEventsUsageDescription in the plist file.
  3. There seems to be a typo in the WWDC video, the entitlement should be: com.apple.shortcuts.events

Enjoy!

@jbenavidesv good findings - but for me it still hasn't changed things.

Are you sure there isn't something more you did? Let me run quickly though my current setup:

Info.plist:

Entitlements:

And runtime:

     guard
      let app: ShortcutsEvents = SBApplication(bundleIdentifier: "com.apple.shortcuts"),
      let shortcuts = app.shortcuts else { throw "error" }

    print("getting short")
    guard let shortcut = shortcuts.object(withName: name) as? Shortcut else { throw "error" }

    print("running short", shortcut)
    let res = shortcut.run?(withInput: input)

    print("shotcut result", res ?? "nil")

This is what worked for me:

In Entitlements:

And NSAppleEventsUsageDescription in Info.plist

Entitlements from WWDC videos:

  1. dont work for me
  2. are rejected when trying to upload to the App Store. Something is wrong with it
Shortcut is failing to run via Scripting Bridge
 
 
Q