I have this macro that converts Notes to markdown files, but it first convert them to .txt
Today I had a note in my Notes app called "Pés". When the macro is supposed to convert that file, I get this:
2024-05-19 14:04:58 Action 15442125 failed: Read File action failed to read text file with error Error Domain=NSCocoaErrorDomain Code=264 "The file “Pés.txt” couldn’t be opened because the text encoding of its contents can’t be determined." UserInfo={NSFilePath=/Users/dannywyatt/Library/Mobile Documents/iCloud~md~obsidian/Documents/Tiago/Inbox/Converted notes from NOTES app/Pés.txt}
Can anyone explain what this is and how to work around it? My action is just this:
This is the script that converts the notes (I copied from somewhere online and made some slight changes, because I'm not that advanced when it comes to AS):
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Notes"
tell folder "Notes" of account "iCloud"
set theCount to (count notes)
set invalid to ((theCount = 0) or ((theCount = 1) and (note 1's body = "")))
end tell
end tell
if (invalid) then error number -128 -- "User canceled" error.
set rootFolder to "Macintosh HD:Users:dannywyatt:Library:Mobile Documents:iCloud~md~obsidian:Documents:Tiago:Inbox"
tell application "Finder"
set exportFolder to make new folder at folder rootFolder with properties {name:"Converted notes from NOTES app"}
set exportFolder to exportFolder as text
end tell
tell application "Notes" to activate
-- Simple text replacing
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 .md file to save the note in. We have to escape
-- the colons or AppleScript gets upset.
on noteNameToFilePath(noteName)
global exportFolder
set strLength to the length of noteName
if strLength > 250 then
set noteName to text 1 thru 250 of noteName
end if
set fileName to (exportFolder & replaceText(":", "_", noteName) & ".txt")
return fileName
end noteNameToFilePath
tell application "Notes"
repeat with theNote in notes of folder "Notes"
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set AppleScript's text item delimiters to oldDelimiters
set fileName to name of theNote as string
set filepath to noteNameToFilePath(fileName) of me
set noteFile to open for access filepath with write permission
set theText to body of theNote as string
write theText to noteFile as Unicode text
close access noteFile
end repeat
end tell
I doubt I can solve it, but I found three web pages that discuss it, and this one seemed to be the most relevant, even though it is not a recent page...
This sounds like it is related to the contents of the file, not the file name itself.
Keyboard Maestro asks the system read the file which involves determining the encoding of the contents and processing that character encoding into resulting characters. It sounds like the file is saved in a format that is ambiguous (possibly something like MacRoman which cannot easily be distinguished from other Western Latin character sets like ISO 8859-1 Latin-1 which was the relatively definitive character set on the Internet before UTF8.
Keyboard Maestro has no way to let you specify the character set, and there is no way to determine the character set from the file, so the solution is either:
Ensure that UTF8 is used for the characters, or
Figure out what the character set is that is being used and then use some tool to convert it to UTF8.
This is the code I'm using to create .txt files from each note:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Notes"
tell folder "Notes" of account "iCloud"
set theCount to (count notes)
set invalid to ((theCount = 0) or ((theCount = 1) and (note 1's body = "")))
end tell
end tell
if (invalid) then error number -128 -- "User canceled" error.
set rootFolder to "Macintosh HD:Users:dannywyatt:Library:Mobile Documents:iCloud~md~obsidian:Documents:Tiago:Inbox"
tell application "Finder"
set exportFolder to make new folder at folder rootFolder with properties {name:"Converted notes from NOTES app"}
set exportFolder to exportFolder as text
end tell
tell application "Notes" to activate
-- Simple text replacing
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 .md file to save the note in. We have to escape
-- the colons or AppleScript gets upset.
on noteNameToFilePath(noteName)
global exportFolder
set strLength to the length of noteName
if strLength > 250 then
set noteName to text 1 thru 250 of noteName
end if
set fileName to (exportFolder & replaceText(":", "_", noteName) & ".txt")
return fileName
end noteNameToFilePath
tell application "Notes"
repeat with theNote in notes of folder "Notes"
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set AppleScript's text item delimiters to oldDelimiters
set fileName to name of theNote as string
set filepath to noteNameToFilePath(fileName) of me
set noteFile to open for access filepath with write permission
set theText to body of theNote as string
write theText to noteFile as Unicode text
close access noteFile
end repeat
end tell
This creates these files:
And their content is:
Then after I remove certain characters from each file, I have this action, that saves the content of the files as .md files):
What I noticed, and I don't know if this is relevant, is that the characters "é" are replaced with "È"