Skip to content

Commit

Permalink
feat: defer accesibility check to execution of the command
Browse files Browse the repository at this point in the history
  • Loading branch information
socsieng committed Jan 3, 2021
1 parent 2028c02 commit 670d091
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
26 changes: 16 additions & 10 deletions Sources/SendKeysLib/SendKeysCli.swift
Expand Up @@ -9,36 +9,42 @@ public struct SendKeysCli: ParsableCommand {
version: "0.0.0", /* auto-updated */
subcommands: [MousePosition.self]
)

@Option(name: .shortAndLong, help: "Name of the application to send keys to.")
var applicationName: String?

@Option(name: .shortAndLong, help: "Default delay between keystrokes in seconds.")
var delay: Double = 0.1

@Option(name: .shortAndLong, help: "Initial delay before sending commands in seconds.")
var initialDelay: Double = 1

@Option(name: NameSpecification([.customShort("f"), .long ]), help: "File containing keystroke instructions.")
var inputFile: String?

@Option(name: .shortAndLong, help: "String of characters to send.")
var characters: String?

public init() { }

public mutating func run() throws {
let accessEnabled = AXIsProcessTrustedWithOptions([kAXTrustedCheckOptionPrompt.takeUnretainedValue() as String: true] as CFDictionary)

if !accessEnabled {
fputs("WARNING: Accessibility preferences must be enabled to use this tool. If running from the terminal, make sure that your terminal app has accessibility permissiions enabled.\n\n", stderr)
}

if !(applicationName ?? "").isEmpty {
try AppActivator(appName: applicationName!).activate()
}

if (initialDelay > 0) {
Sleeper.sleep(seconds: initialDelay)
}

let commandProcessor = CommandsProcessor(defaultPause: delay)
var commandString: String?

if !(inputFile ?? "").isEmpty {
if let data = FileManager.default.contents(atPath: inputFile!) {
commandString = String(data: data, encoding: .utf8)
Expand All @@ -48,12 +54,12 @@ public struct SendKeysCli: ParsableCommand {
} else if !(characters ?? "").isEmpty {
commandString = characters
}

if !(commandString ?? "").isEmpty {
commandProcessor.process(commandString!)
} else if !isTty() {
var data: Data

repeat {
data = FileHandle.standardInput.availableData

Expand Down
12 changes: 0 additions & 12 deletions Sources/sendkeys/main.swift
@@ -1,18 +1,6 @@
import SendKeysLib
import Foundation

private func isTty() -> Bool {
return isatty(FileHandle.standardInput.fileDescriptor) == 1
}

if isTty() {
let accessEnabled = AXIsProcessTrustedWithOptions([kAXTrustedCheckOptionPrompt.takeUnretainedValue() as String: true] as CFDictionary)

if !accessEnabled {
fputs("\nAccessibility preferences must be enabled to use this tool. If running from the terminal, make sure that your terminal app has accessibility permissiions enabled.\n\n", stderr)
}
}

if #available(OSX 10.11, *) {
SendKeysCli.main()
} else {
Expand Down

0 comments on commit 670d091

Please sign in to comment.