Creating a Menubar Applet?

So I followed the tutorial at, and it all worked.

But I’m not very good at Cocoa, so I wanted to follow the same path, but calling everything through AppleScript Studio.

On my menu item, I have “choose menu item” checked and connected to the right script.

But, when invoked, I get the following error: “Can’t get «class menl» id 2 of item id 1. (-1728)”

I’m completely confused as to why.

If necessary, I can upload the project for reference, but I really don’t know where to upload it to. :confused:



AppleScript Studio cannot control menu items of NSStatusMenu because there is no appropriate
AppleScript reference

Okay then, new question.

If I make a script file that contains the proper commands that I wish to execute, is there a Cocoa method of executing said script file?

So clicking on the proper menu item will tell Cocoa to tell the script to run. Does that make sense? Is there any way to accomplish this?

I know there is a way to do it if the script is embedded inside part of the Cocoa code, but I’d rather just have it open the file and run its contents.


In Cocoa you can run scripts with NSAppleScript, in the tutorial you’ve mentioned in the first post look at the section “One Last Thing”. Source code can be passed by a literal string as well as a compiled script file

This is sample code to execute a precompiled script file located in the Scripts folder

NSString *scriptPath = [[NSBundle mainBundle] pathForResource: @"myScript" ofType: @"scpt" inDirectory: @"Scripts"]; NSAppleScript *script = [[NSAppleScript alloc] initWithContentsOfURL: [NSURL fileURLWithPath: scriptPath] error: nil]; NSDictionary *error; NSAppleEventDescriptor *result = [script executeAndReturnError:&error]; [script release];

Thanks soooo much! :smiley:

Now another random question:

My script has a loop in it. When activated by the menubar, it loops and the menu item never gets “unselected.”

How can I fix this?

One idea I have is to set a variable when the item is selected. Then, in the “idle” handler (or whatever the Cocoa equivalent is), check for said variable and either run the script or not based on how my variable is set. Would this work? How does the Cocoa “idle” handler work? How can I set global variables in Cocoa?


Unlike plain AppleScript (including AppleScript Studio) Cocoa is multi-threaded. Tasks can be performed in the background. Global variables can be defined in the same scope (class file), or extern in all files of a project.
There is no explicit idle handler in Cocoa. Recurring tasks can be scheduled with NSTimer or you use the notification system to be notified for particular events