How to write AppleScript that activates a specific app's menu item?

How do I write an AppleScript that clicks a menu item in a specific app (ie “File > New Email” in Spark)? Is using the “click” command the best way? Is there a more direct script structure?

Here’s what I have:

tell application "System Events" to tell process "Spark"
	set frontmost to true
	click menu item "File"
	click menu item "New Email" of menu 1
end tell

I get this error message:

Screen Shot 2021-03-31 at 11.00.16 AM

I got the script idea from this StackExchange thread.

Use case:

I often have to shoot out emails while I’m working on something else. This means I have to app-switch to my email client, (Spark), and then hit a key command to open a “new email” window. To decrease mental load, I want to create a universal key command (shift+command+n) that opens the “new email” window right away. To do that, I think I need to assign an AppleScript to an “All Applications” key command.

see this page and the links there:

Follow the syntax/structure from the StackExchange thread that gave you the idea and it should work. You’ve truncated the syntax and that’s causing the error.

i.e. menu item … of menu 1 of menu bar item … of menu bar 1

And if you don’t want to do it single line, then you need nested tell blocks for each menu element.

I would also put in a small delay between the 2 menu item invocations. Start with 0.2 seconds and maybe graduate to 0.1. (This doesn’t solve your original problem but it might well solve your next one.) :slight_smile:

1 Like