Skip to content

Commit

Permalink
feat: display a list applications that can be used with sendkeys
Browse files Browse the repository at this point in the history
Fixes #46
  • Loading branch information
socsieng committed Aug 21, 2021
1 parent 4dd27ea commit 94626fa
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
12 changes: 12 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "swift build"
}
]
}
59 changes: 59 additions & 0 deletions Sources/SendKeysLib/AppLister.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import ArgumentParser
import Cocoa
import Foundation

class AppLister: ParsableCommand {
public static let configuration = CommandConfiguration(
commandName: "apps",
abstract:
"Lists apps that can be used with the send command."
)

struct AppInfo: Hashable {
let name: String?
let id: String?

init(name: String?, id: String?) {
self.name = name
self.id = id
}

static func == (lhs: AppInfo, rhs: AppInfo) -> Bool {
return lhs.name == rhs.name && lhs.id == rhs.id
}

func hash(into hasher: inout Hasher) {
hasher.combine(self.name)
hasher.combine(self.id)
}
}

required init() {
}

func run() {
let apps = Set(
NSWorkspace.shared.runningApplications.filter { app in
return app.activationPolicy == .regular
}
.map { app in
return AppInfo(name: app.localizedName, id: app.bundleIdentifier)
}
)
.sorted { a, b in
return a.name?.lowercased() ?? "" < b.name?.lowercased() ?? ""
}

let maxLength = apps.reduce(
0,
{ max, info in
return info.name?.count ?? 0 > max ? info.name!.count : max
})

apps.forEach { info in
print(
"\((info.name ?? "-").padding(toLength: maxLength + 4, withPad: " ", startingAt: 0))id:\(info.id ?? "-")"
)
}
}
}
2 changes: 1 addition & 1 deletion Sources/SendKeysLib/SendKeysCli.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public struct SendKeysCli: ParsableCommand {
commandName: "sendkeys",
abstract: "Command line tool for automating keystrokes and mouse events.",
version: "0.0.0", /* auto-updated */
subcommands: [Sender.self, MousePosition.self, Transformer.self],
subcommands: [Sender.self, AppLister.self, MousePosition.self, Transformer.self],
defaultSubcommand: Sender.self
)

Expand Down

0 comments on commit 94626fa

Please sign in to comment.