-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathnotes-import.scpt
More file actions
81 lines (68 loc) · 1.47 KB
/
notes-import.scpt
File metadata and controls
81 lines (68 loc) · 1.47 KB
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
//
// notes-import.scpt
// Steven Frank <stevenf@panic.com>
//
// 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
// - 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()
folderContents.forEach(function(item)
{
var fileContents = fileContentsAtPath(folderName + '/' + item)
if ( fileContents !== false )
{
var note = notesApp.Note({
'name': item,
'body': fileContents.replace(/\n/g,'<br>')
})
notesApp.folders[0].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;
}