Skip to content

Instantly share code, notes, and snippets.

@devahmedshendy
Created September 18, 2022 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devahmedshendy/a1b1944a9072bea2409e4162963c791f to your computer and use it in GitHub Desktop.
Save devahmedshendy/a1b1944a9072bea2409e4162963c791f to your computer and use it in GitHub Desktop.

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 capability
    • MoveMouse: Change mouse position to a custom or predefined points
    • ClickMouse: Click the right & left mouse buttons
    • etc
  • KeyboardRobotTask: A Robot task for keyboard capability
    • TypeKey: Type single RobotKeyboard key
    • PressKey: Press a key down, until later executing ReleaseKey to release it
    • etc
  • ScreenRobotTask: A Robot task for screen capability
    • Screenshot: Take screenshot of the display for a certain CGRect
    • etc
  • NotificationRobotTask: A Robot task for notification capability
    • SendNotification: Send notification to the user
  • Vision: A Robot task for vision capability
    • ReadImageText: Get the text in the provided image
  • SwiftClosureTask: Execute a bunch of Swift language expressions
  • RobotTaskGroup: 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 Example

  • 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

  // ...
}
  • Sending Notification to use
// Request notification permission, and send one
try await SwiftRobot.run {
  RequestNotificationAuthorization(options: [.alert, .badge, .sound])                  
  SendNotification(title: "Title", message: "Message...")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment