-
Notifications
You must be signed in to change notification settings - Fork 1
/
exporter.js
117 lines (94 loc) · 3.54 KB
/
exporter.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
// set up the Notes app
var notesApp = Application('Notes');
notesApp.includeStandardAdditions = true;
// set up the current app
var currentApp = Application.currentApplication();
currentApp.includeStandardAdditions = true;
// ask for notes account
var notesAccount = currentApp.chooseFromList(['iCloud', 'On My Mac'], {
withPrompt: "Choose Notes Account",
defaultItems: ['iCloud'],
});
if (notesAccount.length <= 0) displayError("Notes Account not chosen");
var allNotesInAccount = notesApp.accounts.byName(notesAccount).notes;
if (allNotesInAccount.length <= 0) displayError("Notes Account not found");
// ask for notes from the chosen account
var selectedNotes = currentApp.chooseFromList(allNotesInAccount.name(), {
withPrompt: "Select Notes",
multipleSelectionsAllowed: true,
});
if (selectedNotes.length <= 0) displayError("No note selected");
// ask for file format to save notes in
var outputFormat = currentApp.chooseFromList(['Text (.txt) file', 'Hypertext (.html) file'], {
withPrompt: "Choose output format",
defaultItems: ['Hypertext (.html) file'],
}).toString();
if (outputFormat.length <= 0) displayError("No output format");
if (outputFormat === "Text (.txt) file") {
outputFileFormat = 'plaintext';
outputFileSuffix = '.txt';
} else if (outputFormat === "Hypertext (.html) file") {
outputFileFormat = 'body';
outputFileSuffix = '.html';
}
// ask for folder to save notes in
var savePath = currentApp.chooseFolder({
withPrompt: "Choose output location",
}).toString();
if (savePath.length <= 0) displayError("No output location specified");
// set up progress
Progress.totalUnitCount = selectedNotes.length;
Progress.completedUnitCount = 0;
Progress.description = "Exporting Notes...";
Progress.additionalDescription = `Exporting notes into ${outputFormat}s.`;
// loop through all notes in the chosen account
for (var i = 0; i < allNotesInAccount.length; i++) {
// check against selected notes
if (selectedNotes.includes(allNotesInAccount[i].name())) {
// display progress
Progress.additionalDescription = `Exporting Note ${Progress.completedUnitCount + 1} of ${Progress.totalUnitCount}`;
// name file path
var noteFilePath = `${savePath}/${allNotesInAccount[i].name()}${outputFileSuffix}`;
// write note to file
writeTextToFile(noteFilePath, allNotesInAccount[i][outputFileFormat](), false);
// increment progress
Progress.completedUnitCount++;
}
}
// display notification
currentApp.displayNotification("All selected notes have been exported.", {
withTitle: "MacOS Notes Exporter",
subtitle: "Export is complete.",
soundName: "Glass"
});
function displayError(errorMessage) {
currentApp.displayDialog(errorMessage)
}
function writeTextToFile(file, text, overwriteExistingContent=true) {
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);
// 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}`);
}
// Return a boolean indicating that writing was successful
return false;
}
}