-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSaveIridiumTabsToMarkdown.scpt
161 lines (130 loc) · 4.32 KB
/
SaveIridiumTabsToMarkdown.scpt
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
/************************
Export Iridium tabs to a Markdown file
Version 1.0
May 12, 2020
Usage:
Run the script when Iridium is open.
Customization:
By default the new file is saved in "Documents".
Change the variable 'directory' to change the location.
Example `pathTo` variables: `desktop`, `documents folder`, `home folder`
Requirements:
- Iridium installed
- Destination directory should exist
Changelog:
1.00 by @tisgoud, first public version
************************/
// Declare the global variables
var title = ``
var text = ``
var directory = ``
var file = ``
// Get the Computer Name via the Standard Additions of the current app
currentApp = Application.currentApplication()
currentApp.includeStandardAdditions = true
// Get the variable values
title = shortDate(currentApp.currentDate(), true)+ `-iridium-tabs-` + currentApp.systemInfo().computerName
header = `Iridium tabs on ` + currentApp.systemInfo().computerName
directory = currentApp.pathTo(`documents folder`).toString()
file = `${directory}/${title}.md`
getBookmarks(header)
if (writeTextToFile(text, file, true)) {
currentApp.displayNotification(`All tabs have been saved to ${file}.`, {
withTitle: `IridiumTabsToMarkdown`})
}
function getBookmarks(headerLine) {
var window = {}, tab = {}
var numberOfWindows = 0, numberOfTabs = 0
var totalTabs = 0
var i,j
browser = Application('Iridium')
if (browser.running()) {
for (i = 0, numberOfWindows = browser.windows.length; i < numberOfWindows; i++) {
window = browser.windows[i]
for (j = 0, numberOfTabs = window.tabs.length; j < numberOfTabs; j++) {
tab = window.tabs[j]
// Convert title and URL to markdown, empty title is replaced by the URL
text += '\n- [' + (tab.title().length != 0 ? cleanString(tab.title()) : tab.url()) + ']' + '(' + tab.url() + ')'
}
totalTabs += numberOfTabs
// Add a line between the different windows
text += '\n\n---\n'
}
// Add a title and sub-titles with the date and number of windows and tabs.
text = '# ' + headerLine + '\n\n' + shortDate(currentApp.currentDate()) + ', ' + totalTabs + ' tabs in ' + numberOfWindows + ' windows\n' + text
}
else {
text = `no browser running`
}
}
function shortDate(date, yearFirst = false) {
var day = ``
var month = ``
var year = ``
day = date.getDate()
if (day < 10) {
day = `0` + day;
}
month = date.getMonth() + 1
if (month < 10) {
month = `0` + month;
}
year = date.getFullYear()
if (yearFirst) {
return year + `-` + month + `-` + day
}
else {
return day + `-` + month + `-` + year
}
}
function cleanString(input) {
var output = ``;
for (var i = 0; i < input.length; i++) {
if (input.charCodeAt(i) <= 127) {
output += input.charAt(i);
} else {
output += `&#` + input.charCodeAt(i) + `;`;
}
}
return output;
}
function writeTextToFile(text, file, overwriteExistingContent) {
try {
// Convert the file to a string
var fileString = file.toString()
// Open the file for writing
var openedFile = currentApp.openForAccess(Path(fileString), { writePermission: true })
// Clear the file if content should be overwritten
if (overwriteExistingContent) {
currentApp.setEof(openedFile, { to: 0 })
}
// Write the new content to the file
currentApp.write(text, { to: openedFile, startingAt: currentApp.getEof(openedFile) })
// Close the file
currentApp.closeAccess(openedFile)
// Send success notification
currentApp.displayNotification(`All tabs have been saved to ${file}.`, {
withTitle: `IridiumTabsToMarkdown`})
// Return a boolean indicating that writing was successful
return true
}
catch(error) {
try {
// Close the file
currentApp.closeAccess(file)
}
catch(error) {
// Report the error is closing failed
console.log(`Couldn't close file: ${error}`)
// Display an alert to the user with a probable cause
currentApp.displayAlert(`Error in IridiumTabsToMarkdown!`, {
message: `Please check for non-existing directories: \n${file}.`,
as: `critical`,
buttons: [`Oops`],
givingUpAfter: 10
})
}
// Return a boolean indicating that writing was successful
return false
}
}