This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tabs.js
155 lines (135 loc) · 3.08 KB
/
tabs.js
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
152
153
154
155
'use strict'
// define constants
const SUPPORTED_BROWSERS = [
'Safari',
'Safari Technology Preview',
'Google Chrome',
'Google Chrome Canary',
'Chromium',
'Vivaldi',
'Yandex',
]
const SAFARI = [
'Safari',
'Safari Technology Preview',
]
const CHROMIUM = [
'Google Chrome',
'Google Chrome Canary',
'Chromium',
'Vivaldi',
'Yandex',
]
const prefix = '/Applications/'
const getAppPath = appName => {
return prefix + appName + '.app'
}
const getIconName = name => {
if (name === 'Safari')
return 'compass.icns'
else if (name === 'Safari Technology Preview')
return 'technology-preview-compass.icns'
else
return 'app.icns'
}
const hasApp = name => {
try {
if (Application(getAppPath(name)).running())
return true
} catch (err) {
return false
}
}
const hasTabs = window => {
try {
window.tabs()
return true
} catch (err) {
return false
}
}
const getAppWindows = app => app.windows().
filter(window => window.name() && hasTabs(window)).
map(window => ({
app,
window,
}))
const isTabValid = tab => {
try {
return tab.url().length > 0
} catch (err) {
return false
}
}
const getAppWindowTabs = item => item.window.tabs().
filter(isTabValid).
map((tab, tabIndex) => ({
app: item.app,
window: item.window,
tab,
tabIndex: tabIndex + 1,
}))
const getTabId = tab => {
try {
return tab.id()
} catch (err) {
return null
}
}
const getAppWindowTabData = item => ({
title: item.tab.name(),
subtitle: item.tab.url(),
arg: encodeURI(JSON.stringify({
appName: item.app.name(),
windowId: item.window.id(),
tabId: getTabId(item.tab),
tabIndex: item.tabIndex,
})),
icon: {
path: '/Applications/' + item.app.name() + '.app' +
'/Contents/Resources/' + getIconName(item.app.name()),
},
})
const matches = (item, query) => {
return item.title.toLowerCase().normalize().includes(query) ||
item.subtitle.toLowerCase().normalize().includes(query)
}
function run (argv) {
const cmd = argv[0]
const arg = argv[1]
if ('query' === cmd) {
const query = arg.toLowerCase().normalize()
const tabs = SUPPORTED_BROWSERS.filter(hasApp).
map(name => Application(getAppPath(name))).
map(getAppWindows).
reduce((a, b) => a.concat(b), []).
map(getAppWindowTabs).
reduce((a, b) => a.concat(b), []).
map(getAppWindowTabData).
filter(item => matches(item, query))
return JSON.stringify(tabs)
}
else if ('activate' === cmd || 'close' === cmd) {
const item = JSON.parse(decodeURI(arg))
const app = Application(getAppPath(item.appName))
const window = app.windows().
filter(window => window.id() === item.windowId)[0]
const tab = window.tabs().
filter(tab => getTabId(tab) ? tab.id() === item.tabId : tab.index() ===
item.tabIndex)[0]
console.log('activating', item.appName, item.tabId)
if (SAFARI.find(t => { return t.includes(item.appName.substr(0, 4)) })) {
window.currentTab = tab
}
else if (CHROMIUM.find(
t => { return t.includes(item.appName.substr(0, 4)) })) {
window.activeTabIndex = item.tabIndex
} else {
app.activate()
return undefined
}
app.activate()
if ('close' === cmd) app.close(tab)
return 0
}
}