forked from mandrigin/AlfredSwitchWindows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BrowserApplication.swift
151 lines (116 loc) · 4 KB
/
BrowserApplication.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import Foundation
import AppKit
import ScriptingBridge
protocol BrowserEntity {
var rawItem : AnyObject { get }
}
protocol BrowserNamedEntity : BrowserEntity {
var title : String { get }
}
extension BrowserEntity {
func performSelectorByName<T>(name : String, defaultValue : T) -> T {
let sel = Selector(name)
let selectorResult = self.rawItem.perform(sel)
guard let retainedValue = selectorResult?.takeRetainedValue() else {
return defaultValue
}
guard let result = retainedValue as? T else {
return defaultValue
}
return result
}
}
extension BrowserNamedEntity {
var title : String {
/* Safari uses 'name' as the tab title, while most of the browsers have 'title' there */
if self.rawItem.responds(to: #selector(getter: MTLFunction.name)) {
return performSelectorByName(name: "name", defaultValue: "")
}
return performSelectorByName(name: "title", defaultValue: "")
}
}
class BrowserTab : BrowserNamedEntity, Searchable, ProcessNameProtocol {
private let tabRaw : AnyObject
private let index : Int?
let windowTitle : String
let processName : String
init(raw: AnyObject, index: Int?, windowTitle: String, processName: String) {
tabRaw = raw
self.index = index
self.windowTitle = windowTitle
self.processName = processName
}
var rawItem: AnyObject {
return self.tabRaw
}
var url : String {
return performSelectorByName(name: "URL", defaultValue: "")
}
var tabIndex : Int {
guard let i = index else {
return 0
}
return i
}
var searchStrings : [String] {
return ["Browser", self.url, self.title]
}
/*
(lldb) po raw.perform("URL").takeRetainedValue()
https://encrypted.google.com/search?hl=en&q=objc%20mac%20list%20Browser%20tabs#hl=en&q=swift+call+metho+by+name
(lldb) po raw.perform("name").takeRetainedValue()
scriptingbridge Browsertab - Google Search
*/
}
class BrowserWindow : BrowserNamedEntity {
private let windowRaw : AnyObject
let processName : String
init(raw: AnyObject, processName: String) {
windowRaw = raw
self.processName = processName
}
var rawItem: AnyObject {
return self.windowRaw
}
var tabs : [BrowserTab] {
let result = performSelectorByName(name: "tabs", defaultValue: [AnyObject]())
return result.enumerated().map { (index, element) in
return BrowserTab(raw: element, index: index + 1, windowTitle: self.title, processName: self.processName)
}
}
}
class BrowserApplication : BrowserEntity {
private let app : SBApplication
private let processName : String
static func connect(processName: String) -> BrowserApplication? {
let ws = NSWorkspace.shared()
guard let fullPath = ws.fullPath(forApplication: processName) else {
return nil
}
let bundle = Bundle(path: fullPath)
guard let bundleId = bundle?.bundleIdentifier else {
return nil
}
let runningBrowsers = ws.runningApplications.filter { $0.bundleIdentifier == bundleId }
guard runningBrowsers.count > 0 else {
return nil
}
guard let app = SBApplication(bundleIdentifier: bundleId) else {
return nil
}
return BrowserApplication(app: app, processName: processName)
}
init(app: SBApplication, processName: String) {
self.app = app
self.processName = processName
}
var rawItem: AnyObject {
return app
}
var windows : [BrowserWindow] {
let result = performSelectorByName(name: "windows", defaultValue: [AnyObject]())
return result.map {
return BrowserWindow(raw: $0, processName: self.processName)
}
}
}