-
Notifications
You must be signed in to change notification settings - Fork 31
/
main.swift
104 lines (82 loc) · 3.53 KB
/
main.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import Foundation
/// Removes browser window from the list of windows and adds tabs to the results array
func searchBrowserTabsIfNeeded(processName: String,
windows: [WindowInfoDict],
query: String,
results: inout [[AlfredItem]]) -> [WindowInfoDict] {
let activeWindowsExceptBrowser = windows.filter { ($0.processName != processName) }
let browserTabs =
BrowserApplication.connect(processName: processName)?.windows
.flatMap { return $0.tabs }
.search(query: query)
results.append(browserTabs ?? [])
return activeWindowsExceptBrowser
}
func search(query: String, onlyTabs: Bool) {
var results : [[AlfredItem]] = []
var allActiveWindows : [WindowInfoDict] = Windows.all
for browserName in ["Safari", "Safari Technology Preview",
"Google Chrome", "Google Chrome Canary",
"Opera", "Opera Beta", "Opera Developer",
"Brave Browser", "iTerm"] {
allActiveWindows = searchBrowserTabsIfNeeded(processName: browserName,
windows: allActiveWindows,
query: query,
results: &results) // inout!
}
if !onlyTabs {
results.append(allActiveWindows.search(query: query))
}
let alfredItems : [AlfredItem] = results.flatMap { $0 }
print(AlfredDocument(withItems: alfredItems).xml.xmlString)
}
func handleCatalinaScreenRecordingPermission() {
guard let firstWindow = Windows.any else {
return
}
guard !firstWindow.hasName else {
return
}
let windowImage = CGWindowListCreateImage(.null, .optionIncludingWindow,
firstWindow.number,
[.boundsIgnoreFraming, .bestResolution])
if windowImage == nil {
debugPrint("Before using this app, you need to give permission in System Preferences > Security & Privacy > Privacy > Screen Recording.\nPlease authorize and re-launch.")
exit(1)
}
}
handleCatalinaScreenRecordingPermission()
/*
a naive perf test, decided to keep it here for convenience
let start = DispatchTime.now() // <<<<<<<<<< Start time
for _ in 0...100 {
search(query: "pull", onlyTabs: false)
}
let end = DispatchTime.now() // <<<<<<<<<< end time
let nanoTime = end.uptimeNanoseconds - start.uptimeNanoseconds // <<<<< Difference in nano seconds (UInt64)
let timeInterval = Double(nanoTime) / 1_000_000_000 // Technically could overflow for long running tests
print("TIME SPENT: \(timeInterval)")
*/
if(CommandLine.commands().isEmpty) {
print("Unknown command!")
print("Commands:")
print("--search=<query> to search for active windows/Safari tabs.")
print("--search-tabs=<query> to search for active browser tabs.")
exit(1)
}
for command in CommandLine.commands() {
switch command {
case let searchCommand as SearchCommand:
search(query: searchCommand.query, onlyTabs: false)
exit(0)
case let searchCommand as OnlyTabsCommand:
search(query: searchCommand.query, onlyTabs: true)
exit(0)
default:
print("Unknown command!")
print("Commands:")
print("--search=<query> to search for active windows/Safari tabs.")
print("--search-tabs=<query> to search for active browser tabs.")
exit(1)
}
}