Possible to cancel AppleScript's script, but ignore notification? [SOLVED]

I have the following script to convert Notes to .md files, but if the Notes app doesn't have any notes or there's an empty note (which is what happens when you open Notes and there's none there), I want KM the script to cancel, but I don't want to see the error notification. I just want it to cancel silently, if that makes sense?


Here's the script:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use framework "Foundation"

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.

-- Get current date and time
set currentDate to current date
set dateFormatter to current application's NSDateFormatter's new()
-- Set your desired date format here
dateFormatter's setDateFormat:"MMM d, yyyy 'at' h'h' mm'm' ss's' a"
set formattedDate to (dateFormatter's stringFromDate:currentDate) as text

set rootFolder to "Macintosh HD:Users:dannywyatt:Library:Mobile Documents:iCloud~md~obsidian:Documents:Tiago:Inbox"

-- Append current date and time to the folder name
set folderName to "Converted notes from NOTES app " & formattedDate

tell application "Finder"
	set exportFolder to make new folder at folder rootFolder with properties {name:folderName}
	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
	-- Construct file path
	set fileName to (exportFolder & replaceText(":", "_", noteName) & ".md")
	-- Remove Horizontal Ellipsis (U+2026) from filename
	set fileName to my replaceText("…", "", fileName)
	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 plaintext of theNote as string
		write theText to noteFile as «class utf8»
		close access noteFile
	end repeat
end tell

You're explicitly throwing an error with the line

if (invalid) then error number -128 -- "User canceled" error.

Try changing that to

if (invalid) then return 0

...or some value other than 0. The return will end the script at that point and you don't need a value, but the good thing about returning something is that you can "Save results to a variable" in KM then test them later in your macro, or just leave the action set to "Ignore" and... ignore it.

1 Like

The person who helped me with the script added that and unless I'm wrong, it sounded like that was the only way to go if there were no notes.

That worked like a charm!

And that's another AppleScript lesson for me.

So if I understand it correctly:
Let's say I have a different script and at one point I have some "action" that includes a return. Whatever that action's result value is, when I use return, KM will save that as a variable (if I set it to Save results to a Variable)?
So would it be similar to when we have a variable and then we use something like this?

set myAppleScriptVar to "12345"

tell application "Keyboard Maestro Engine" to setvariable "myKMvar" to myAppleScriptVar

In this case, instead of using the "tell application......" wouldn't I be able to use return and use the Save results to a variable option?
I just tested this and it shows 15, as expected

So I guess I could replace (depending on each scenario, of course) all those lines with a simple return.
Hope I'm making sense?

It's one way to go, and a good one if you are expecting the script to be run by a user -- you want to tell them that the script failed to do what they expected and, ideally, why. Throwing an error, complete with dialog, will do that nicely.

That's not so good if you want the script to run unattended (on a timer, maybe), and may or may not work well within a KM macro -- that will depend on what you want to happen after an error.

It depends where in the script the return is. At the "top level", like here, then yes -- the return ends the script. But if it is part of a function it will return to the function's caller (like a KM sub-routine can send back a result to the "parent" macro).

And the return is also an "exit now" -- whatever is doing the returning stops at that point. In this case, because it is at the "top level", the script itself.

It depends on what you are doing. When a script completes the result of the last evaluation is returned anyway and can be saved to a KM variable. So


...sets Local_result to 15.

But you can explicitly return something other than the last result:


...which will set Local_result to 10.

You can only return one value, so setvariable comes into its own if you want your script to set two or more KM variables (leaving aside pseudo arrays, JSON, etc :wink: ). And, since return means the end of the script, you can't use it if you want to set KM variables partway through.

1 Like

Thank you so much for all the detailed info. Definitely something I will be adding to my AppleScript notes :wink:

Really appreciate your time and help! :raised_hands: