-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSafari-save-tabs.sh
executable file
·42 lines (30 loc) · 1.15 KB
/
Safari-save-tabs.sh
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
#!/usr/bin/env osascript -l JavaScript
console.log("Safari save open tabs.");
// This script app for display debug things.
var current_app = Application.currentApplication()
current_app.includeStandardAdditions = true
// Save to dir.
const basePath = "~/Workspaces/Safari/Tabs"
const filePath = `${basePath}/$(date '+%Y-%m-%d-%H').txt`
// Traget app.
const Safari = Application("Safari")
Safari.includeStandardAdditions = true
const window_count = Safari.windows.length
var total_tab_count = 0
var fileContent = "";
for (var window_index = window_count - 1; window_index >= 0; window_index--) {
this_window = Safari.windows[window_index]
// Inc total tabs.
tab_count = this_window.tabs.length
total_tab_count += tab_count
for (var tab_index = 0; tab_index < tab_count; tab_index ++) {
tab = this_window.tabs[tab_index]
fileContent += (tab.url() + '\n')
// Also you can use tab.name()
}
}
console.log(`Found Safari tabs: ${total_tab_count}`)
console.log(`Save Safari tabs to dir ${basePath}`)
current_app.doShellScript(`mkdir -p ${basePath}`)
current_app.doShellScript(`echo ${JSON.stringify(fileContent)} > ${filePath}`)
result = total_tab_count