forked from alexwlchan/notes-export
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotes-export.scpt
77 lines (63 loc) · 2.38 KB
/
notes-export.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
--
-- notes-export.scpt
-- Alex Chan <alex@alexwlchan.net>
--
-- Export all the notes from Notes.app into a folder as HTML text.
-- A very basic backup script.
--
-- Usage:
-- * Open this file in Script Editor
-- * Run
-- * Select a folder to export files to
--
-- Output:
-- * A collection of (Latin-1 encoded) HTML files
-- * A list of attachment names for each note.
--
-- Known issues:
-- * Only the names of attachments are exported, not the files
-- themselves. (Look for the files in
-- ~Library/Group Containers/group.com.apple.notes/Media).
-- * Some attachment types (e.g. app links) show up as "Missing value"
-- * Some formatting is lost.
-- * A note with the same title as a previously exported note will be
-- overwritten.
-- * It will exported notes in the "Recently Deleted" folder which
-- haven't been purged from disk yet.
--
set exportFolder to (choose folder) as string
-- Find and Replace text. Yes, this really takes 11 lines.
-- Taken from http://brucep.net/2007/replace-text/
on replaceText(find, replace, subject)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set subject to text items of subject
set text item delimiters of AppleScript to replace
set subject to "" & subject
set text item delimiters of AppleScript to prevTIDs
return subject
end replaceText
-- Get an HTML file to save the note in. We have to escape
-- the colons or AppleScript gets upset.
on noteNameToFilePath(noteName)
global exportFolder
return (exportFolder & replaceText(":", "_", noteName) & ".html")
end noteNameToFilePath
tell application "Notes"
set attachmentLog to open for access (exportFolder & "_attachments.txt") with write permission
repeat with theNote in notes
-- Write the body of the note out to file as HTML
set filepath to noteNameToFilePath(name of theNote as string) of me
set noteFile to open for access filepath with write permission
write (body of theNote as string) to noteFile starting at (get eof of noteFile) + 1
close access noteFile
-- Record a list of attachments for this file
if (count of (attachments of theNote)) is greater than 0 then
write ("\n" & name of theNote & ":\n\n") to attachmentLog
end if
repeat with theAttachment in attachments of theNote
write ("* " & name of theAttachment & "\n") to attachmentLog
end repeat
end repeat
close access attachmentLog
end tell