-
Meet Shortcuts for macOS
Shortcuts is coming to macOS, and your apps are a key part of that process. Discover how you can elevate the capabilities of your app by exposing those features as Shortcuts actions. We'll show you how to build actions for your macOS apps built with Catalyst or AppKit, deploy actions across platforms, publish and share shortcuts, and enable your app to run shortcuts from other apps. We'll also take you through how Shortcuts fits in with existing Mac automation technologies like Automator and AppleScript.
Recursos
Vídeos relacionados
Tech Talks
WWDC21
-
Buscar neste vídeo...
-
-
17:10 - Adding Intent dispatch method in SwiftUI
import SwiftUI import Intents @main struct SouperTaskApp: App { @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate var body: some Scene { WindowGroup { ContentView() } } } class AppDelegate: NSObject, NSApplicationDelegate { func application(_ application: NSApplication, handlerFor intent: INIntent) -> Any? { } } -
18:32 - Resolve intent
class IntentHandler: NSObject, CreateTaskIntentHandling { func resolveTitle(for intent: CreateTaskIntent, with completion: @escaping (INStringResolutionResult) -> Void) { guard let title = intent.title, !title.isEmpty else { return completion(.needsValue()) } return completion(.success(with: title)) } func resolveDueDate(for intent: CreateTaskIntent, with completion: @escaping (CreateTaskDueDateResolutionResult) -> Void) { guard let dateComponents = intent.dueDate else { return completion(.needsValue()) } return completion(.success(with: dateComponents)) } ... } -
19:37 - Date range validation in dueDate resolve method
func resolveDueDate(for intent: CreateTaskIntent, with completion: @escaping (CreateTaskDueDateResolutionResult) -> Void) { guard let dateComponents = intent.dueDate, let dueDate = Calendar.current.date(from: dateComponents) else { return completion(.needsValue()) } if dueDate < Date() { return completion(.unsupported(forReason: .invalidDate)) } return completion(.success(with: dateComponents)) } -
20:40 - Handle intent
class IntentHandler: NSObject, CreateTaskIntentHandling { func handle(intent: CreateTaskIntent, completion: @escaping (CreateTaskIntentResponse) -> Void) { let title = intent.title! let dueDate = intent.dueDate! let task = createTask(name: title, due: dueDate) let response = CreateTaskIntentResponse(code: .success, userActivity: nil) response.task = task completion(response) } } -
25:39 - Running Shortcut from AppleScript
tell application "Shortcuts Events" run the shortcut whose name is "Make GIF" end tell -
25:49 - Using scripting bridge
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 {} guard let app: ShortcutsEvents = SBApplication(bundleIdentifier: "com.apple.shortcuts.events"), let shortcuts = app.shortcuts else { print("Couldn't access shortcuts") return } guard let shortcut = shortcuts.object(withName: "Make GIF") as? Shortcut else { print("Shortcut doesn't exist") return } _ = shortcut.run?(withInput: nil)
-