DEV Community

Nakaoka Rei
Nakaoka Rei

Posted on

3

SwiftAutoGUI: Library for manipulating macOS with Swift

Introduction

I developed library for manipulating macOS with Swift. In this article, I will explain usage.

If you think it's good, star me!

GitHub logo NakaokaRei / SwiftAutoGUI

Used to programmatically control the mouse & keyboard.

SwiftAutoGUI

SPM is supported Github issues Github forks Github stars Github top language Github license

Used to programmatically control the mouse & keyboard A library for manipulating macOS with Swift.

This repository is implemented with reference to pyautogui.

Installation

SwiftAutoGUI is available through Swift Package Manager.

in Package.swift add the following:

dependencies: [
    // Dependencies declare other packages that this package depends on.
    .package(url: "https://github.com/NakaokaRei/SwiftAutoGUI", branch: "master")
],
targets: [
    .target(
        name: "MyProject",
        dependencies: [..., "SwiftAutoGUI"]
    )
    ...
]
Enter fullscreen mode Exit fullscreen mode

Example Usage

Keyboard and Mouse Control

See Keycode.swift for supported keys. If it is not a US keyboard, This may not work properly.

import SwiftAutoGUI
// Send ctrl + ←
SwiftAutoGUI.sendKeyShortcut([.control, .leftArrow])

// Send sound up
SwiftAutoGUI.keyDown(.soundUp)
SwiftAutoGUI.keyUp(.soundUp)

// Move mouse by dx, dy
Enter fullscreen mode Exit fullscreen mode

Installation

SwiftAutoGUI is available through Swift Package Manager. 

in Package.swift add the following:

dependencies: [
    // Dependencies declare other packages that this package depends on.
    .package(url: "https://github.com/NakaokaRei/SwiftAutoGUI", branch: "master")
],
targets: [
    .target(
        name: "MyProject",
        dependencies: [..., "SwiftAutoGUI"]
    )
    ...
]
Enter fullscreen mode Exit fullscreen mode

Example Usage

Keyboard

By calling a method of the SwiftAutoGUI class as shown below, you can send key input commands to macOS. Supported keys are written in Keycode.swift.

As shown in the sample below, you can also input shortcuts, such as moving the virtual desktop by sending the command ctrl + ←.

import SwiftAutoGUI

// Send ctrl + ←
SwiftAutoGUI.sendKeyShortcut([.control, .leftArrow])

// Send sound up
SwiftAutoGUI.keyDown(.soundUp)
SwiftAutoGUI.keyUp(.soundUp)
Enter fullscreen mode Exit fullscreen mode

Mouse

Similarly, mouse operations can generate basic commands such as mouse movement, clicking, and scrolling by invoking methods of the SwiftAutoGUI class.

import SwiftAutoGUI

// Move mouse by dx, dy from the current location
SwiftAutoGUI.moveMouse(dx: 10, dy: 10)

// Click where the mouse is located
SwiftAutoGUI.leftClick()

// Scroll
SwiftAutoGUI.vscroll(clicks: 10) // up
SwiftAutoGUI.vscroll(clicks: -10) // down
SwiftAutoGUI.hscroll(clicks: 10) // left
SwiftAutoGUI.hscroll(clicks: -10) // right
Enter fullscreen mode Exit fullscreen mode

Conclusion

In the future, we would like to add JIS keyboard support, support for other operating systems, and events.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more