The performAction(action:optionsInput:) function is the primary entry point for executing user-like interactions (such as opening applications or typing text) using the MacosUseSDK. It takes a PrimaryAction and an ActionOptions configuration object.
When performing actions that involve UI changes (like typing), it is highly recommended to use the pidForTraversal option to ensure the SDK targets the correct application process for state comparison and visual feedback.
let openOptions = ActionOptions(
traverseBefore: true,
traverseAfter: true,
showDiff: false,
onlyVisibleElements: true,
showAnimation: false,
delayAfterAction: 0.0
)
let openResult = await performAction(action: PrimaryAction.open(identifier: "TextEdit"), optionsInput: openOptions)
// For subsequent actions like typing, use the PID from the previous result
let typeOptions = ActionOptions(
traverseBefore: true,
traverseAfter: true,
showDiff: true,
onlyVisibleElements: true,
showAnimation: true,
animationDuration: 0.8,
pidForTraversal: openResult.openResult?.pid,
delayAfterAction: 0.0
)
let typeResult = await performAction(action: PrimaryAction.input(action: .type(text: "Hello!")), optionsInput: typeOptions)