Export selected notes from Notes.app w/ attachments, import to EN

Hi JD.

Thanks for the feedback. Sorry to learn that Evernote isn’t attaching the files. I’ll see if I can find any more information on the Evernote support site.

Are you able to tell whether the boxes with question marks relate to Evernote’s attempts to attach the files or to its not being able to understand Notes’s references to the attachments in the HTML? (eg. The HTML might contain something like “

<object type="application/x-apple-msg-attachment" data="cid:389017FA-FEB0-46D8-B98E-63251F971816@home">
”, which is Note’s internal reference to an attachment in that note. This currently remains in the HTML transferred to Evernote, to which it won’t mean anything.)

The idea of having the numbered folders is to avoid clashes in the event of notes or attachments having the same names. But names are possible, perhaps in combination with unique numbers. I’ll sort something out later today (BST).

Hi Nigel.

Apparently EN does not use HTML, it uses an xhtml variant they call enml.

http://dev.evernote.com/documentation/cloud/chapters/ENML.php

The only way to view it I believe is through this 3rd party web interface:

http://enml-editor.ping13.net/

I’ll give it a go when I get a chance.

PS I posted your script in the Evernote forum where I first found scripts, to see if any of those people can shed some light on it. Will report back any feedback. https://discussion.evernote.com/topic/64814-apple-notes-app/

OK I used the web interface to view the Evernote ENML code, here’s the result of my test note:

<?xml version='1.0' encoding='utf-8'?>
A Test Note With Attachment

test text

modified today


attachment below: Miura jpeg



OK. The version here addresses this aspect. Since the ‘name’ of a Notes note can be its entire first paragraph (!), the script derives a name based on the first fifteen words only. If there are more than fifteen words, the derived name is the first fifteen words followed by a horizontal ellipsis and the loop index in parenthesis. Otherwise, it’s the entire note name followed by a space and the parenthesised loop index. (It sorta makes sense.) Any slashes or colons in the results are replaced with dashes to avoid path problems.

I haven’t done anything more with the Evernote code as it would just be guesswork on my part and I see that on the Evernote forum, JMichaelTX, who’s also a MacScripter member, has asked for a link to this thread. He’s more likely to come up with the right answer for importing the attachment files into Evernote.

(*
	====================================================
	  [EN] Import Apple Notes into Evernote
	====================================================
	
	DATE:    2013-10-24
	AUTHOR: d.b.walker
	
	REVISED BY:  JMichaelTX on 2016-03-28 to make BUG fix. <https://discussion.evernote.com/topic/64814-apple-notes-app/#comment-395941>
	
	REF:
	  ¢ Importing from Apple Mail.app's Notes - Mac Help - Evernote User Forum       
	  ¢ https://discussion.evernote.com/topic/4046-importing-from-apple-mailapps-notes/?do=findComment&comment=236445
	
	Posted 24 Oct 2013
	Modified this script to work with Mavericks Notes, which is no longer in the mail app.
	Added the original creation and modification dates
	Added multiple tags - replace with your own
	Did not add the long note name fix (I needed to preserve my note names)
	====================================================
	
	FURTHER DEVELOPED BY: Nigel Garvey 2017-03-21/22/23, based on information in the Evernote fora, to allow a choice of Notes source folder(s) and to handle attachments.
	REVISION BY NG, 2017-03-29: "Note" folder names in the temporary desktop hierarchy for attachments now based on the notes' names. Any path delimiters in potential folder names now replaced with dashes.
	
	CAVEATS:
		1. I don't have Evernote and can't test that part of the code.
		2. (No longer relevant. Thanks to Yvan Koenig for the fix.)
		3. Any attachments are simply "appended" to the Evernote notes in the order they happen to be returned by Notes.
		4. The effect in Evernote of Notes's references to the attachments in the note HTML is unknown.
*)

main()

on main()
	-- User choice of one or more Notes folders (by name).
	tell application "Notes"
		activate
		set folderNames to name of folders
		set chosenFolderNames to (choose from list folderNames with multiple selections allowed)
		if (chosenFolderNames is false) then error number -128 -- Cancel button.
	end tell
	-- Preset HFS and POSIX versions of a path to a folder on the desktop for storing any attachments.
	set tempFolderPath to (path to desktop as text) & "Attachments from Notes:"
	set tempFolderPosix to quoted form of POSIX path of tempFolderPath
	
	-- Repeat with each chosen folder name:
	repeat with i from 1 to (count chosenFolderNames)
		-- Get all the notes in the folder with this name.
		set thisFolderName to item i of chosenFolderNames
		tell application "Notes" to set theNotes to notes of folder thisFolderName
		-- Get safe versions of the Notes folder name for possible use in HFS and POSIX paths.
		set safeFolderName to degremlinise(thisFolderName)
		set quotedSafeFolderName to quoted form of safeFolderName
		
		-- Repeat with each note in the folder:
		repeat with j from 1 to (count theNotes)
			set thisNote to item j of theNotes
			
			-- Get the relevant note data.
			tell application "Notes"
				set myTitle to the name of thisNote
				set myText to the body of thisNote
				set myCreateDate to the creation date of thisNote
				set myModDate to the modification date of thisNote
				set myAttachments to the attachments of thisNote
			end tell
			
			-- Any attachments will need to be extracted to the folder on the desktop and attached to the Evernote note from there.
			-- To preserve the attachment names and avoid confusion in the case of duplicated names, each attachment is saved to a separate subfolder in a hierarchy based on the folder/note/attachment structure. 
			set attachmentFiles to {}
			set attachmentCount to (count myAttachments)
			if (attachmentCount > 0) then
				-- If this note has any attachments, create or add to the hierarchy of the folder on the desktop to accommodate each one.
				-- Firstly, derive a name for the hierarchy subfolder corresponding to the note itself: a maximum of 15 words from the title + a unique number.
				set maxWords to 15
				if ((count myTitle's words) > maxWords) then
					set safeNoteName to text 1 thru word maxWords of myTitle & ".(" & j & ")"
				else
					set safeNoteName to myTitle & " (" & j & ")"
				end if
				-- Replace any path delimiters in the derived name with dashes.
				set safeNoteName to degremlinise(safeNoteName)
				-- Create the necessary subfolders.
				do shell script ("mkdir -p " & tempFolderPosix & quotedSafeFolderName & "/" & quoted form of safeNoteName & "/'Attachment '{1.." & attachmentCount & "}")
				
				-- Repeat with each attachment:
				repeat with k from 1 to attachmentCount
					set thisAttachment to item k of myAttachments
					-- Put together a specifier for a file in which to save this attachment.
					set thisFile to (tempFolderPath & safeFolderName & ":" & safeNoteName & ":Attachment " & k & ":" & thisAttachment's name) as «class furl»
					-- Create the file before trying to save to it. (Suggested by Yvan Koenig.)
					close access (open for access thisFile)
					-- Save the attachment to it.
					tell application "Notes" to save thisAttachment in thisFile -- Now it works! Thanks, Yvan!
					-- Store the file specifier for the retrieval of the attachment below.
					set end of attachmentFiles to thisFile
				end repeat
				
			end if
			
			tell application "Evernote"

				set myNote to create note with text myTitle title myTitle notebook "Imported From Notes" tags ["imported_from_notes"]
				set the HTML content of myNote to myText
				
				repeat with thisFile in attachmentFiles
					tell myNote to append attachment thisFile
				end
				
				set the creation date of myNote to myCreateDate
				set the modification date of myNote to myModDate
            
			end tell
			
		end repeat
		
	end repeat
end main

-- Replace any POSIX or HFS path delimiters in the passed text with dashes.
on degremlinise(thisText)
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {"/", ":"}
	set degreminlisedText to thisText's text items
	set AppleScript's text item delimiters to {"-"}
	set degreminlisedText to degreminlisedText as text
	set AppleScript's text item delimiters to astid
	
	return degreminlisedText
end degremlinise

Edit: More suitable arrangement of the ‘tell’ blocks. Thanks again to Yvan.

Hi Nigel,

An update: the attachment files have not actually been saved in folders as I thought they were. It looked as if they were there, but when I tried to view one or open it, it doesn’t open. The file size is 0 bytes.

I have posted this info on the EN forums too, DTLow says he is looking into it.

Hello JD

It’s my fault.
As the tip does the job for many applications, I was too confident.
When I saw that the file descriptors were created I thought that all was OK.
In fact what we get is what is done by the close access (open for access thisFile) instruction exactly as if the save one was removed.

It seems that there is a deeper problem.
Just after the instruction : set thisAttachment to item k of myAttachments
I inserted the instruction : tell application “Notes” to (get properties of thisAttachment)
When I executed the script I got :
error “Erreur dans Notes : La lecture n’a pas été autorisée.” number -10005 from properties of attachment id “x-coredata://CABFE677-3CC1-4D76-A9FF-7B60DEA99F51/ICAttachment/p10”
which seems to demonstrate that something is wrong between Notes and AppleScript.

Yvan KOENIG running Sierra 10.12.4 in French (VALLAURIS, France) jeudi 30 mars 2017 15:18:37

Drat! I’m sorry. I was sure that part was working. But I’m getting zero-byte sizes too. At least that explains why the attachments weren’t appearing in Evernote. I can’t think what to do about a bug as determined as that. :confused:

I think this may be a red herring. There’s no problem getting the individual properties of an attachment (on my system, anyway). There’s just no ‘properties’ property.

Thanks Nigel.
Here, 10.12.4, properties compile but fails at execution.
every property refuse to compile.
I am forced to ask for every property by its name.

Below is a piece of GUI scripting code which may help.

tell application "System Events" to tell process "Notes"
	set frontmost to true
	tell window 1
		class of UI elements --> {splitter group, button, button, button, toolbar}
		tell splitter group 1
			class of UI elements --> {button, scroll area, splitter, splitter group}
			tell splitter group 1
				class of UI elements --> {group, splitter, group}
				
				# not for us
				tell group 1
					class of UI elements --> {scroll area}
					tell scroll area 1
						class of UI elements --> {table}
						tell table 1
							class of UI elements --> {row, row, row, column}
							tell row 1
								class of UI elements --> {UI element}
								class of UI elements of UI element 1 --> {image, image, static text, static text, static text, image}
								value of every static text of UI element 1 --> {"trucmuche", "2 pièces jointes", "vendredi"}
							end tell -- row 1
							tell row 2
								class of UI elements --> {UI element}
								class of UI elements of UI element 1 --> {image, image, static text, static text, static text, image}
								value of every static text of UI element 1 --> {"tartempion", "Pas de texte supplémentaire", "17/02/2016"}
							end tell -- row 2
							tell row 3
								class of UI elements --> {UI element}
								class of UI elements of UI element 1 --> {image, image, static text, static text, static text, image}
								value of every static text of UI element 1 --> {"azerty uiop", "Son of a .", "18/09/2015"}
							end tell -- row 3
							tell column 1
								class of UI elements --> {}
							end tell -- column 1
						end tell -- table 1
					end tell -- scroll area 1
				end tell -- group 1
				
				tell group 2
					class of UI elements --> --> {static text, scroll area}
					tell scroll area 1
						class of UI elements --> --> {text area, scroll bar}
						tell text area 1
							class of UI elements --> {UI element, UI element}
							tell UI element 1
								description --> "image jointe, grande"
								position --> {1807, 316}
								size --> {300, 601}
								value of attribute "AXValueDescription" --> "bordereau Chronopost.jpg"
							end tell -- UI element 1
							tell UI element 2
								description --> "fichier joint"
								position --> {1807, 933}
								size --> {292, 72}
								value of attribute "AXValueDescription" --> "test Shane.scptd, 13 Ko"
							end tell -- UI element 2
						end tell -- text area 1
						
						tell scroll bar 1
							class of UI elements -->{value indicator, button, button, button, button}
							class of UI elements of value indicator 1 --> {}
							subrole of buttons --> {"AXIncrementArrow", "AXDecrementArrow", "AXIncrementPage", "AXDecrementPage"}
							properties of button 1 --> {minimum value:missing value, orientation:missing value, position:{2167, 260}, class:button, role description:"Flèche d'augmentation", accessibility description:missing value, focused:missing value, title:missing value, size:{0, 0}, value:missing value, help:missing value, enabled:missing value, maximum value:missing value, role:"AXButton", entire contents:{}, subrole:"AXIncrementArrow", selected:missing value, name:missing value, description:"Flèche d'augmentation"}
						end tell -- scroll bar 1
						
					end tell -- scroll area 1
				end tell -- group 2
			end tell
		end tell
	end tell
end tell

It may be used with cliclick to double click every attachment (the UI elements for which I grab position and size).
Doing that open pictures in Preview
it open scripts in our current editor .
so that we may save them as we want.
It’s awful but at least it would do the job.
It seems that it would be a good idea to file a bug report to Apple engineers.
After all, they corrected the bug forcing us to use close access (open for access thisFile) when driving many applications, maybe they would be fair enough to kill one striking upon an Apple one.

Yvan KOENIG running Sierra 10.12.4 in French (VALLAURIS, France) jeudi 30 mars 2017 17:14:08

JMichaelTX on the Evernote forum (https://discussion.evernote.com/topic/64814-apple-notes-app/?page=2#comment-461374), and I, are wondering about why this is a bug in Sierra, yet we are getting the same behaviour in El Capitan which we are both running?

We don’t seem to be able to get Apple Notes to export and save the attachments at all, can we?

As I wrote, it’s my fault.
i thought that the inability to save the attachment was the problem which stroke upon Mail, Pages, Numbers, Keynote, Word, Excel.
I was wrong. With Notes we are facing a different problem.
You wrote that it strikes under El Capitan. Thanks, I learnt something.
It would be interesting to know if it already stroke before.
As I have both installed I will test under 10.10 (Yosemite) and 10.9 (Mavericks).

I’m back.
I was able to save attachments under 10.9.5 and 10.10.5.
I tried under 10.11.6 with the folder on the desktop and with the folder in Containers:com.apple.notes.
In both cases it failed.

It would be fine that one of you, real Notes user, send a bug report to Apple.

I sent one registered as #31398740:

[i]Summary:
When we use 10.11 or 10.12 we can’t save Notes attachments thru AppleScript (worked under 10.9 and 10.10)

Steps to Reproduce:
open the attached script
execute it

Expected Results:
it was supposed to save the attachments in a folder desktop/Attachments from Notes

Actual Results:
the folder is created with its internal hierarchy but the attachments aren’t saved. No error message issued

Version:
I tested under 10.11.6 and 10.11.4 on my iMac 2012

Notes:
At first look the problem resembled to one reported under 10.12 for :
TextEdit, Pages, Numbers, Keynote, Word, Excel, Mail and was killed in 10.12.4.
Notes odd behavior is a different problem.

Configuration:
Several users reported the problem running 10.11 or/and 10.12 on different machines.
I tested under 10.11.6 and 10.11.4 on my iMac 2012[/i]

Yvan KOENIG running Sierra 10.12.4 in French (VALLAURIS, France) lundi 3 avril 2017 09:52:39

This version populates the attachment file list with aliases to where Notes keeps the attachments. It works for me, BUT it carries an enormous health warning. It parses Notes’s database file using sed, which isn’t the appropriate tool for the job. So while it works with my test notes, there may be cases where it doesn’t correctly identify the ends of the file URLs. In such cases, it should error when it tries to coerce the NSURLs derived from them to aliases. Hopefully someone who knows how to script database queries will be able to improve it.

Edit: Script reposted with redundant code from the previous version removed, the getAttachmentLookup() handler rewritten entirely in ASObjC, and the Evernote code re-enabled.

(*
	====================================================
	  [EN] Import Apple Notes into Evernote
	====================================================
	
	DATE:    2013-10-24
	AUTHOR: d.b.walker
	
	REVISED BY:  JMichaelTX on 2016-03-28 to make BUG fix. <https://discussion.evernote.com/topic/64814-apple-notes-app/#comment-395941>
	
	REF:
	  ¢ Importing from Apple Mail.app's Notes - Mac Help - Evernote User Forum       
	  ¢ https://discussion.evernote.com/topic/4046-importing-from-apple-mailapps-notes/?do=findComment&comment=236445
	
	Posted 24 Oct 2013
	Modified this script to work with Mavericks Notes, which is no longer in the mail app.
	Added the original creation and modification dates
	Added multiple tags - replace with your own
	Did not add the long note name fix (I needed to preserve my note names)
	====================================================
	
	FURTHER DEVELOPED BY: Nigel Garvey 2017-03-21/22/23, based on information in the Evernote fora, to allow a choice of Notes source folder(s) and to handle attachments.
	REVISION BY NG, 2017-03-29: "Note" folder names in the temporary desktop hierarchy for attachments now based on the notes' names. Any path delimiters in potential folder names now replaced with dashes.
	FURTHER REVSION 2017-04-03/4: The attachment files list is now populated with aliases to Notes's copies of the files.
	
	CAVEATS:
		1. I don't have Evernote and can't test that part of the code.
		2. Only intended for use with Notes's "On my Mac" account.
		3. Any attachments are simply "appended" to the Evernote notes in the order they happen to be returned by Notes.
		4. The effect in Evernote of Notes's references to the attachments in the note HTML is unknown.
		5. Although the script works for me, there's a possibility it may not correctly identify some file URLs. 
*)

use AppleScript version "2.5" -- Mac OS 10.11 (El Capitan) or later. (For NSURL coercions to alias.)
use framework "Foundation"
use scripting additions

main()

on main()
	-- User choice of one or more Notes folders (by name).
	tell application "Notes"
		activate
		set folderNames to name of folders
		set chosenFolderNames to (choose from list folderNames with multiple selections allowed)
		if (chosenFolderNames is false) then error number -128 -- Cancel button.
	end tell
	
	-- Get an NSDictionary which has the attachment CIDs as keys and the corresponding file URLs as values.
	set attachmentLookup to getAttachmentLookup()
	
	-- Repeat with each chosen folder name:
	repeat with i from 1 to (count chosenFolderNames)
		-- Get all the notes in the folder with this name.
		set thisFolderName to item i of chosenFolderNames
		tell application "Notes" to set theNotes to notes of folder thisFolderName
		
		-- Repeat with each note in the folder:
		repeat with j from 1 to (count theNotes)
			set thisNote to item j of theNotes
			
			tell application "Notes"
				-- Get the relevant note data.
				set myTitle to the name of thisNote
				set myText to the body of thisNote
				set myCreateDate to the creation date of thisNote
				set myModDate to the modification date of thisNote
				set myAttachments to the attachments of thisNote
			end tell
			
			-- Get alias specifiers for any attachments.
			set attachmentFiles to {}
			repeat with thisAttachment in myAttachments
				-- Get this attachment's content identifier (cid) from Notes.
				tell application "Notes" to set thisCID to content identifier of thisAttachment
				-- Use it to look up the corresponding NSURL in the lookup dictionary and store the result as a file specifier.
				set end of attachmentFiles to (attachmentLookup's valueForKey:(thisCID)) as alias
			end repeat
			
			tell application "Evernote"

				set myNote to create note with text myTitle title myTitle notebook "Imported From Notes" tags ["imported_from_notes"]
				set the HTML content of myNote to myText
				
				repeat with thisFile in attachmentFiles
					tell myNote to append attachment thisFile
				end
				
				set the creation date of myNote to myCreateDate
				set the modification date of myNote to myModDate
            
			end tell
			
		end repeat
		
	end repeat
	
end main

-- Create a lookup dictionary (NSDictionary) which has all the available attachment CIDs as keys and URLs to the corresponding files as values.
on getAttachmentLookup()
	set |⌘| to current application
	set fileManager to |⌘|'s class "NSFileManager"'s defaultManager()
	-- Get an NSURL to the user Library folder and thence to the folder containing Note's database file(s).
	set userLibraryFolderURL to (fileManager's URLForDirectory:(|⌘|'s NSLibraryDirectory) inDomain:(|⌘|'s NSUserDomainMask) appropriateForURL:(missing value) create:(false) |error|:(missing value))
	set NotesDBFolderURL to userLibraryFolderURL's URLByAppendingPathComponent:("Containers/com.apple.Notes/Data/Library/Notes")
	-- Or of course:
	(* set NotesDBFolderPath to |⌘|'s class "NSString"'s stringWithString:("~/Library/Containers/com.apple.Notes/Data/Library/Notes")
	set NotesDBFolderPath to NotesDBFolderPath's stringByExpandingTildeInPath()
	set NotesDBFolderURL to |⌘|'s class "NSURL"'s fileURLWithPath:(NotesDBFolderPath) *)
	-- On my system, the database file of interest has a name ending with "-wal".
	set databaseCandidates to fileManager's contentsOfDirectoryAtURL:(NotesDBFolderURL) includingPropertiesForKeys:({}) options:(|⌘|'s NSDirectoryEnumerationSkipsHiddenFiles) |error|:(missing value)
	set walFilter to |⌘|'s class "NSPredicate"'s predicateWithFormat:("path ENDSWITH '-wal'")
	set databaseURL to (databaseCandidates's filteredArrayUsingPredicate:(walFilter))'s firstObject()
	
	-- The file's contents are binary data, but this hack involves treating them as ISO Latin1 encoded text.
	set databaseText to |⌘|'s class "NSString"'s stringWithContentsOfURL:(databaseURL) encoding:(|⌘|'s NSISOLatin1StringEncoding) |error|:(missing value)
	-- Set a regex to scan for instances of attachment content identifiers (CIDs) in angle brackets ("<xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx@home>" on my machine, simply "<xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx>" on Yvan's) and of URLs to files in Notes's attachments folder hierarchy ("file:///Users/username/Library/Containers/com.apple.Notes/Data/Library/CoreData/Attachments/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/filename").
	set searchRegex to |⌘|'s class "NSRegularExpression"'s regularExpressionWithPattern:("(?<=<)[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}(?:@home)?(?=>)|file:///Users/[^/]++/Library/Containers/com\\.apple\\.Notes/Data/Library/CoreData/Attachments/[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}/[\\u0021-\\u007f]++") options:(0) |error|:(missing value)
	-- For additional security, find out where the first CID occurs in the "text".
	set searchStart to (databaseText's rangeOfString:("<[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}(?:@home)?>") options:(|⌘|'s NSRegularExpressionSearch))'s location()
	-- Start the search proper from there and get the ranges of all the matches. (No harm if none.)
	set matchRanges to (searchRegex's matchesInString:(databaseText) options:(0) range:({searchStart, (databaseText's |length|()) - searchStart}))'s valueForKey:("range")
	
	-- The matches should be alternating instances of CIDs and file URLs, but the code below can easily be modified if this turns out not always to be the case.
	-- Extract the CIDs to one array and NSURL versions of the file URLs to another.
	set cidPrefix to |⌘|'s class "NSString"'s stringWithString:("cid:")
	set theCIDs to |⌘|'s class "NSMutableArray"'s new()
	set theURLs to |⌘|'s class "NSMutableArray"'s new()
	set i to 1
	set j to 2
	set matchCount to matchRanges's |count|()
	repeat until (j > matchCount)
		set thisCID to databaseText's substringWithRange:(item i of matchRanges)
		set thisURL to databaseText's substringWithRange:(item j of matchRanges)
		tell theCIDs to addObject:(cidPrefix's stringByAppendingString:(thisCID))
		tell theURLs to addObject:(|⌘|'s class "NSURL"'s URLWithString:(thisURL))
		set i to j + 1
		set j to i + 1
	end repeat
	
	-- Make and return an NSDictionary with the CIDs as the keys and the NSURLs as the values.
	-- The CID/NSURL pairs will no doubt be duplicated in the lists, but not in the dictionary.
	return |⌘|'s class "NSDictionary"'s dictionaryWithObjects:(theURLs) forKeys:(theCIDs)
end getAttachmentLookup

Just checking back here, seems no-one has had any joy in getting the Notes part to work or even reporting the Notes behaviour as a bug (as Yvan suggested).

I won’t play with your latest script @Nigel Garvey, as I am heeding your “enormous health warning”!

PS Since the postings I have upgraded to Sierra.

Jonathan

Have the opportunity to test with Evernote. Just tried the latest version of the script. The script stops for an error:

error “unable to set argument 4 - the AppleScript value <NSAppleEventDescriptor: [ 9.22337e+18, -9.22337e+18 ]> could not be coerced to type {_NSRange=QQ}.” number -10000

I am not very familiar (yet) with applescript, it doesn’t give me a clue.
Anyone?

Model: MacBook Air
AppleScript: 2.9
Browser: Safari 537.36
Operating System: Mac OS X (10.10)

What you got is not surprising.
The script posted by Nigel carefully states :
[format]use AppleScript version “2.5” – Mac OS 10.11 (El Capitan) or later. (For NSURL coercions to alias.)[/format]

Your signature tell us that you are running 10.10, so, if this signature is right it’s normal that the script fails.

I highlighted the words if this signature is right because if my memory is right, the script is supposed to refuse to compile on a 10.10 machine.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) jeudi 1 juin 2017 21:04:34

Hi. Welcome to MacScripter.

Much of the script’s in AppleScriptObjC, which is an extension to AppleScript allowing it to use some of the operating system’s Objective-C methods directly.

The error looks like an AppleScriptObjC one which occurs when a script fails to find a substring in a text but tries to extract it anyway. The offending object’s usually [ 9.22337e+18, 0 ].

At a quick guess, I’d say the error was happening in these lines because your Notes database is empty or the script’s not reading it properly:

set searchStart to (databaseText's rangeOfString:("<[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}(?:@home)?>") options:(|⌘|'s NSRegularExpressionSearch))'s location()
-- Start the search proper from there and get the ranges of all the matches. (No harm if none.)
set matchRanges to (searchRegex's matchesInString:(databaseText) options:(0) range:({searchStart, (databaseText's |length|()) - searchStart}))'s valueForKey:("range")

If databaseText is an empty string, the regex search in the first line will return {location:9.22337203685478E+18, |length|:0}, which should be the range of the match, but that large location value is a code meaning “not found”. Since the script’s not expecting the substring not to be found, it goes ahead and calculates another search range from the result, using the error code as the new start location and databaseText’s zero length minus that for the range length. Ir’s probably the negative length that’s the immediate cause of the system’s complaint.

It’s late at night here and I’ve largely forgotten what the script’s supposed to do, but I’ll look at it in the morning.

Hi Alcedo.

Yes. I’m pretty sure the error’s happening in the lines I pinpointed last night. But it doesn’t necessarily mean your database file’s empty, only that the regex couldn’t be matched in the “text” from it. (The error code 9.22337203685478E+18 is such a large number that you have to subtract it from a pretty large amount before the truncated form of the result is anything other than -9.22337e+18.)

So, either:
Your database is empty, or
It doesn’t contain any note CIDs, or
The CIDs are in a different format from those used in El Capitan and Sierra (“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx@home” or “” ” where each “x” is a hexadecimal digit character), or
You have more than one database file in ~/Library/Containers/com.apple.Notes/Data/Library/Notes whose name ends with “-wal” and the script’s reading the wrong one, or
ISOLatin1StringEncoding isn’t the best interpretation to impose when reading the file, or
Something else I haven’t thought of.

The search regexes could be made tighter by changing every instance of “[[:alnum:]]” in them to “[[:xdigit:]]”, but otherwise any fix would depend on what’s causing the CIDs not to be identified.

@Nigel, sorry, but I’m not following the meaning of “BUT it carries an enormous health warning.”.
Could you please explain?

Looks like a great script, and shows a lot of innovation. I haven’t had a chance to test it yet in my system (macOS 10.11.6), but I hope to soon.

Thanks for sharing.

Hi JMichaelTX.

Pretty much what it says in the rest of that paragraph and in the caveats in the script comments. The script uses regex to parse a database file, which isn’t the most foolproof idea in the world, and was written by someone who doesn’t use Notes and doesn’t have Evernote or an iCloud account. The Notes side of it worked in 10.11.6 on my machine with the on-board notes I created to research the problem, but otherwise I was hoping that someone with more knowledge of database and/or Evernote scripting would be able to expand my groundwork into something more solid.

Since the post before yours, MacScripter’s site software’s been updated and some characters in the posts above are now appearing as gibberish strings through being interpreted with different encodings from the ones under which they were posted. The ‘|⌘|’ label I use as a shortcut for ‘current application’ looks like ‘|⌘|’ (although this doesn’t actually sabotage the script) and there are various other things throughout the thread such as ‘”’ for ‘—’ and the misrendering of the diacriticals in Yvan’s results.

By which I mean my contribution, of course. I didn’t mean to imply any disregard for the original code by either d.b.walker or JMichaelTX.

Just checking to see if there’s been any activity forward on this. The two scripts do not seem to work with High Sierra. The first one produces the 0 byte file and the second failed with

error “Can’t make missing value into type alias.” number -1700 from missing value to alias

on the

set end of attachmentFiles to (attachmentLookup’s valueForKey:(thisCID)) as alias

line.

Thanks!