forked from mandrigin/AlfredSwitchWindows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.swift
58 lines (44 loc) · 1.91 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
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"] {
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)
}
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.")
exit(1)
}
}