SwiftRobot
Tell your Mac what to do, declaratively
What is SwiftRobot?
It is the declarative Layer for RobotKit. It provides several tasks of type RobotTask
that helps client define their required tasks in declarative way.
Robot Tasks
MouseRobotTask
: A Robot task for mouse capabilityMoveMouse
: Change mouse position to a custom or predefined pointsClickMouse
: Click the right & left mouse buttons- etc
KeyboardRobotTask
: A Robot task for keyboard capabilityTypeKey
: Type single RobotKeyboard keyPressKey
: Press a key down, until later executingReleaseKey
to release it- etc
ScreenRobotTask
: A Robot task for screen capabilityScreenshot
: Take screenshot of the display for a certainCGRect
- etc
NotificationRobotTask
: A Robot task for notification capabilitySendNotification
: Send notification to the user
Vision
: A Robot task for vision capabilityReadImageText
: Get the text in the provided image
SwiftClosureTask
: Execute a bunch of Swift language expressionsRobotTaskGroup
: Group one or more tasks
Basic Usage
Just import SwiftRobot
import SwiftRobot
Now ask SwiftRobot to run one or more task:
import SwiftRobot
try await SwiftRobot.run {
MoveMouse(to: .center)
TypeKey(.a)
// ...
}
You can also ask SwiftRobot execute tasks on the main queue
try await SwiftRobot.main {
TypeCapitalKey(.h)
TypeKey(.e)
// ...
}
Robot Task Examples
- Hello World Example
// Type Hello World
try await SwiftRobot.run {
TypeCapitalKey(.h)
TypeKey(.e)
TypeKey(.l)
TypeKey(.l)
TypeKey(.o)
TypeKey(.Space)
TypeCapitalKey(.W)
TypeKey(.o)
TypeKey(.r)
TypeKey(.l)
TypeKey(.d)
}
You can group these tasks using RobotTaskGroup
then as SwiftRobot to run the group:
var hwTaskGroup: RobotTaskGroup {
get async {
await RobotTaskGroup {
TypeCapitalKey(.h)
TypeKey(.e)
TypeKey(.l)
TypeKey(.l)
TypeKey(.o)
TypeKey(.Space)
TypeCapitalKey(.W)
TypeKey(.o)
TypeKey(.r)
TypeKey(.l)
TypeKey(.d)
}
}
}
try await SwiftRobot.run {
await hwTaskGroup
// ...
}