[SOLVED] How to merge these 2 AppleScript scripts?

I'm creating a macro that creates a reminder when there is 1 or more notes in Notes.
I have this script that shows me a notification:

tell application "Notes" to set theCount to count of notes of folder "Notes"
if theCount is not 0 then display notification "There are notes to be converted" with title "Apple Notes Checker"

but instead of the notification, because I don't always have the time right away to convert the notes, I want to create a reminder instead with this code, but I can't seem to understand how to merge the 2:

tell application "Reminders"
	set mylist to list "Today"
	tell mylist
		make new reminder at end with properties {name:"Convert NOTES to Obsidian"}
	end tell
end tell

You just need to expand the if block to add the Reminders bit to it... see the following example:

AppleScript (click to expand/collapse)
tell application "Notes" to set theCount to count of notes of folder "Notes"
if theCount is not 0 then
	
	# original notification
	display notification "There are notes to be converted" with title "Apple Notes Checker"
	
	# now sets a reminder
	tell application "Reminders"
		set mylist to list "Today"
		tell mylist
			make new reminder at end with properties {name:"Convert NOTES to Obsidian"}
		end tell
	end tell
	
end if

Alternatively, you can likely simplify this by setting the variable for your list outside of the tell Reminders block, and then have the Reminders portion as a one-liner, like the following example:

AppleScript (click to expand/collapse)
set mylist to list "Today"
tell application "Notes" to set theCount to count of notes of folder "Notes"
if theCount is not 0 then
	
	# original notification
	display notification "There are notes to be converted" with title "Apple Notes Checker"
	
	# now sets a reminder
	tell application "Reminders" to tell mylist to make new reminder at end with properties {name:"Convert NOTES to Obsidian"}
	
end if

I say likely, because some applications require you to set variables they use from within their own tell blocks. I haven’t messed too much with Reminders scripting to know if it’s one of them though. Either way, try one or both of those out and see if they work. If not, let me know and we can go from there!

-chris

1 Like

Thank you so much.
In this case, the second version didn't work. It said:
image

When I was trying to merge them myself, I thought:
1 - I needed another end tell at the end, because of the beginning of the script. I'm still trying to understand why sometimes some scripts include it and sometimes it doesn't. Why is that?
2 - On the other hand, I didn't think that the if needed the end if

What is the difference between using the "non-one-liner" version and the "one-liner" version?

Whoops, if I had paid better attention I would have answered my own question. :sweat_smile: This is a case where the variable has to be set from within the application tell block, since it references a property of said application, in this case, the list “Today”. If that set command is run outside of the Reminders tell block, then the AppleScript has no idea what the word list is supposed to refer to, and fails.

It can still be simplified a bit from my first method, however...

Try this AppleScript (click to expand/collapse)
tell application "Notes" to set theCount to count of notes of folder "Notes"
if theCount is not 0 then
	
	# original notification
	display notification "There are notes to be converted" with title "Apple Notes Checker"
	
	# now sets a reminder
	tell application "Reminders"
		set mylist to list "Today"
		tell mylist to make new reminder at end with properties {name:"Convert NOTES to Obsidian"}
	end tell
	
end if

Basically, a command can “open” and “close” without the end statement if it’s all on one line. But one-liners are limited to very basic commands, and cannot run multiple commands. So if you need to run more than one command, you need to separate them by lines, in which case, you need to add the end command at the bottom.

Run this basic AppleScript to see what I’m referring to. (click to expand/collapse)
set testVar to "Howdy"

### this is a simple one line command
### all you are doing is showing a notification, nothing else
if testVar is "Howdy" then display notification "The test variable is Howdy"

display dialog "Press OK when you are ready to continue" buttons {"Cancel", "OK"} default button "OK"

### this is a multi-line command
### you are showing a notification AND a dialog box
if testVar is "Howdy" then
	display notification "The test variable is Howdy"
	display dialog "Because you had more than one command, you had to separate them onto different lines, and then close the command with an end if" buttons {"Cancel", "OK"} default button "OK"
end if

Thank you so much for this mini-lesson. :raised_hands:
Always good to learn a bit more.
I will save this to my AppleScript notes for sure!

Meanwhile, I started working on a new macro that instead of creating a reminder, it will run a few actions that convert all notes to .md files, which is what I need in the end.
I'm just having issues with some NULL characters (I guess that's what it's called) and can't figure out how to work around it. Do you know anything about this?

But I will also save this new version of the script for future reference and to learn a bit more of AppleScript. By the way: is there any YouTube channel that you'd recommend to learn AS? I don't mind written text, but video is always better. Also, Apple's pages on AS can be super confusing sometimes.