I have this script to check if the number of notes in the Notes app is not zero, and if not, then run the script. The issue is that Notes always creates an empty note called “New Note” (even though the note itself is empty), so no matter what, it will always “validate” the script, which is not intended.
So what I wanted to achieve is:
1 - Check if the number of notes is not zero (which is doing it already)
2 - If it’s not zero, check if it’s just 1 and not 2 or more, to check if the note in there is that empt one.
3 - If it’s just 1, check if the note is empty. If so, discard the rest of the script.
This way, it will only run the actual “meat” of the script, if there’s more than 1 note, or, if there’s a single note where the body has something in it (not empty).
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Notes" to set theCount to count of notes of folder "Notes"
if theCount is not 0 then
set rootFolder to "path:to:my:folder"
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
end if
-- 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