Check Notes content [SOLVED]

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

Hi @alltiagocom. Something like this?

tell application "Notes"
	activate
	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.

-- Rest of script here.
2 Likes

Sorry for the delayed reply, but I wanted to really sit and test this, because I’m still learning AS and I knew it would take me a bit of time to see how I could adapt that to my Keyboard Maestro macro.

That worked like a charm! Thank you so much!

So I ended up splitting the script into 2 Keyboard Maestro actions, so it won’t even go to the next actions if the result from Notes comes “invalid”.

So the first script is:

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.

I don’t need to add that parte to activate, because apparently it just does it anyway. At least, for this particular script, I just need it to check the validity of that “statement” (if that’s what you call it…?)

So once that’s valid (meaning I have important notes), it will run some Keyboard Maestro actions to create some folders and rename them properly, etc, and then it runs the second script (do you mind checking if there’s anything that I could write differently or something that’s not necessary?)

For this particular case I added the part to activate Notes, because I will need it to then move and resize the Notes window using a Keyboard Maestro action.
The only thing that’s slightly different from the original is the part before “Simple text replacing”, so if you don’t mind just checking that section, I would appreciate it.

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

But yes, that works and I really wanna thank you for helping me with this. Not only will this make it work as expected, but looking at other people’s scripts is always a way for me to learn a bit more AS. :raised_hands: