Trying to enable the "Quit" menu item in an applet

My applet’s UI is an enhanced window created with Shane Stanley’s DTP v1.1.3. It currently has a “Quit” button which is used to do some clean up and quit the applet [with an error number -128]. I need a new button to go into the space taken by the Quit button. So, I have been trying to enable the “Quit” menu item, which is greyed out.

First idea I tried was to add this to the DTP library in the showTheWindowAndWaitMainThread handler:

That crashes with: “[NSMenultem validateMenultem]: unrecognized selector sent to instance”.

Second idea was to try code lifted from a MacScripter post: https://macscripter.net/viewtopic.php?id=32754. That crashed so bad, it’s not worth repeating the code I tried.

Another option would be to get the DTP library to report back that the window was closed and treat that as a quit. But, I have two questions: Why is the Quit menu item disabled for an AppleScript applet and is there a way to enable it or any other disabled menu item ?

Thanks.

Model: Mac Studio Max 32GB 24c (Ventura)
Browser: Safari 16.1 (18614.2.9.1.12)
Operating System: macOS 12

First of all do not call validateMenuItem(). The method is called by the framework.

An NSMenuItem must be assigned to a target and have an action, it enables the menu item automatically.

Stefan, many thanks.

I can’t figure how to make it work. I’m really not sure what “target” and “action” mean. But, I’ve tried a variety of code ideas all of which fail “unrecognized selector sent to instance”. My final attempt was this:

set appMenu to (current application's NSApp's mainMenu()'s itemAtIndex:0)'s submenu()
set menuItem to appMenu's itemAtIndex:6
menuItem's enabled:true
menuItem's settarget:me
menuItem's action:terminate()

And elsewhere I added:

to terminate()
	if name of current application does not start with "Script" then tell me to quit
end terminate

Googling has revealed a lot of examples and ideas none of which worked. There must be a way to get Quit menu item working but, I can’t find it.

You need to use
setEnabled:
setAction:

Just like you do with
setTarget:

Also the setAction: method takes a
SEL variable

In ObjC you would
SEL aSelector = @selector(yourMethod:withVariable:);

I’ve never tried it with AsOBJc.

Might need to use NSSelectorFromString(NSString *aSelectorName);
https://developer.apple.com/documentation/foundation/1395294-nsselectorfromstring?language=objc

More:
https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocSelectors.html

Also your target needs to implement that method.
Which ME your script may not.

You may wed to set the target to the current application

current application’s NSApp
Or
current application’s sharedApplication

Apple’s programming language and many others use a software pattern called MVC.
It stands for Model-View-Controller.

You could google: MVC

In simple terms:
Model could be a database, storage or data of some kind
View is what the user see on the screen, it could contents buttons, sliders…
Controller perform events or actions to communicate with the view or the model

Why is this important, it will help you to understand how a application works.

Action is a selector and in ASObjC its a string + “:” at the end.
ex. setAction:“terminateAction:”

You need a action handler
ex.
on terminateAction:sender
if name of current application does not start with “Script” then tell me to quit
end terminateAction:

The target is the recivier of the action.
https://developer.apple.com/library/archive/documentation/General/Conceptual/CocoaEncyclopedia/Target-Action/Target-Action.html

If you do a search on macscripter you could find example from me. There I used menuBar and menuItems to perform actions and also included quit. That maybe could help you to understand it better.

I’m still trying to understand why the ‘Quit’ menu item is disabled in an applet in the first place.
In every applet I have/create, the menu item ‘Quit’ works just fine.
Along with a ‘on quit’ handler in my stay-open applet, it works fine.

What kind of applet is this that the standard ‘Quit’ is greyed out?

  1. menuItem’s enabled:true has no effect. Delete the line
  2. The second t in settarget must be uppercase:
menuItem's setTarget:me
  1. You can set the action just with a string literal and the action must also have a set prefix. The action is just the name of the function/handler
menuItem's setAction:"terminate"