forked from raycast/script-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquit-app.swift
executable file
·75 lines (58 loc) · 1.67 KB
/
quit-app.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/swift
// Required parameters:
// @raycast.schemaVersion 1
// @raycast.title Quit app
// @raycast.mode silent
// @raycast.packageName System
// Optional parameters:
// @raycast.icon 💥
// @raycast.argument1 { "type": "text", "placeholder": "Name or pid" }
// Documentation:
// @raycast.description Quits an app, by name or process id.
// @raycast.author Roland Leth
// @raycast.authorURL https://runtimesharks.com
import AppKit
let argument = Array(CommandLine.arguments.dropFirst()).first!
let runningApps = NSWorkspace.shared.runningApplications
if let processId = Int(argument) {
runningApps
.first { $0.processIdentifier == processId }
.map {
$0.terminate()
print("Quit \($0.localizedName ?? "\(processId)")")
exit(0)
}
print("No apps with process id \(processId)")
exit(1)
}
let apps = runningApps.filter {
$0.localizedName?.localizedCaseInsensitiveContains(argument) == true
}
if apps.isEmpty {
print("No apps with name \(argument)")
exit(1)
}
apps.first.map {
// If there's just one match, quit it and exit.
guard apps.count == 1 else { return }
$0.terminate()
print("Quit \($0.localizedName ?? argument)")
exit(0)
}
runningApps
.first { $0.localizedName?.localizedCaseInsensitiveCompare(argument) == .orderedSame }
.map {
// If an exact match exists, quit that and exit, even if there are several results.
$0.terminate()
print("Quit \($0.localizedName ?? argument)")
exit(0)
}
let names = apps
.compactMap { $0.localizedName }
.joined(separator: ", ")
if names.isEmpty {
// Just in case `localizedName` were all `nil`, which shouldn't really happen.
print("Multiple apps found")
} else {
print("Multiple apps found: \(names)")
}