/
arc.ts
226 lines (180 loc) · 5.85 KB
/
arc.ts
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Source: https://github.com/raycast/extensions/tree/94a85235fc605b1ad5eb75ff13bb3fad742b4b07/extensions/arc/
import { runAppleScript } from "run-applescript";
import { Space, Tab } from "./types.js";
export async function getTabs() {
const response = await runAppleScript(`
on escape_value(this_text)
set AppleScript's text item delimiters to the "\\""
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the "\\\\\\""
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
set _output to ""
tell application "Arc"
set _window_index to 1
set _tab_index to 1
repeat with _tab in tabs of first window
set _title to my escape_value(get title of _tab)
set _url to get URL of _tab
set _location to get location of _tab
set _output to (_output & "{ \\"title\\": \\"" & _title & "\\", \\"url\\": \\"" & _url & "\\", \\"windowId\\": " & _window_index & ", \\"tabId\\": " & _tab_index & " , \\"location\\": \\"" & _location & "\\" }")
if _tab_index < (count tabs of first window) then
set _output to (_output & ",\\n")
else
set _output to (_output & "\\n")
end if
set _tab_index to _tab_index + 1
end repeat
end tell
return "[\\n" & _output & "\\n]"
`);
return response ? (JSON.parse(response) as Tab[]) : undefined;
}
export async function findTab(url: string) {
const response = await runAppleScript(`
on escape_value(this_text)
set AppleScript's text item delimiters to the "\\""
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the "\\\\\\""
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
set _output to ""
tell application "Arc"
set _window_index to 1
repeat with _window in windows
set _tab_index to 1
repeat with _tab in tabs of _window
set _url to get URL of _tab
if _url is equal "${url}" then
set _title to my escape_value(get title of _tab)
set _location to get location of _tab
set _output to (_output & "{ \\"title\\": \\"" & _title & "\\", \\"url\\": \\"" & _url & "\\", \\"windowId\\": " & _window_index & ", \\"tabId\\": " & _tab_index & " , \\"location\\": \\"" & _location & "\\" }")
return _output
end if
set _tab_index to _tab_index + 1
end repeat
set _window_index to _window_index + 1
end repeat
end tell
return _output
`);
return response ? (JSON.parse(response) as Tab) : undefined;
}
export async function selectTab({ windowId, tabId }: Pick<Tab, "windowId" | "tabId">) {
await runAppleScript(`
tell application "Arc"
tell window (${windowId} as number)
tell tab (${tabId} as number) to select
end tell
activate
end tell
`);
}
export async function closeTab({ windowId, tabId }: Pick<Tab, "windowId" | "tabId">) {
await runAppleScript(`
tell application "Arc"
tell window (${windowId} as number)
tell tab (${tabId} as number) to close
end tell
end tell
`);
}
export async function reloadTab({ windowId, tabId }: Pick<Tab, "windowId" | "tabId">) {
await runAppleScript(`
tell application "Arc"
tell window (${windowId} as number)
tell tab (${tabId} as number) to reload
end tell
end tell
`);
}
export async function makeNewTab(url: string) {
await runAppleScript(`
tell application "Arc"
tell front window
make new tab with properties {URL:"${url}"}
end tell
activate
end tell
`);
}
export type MakeNewWindowOptions = {
incognito?: boolean;
url?: string;
};
export async function makeNewWindow(options: MakeNewWindowOptions = {}): Promise<void> {
await runAppleScript(`
tell application "Arc"
make new window with properties {incognito:${options.incognito ?? false}}
activate
${
options.url
? `tell front window to make new tab with properties {URL:"${options.url}"}`
: ""
}
end tell
`);
}
export async function makeNewLittleArcWindow(url: string) {
await runAppleScript(`
tell application "Arc"
make new tab with properties {URL:"${url}"}
activate
end tell
`);
}
export async function makeNewTabWithinSpace(url: string, space: Space) {
await runAppleScript(`
tell application "Arc"
tell front window
tell space ${space.id}
make new tab with properties {URL:"${url}"}
end tell
end tell
activate
end tell
`);
}
export async function selectSpace(id: Space["id"]) {
await runAppleScript(`
launch app "Arc"
delay "1"
tell application "Arc"
tell front window
tell space ${id} to focus
end tell
end tell
`);
}
export async function getSpaces() {
const response = await runAppleScript(`
set _output to ""
tell application "Arc"
set _space_index to 1
repeat with _space in spaces of front window
set _title to get title of _space
set _output to (_output & "{ \\"title\\": \\"" & _title & "\\", \\"id\\": " & _space_index & " }")
if _space_index < (count spaces of front window) then
set _output to (_output & ",\\n")
else
set _output to (_output & "\\n")
end if
set _space_index to _space_index + 1
end repeat
end tell
return "[\\n" & _output & "\\n]"
`);
return response ? (JSON.parse(response) as Space[]) : undefined;
}
export async function getVersion() {
const response = await runAppleScript(`
tell application "Arc"
return version
end tell
`);
return response;
}