-
Notifications
You must be signed in to change notification settings - Fork 8
/
notes-import.scpt
134 lines (108 loc) · 2.56 KB
/
notes-import.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
//
// notes-import.scpt
// Steven Frank <[email protected]>
//
// Import a folder full of HTML or text files into Notes.app.
// Crude, but better than nothing. Maybe?
//
// Usage:
// - Open this file in Script Editor
// - Change Script Editor language popup from AppleScript to JavaScript
// - Run
// - Select folder containing files to import
//
// Known issues:
// - Attachments don't get imported
// - Formatting/HTML is not preserved
// - No idea how text encoding is handled
// - File extension should be stripped from title of new note
//
var notesApp = Application('Notes')
notesApp.includeStandardAdditions = true
notesApp.activate()
// Prompt user to pick a folder
var folderName = notesApp.chooseFolder()
var folderContents = Application('System Events').folders.byName(folderName.toString()).diskItems.name()
var notesFolder = getNotesFolder();
folderContents.forEach(function(item)
{
var fileContents = fileContentsAtPath(folderName + '/' + item)
if ( fileContents !== false )
{
var note = notesApp.Note({
'name': item.replace(/\.\w{3,4}$/, ''),
'body': fileContents
})
notesFolder.notes.push(note)
}
})
function fileContentsAtPath(pathAsString)
{
var path = Path(pathAsString)
var app = Application.currentApplication()
app.includeStandardAdditions = true
try
{
var file = app.openForAccess(path);
}
catch (e)
{
return false;
}
var eof = app.getEof(file)
var data = null
try
{
data = app.read(file,
{
'to': eof
});
}
catch (e)
{
return false;
}
finally
{
app.closeAccess(file);
}
return data;
}
// Prompt user to pick a folder from chosen account if more than one
function getNotesFolder()
{
var folderChoice;
var acct = getNotesAccount();
var folders = [];
var msg = 'Which folder in ' + acct.name() + '?';
if (acct.folders.length === 1) {
return acct.folders[0];
}
for (var e in acct.folders) {
folders.push(acct.folders[e].name())
}
folderChoice = notesApp.chooseFromList(folders, {
withTitle: 'Folder for import',
withPrompt: msg
});
return acct.folders[ folderChoice[0] ];
}
// Prompt user to pick an account in Notes app if more than one
function getNotesAccount()
{
var acctChoice, acct;
var options = [];
var accts = notesApp.accounts;
if (accts.length === 1)
{
return notesApp
}
for (var e in accts) {
options.push(accts[e].name());
}
acctChoice = notesApp.chooseFromList(options, {
withTitle: 'Notes Account',
withPrompt: 'Which account do you want to use to import your notes?'
});
return notesApp.accounts[ acctChoice[0] ];
}