How to contextual menu's to popup button

Hi
It appears that I can add a contextual menu to a popup button, (according to the doc info)

https://developer.apple.com/design/human-interface-guidelines/components/menus-and-actions/pop-up-buttons

but for the life of me I cant find anything that demonstrates this using ASOBJC in Xcode.

So the question is, am I understanding this incorrectly?, and can it be done?, if it can be done
could someone kindly point me in the right direction to some code, or demonstrate how this is
done,

cheers

When you say “contextual menu”, what do you mean ?

The type of pop-up button you would use within an AppleScriptObjC Xcode project, would be the “NSPopUpButton” class.
https://developer.apple.com/documentation/appkit/nspopupbutton

What the “NSPopUpButton” class does, is show a list of items a bit like a menu, is that what you are asking about ?

I suggest using DialogToolkitPlus, from Shane Stanley. Not only does it let you add a popup menu to a dialog, but you can inspect the code and see how its done.

Freeware | Late Night Software

@estockly
I believe the op is asking about using pop-up buttons within an Xcode project, and not dialog box’s.
Shane’s dialog tool kit is used for displaying and customising dialog boxes, and not specifically for using pop-up buttons.

thanks for the replies,

I have a popup button, and have no problems populating it with information using something similar as below


property slugType : missing value

on awakeFromNib()
    set listOfSlugs to {"BLACK","SPOT","CMYK"}
    slugType's removeAllItems()
    slugType's addItemsWithTitles:listOfSlugs
end awakeFromNib

this demonstrates exactly what I am trying to achieve, add extra submenu or contextual menu

https://developer.apple.com/design/human-interface-guidelines/components/menus-and-actions/menus

submenu as it is described in the guidelines

https://developer.apple.com/design/human-interface-guidelines/components/menus-and-actions/pop-up-buttons

https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/MenuList/Articles/DisplayContextMenu.html

technomorph, thank you…, appreciated

terminology is the real secret to finding information, my lack of how to translate
though is my flaw with ASOBJC, id appreciate some guidance with this please, if any one has the time.

cheers

Ok I’ll see what i can do when I have time.
I last used it in Objective-C context.
I started in AppleScript, then into AsOBJc and there was lots
Of learning curves, but it helped me into Objective-C which
Is where most of my programming happens, with some AppleScript and
scripting bridge.

Also when I last used contextual menu item it was also
Done on a NSTextView so that it would respond and show a menu
For selected text items.

That was a little bit more complicated as it involved many of the
NSTextEditor protocols and I think I also had to add my menu items to the already existing
Contextual Text menu items.

So it seems like you know how to connect an IBOutlet from your AsOBJc code to a menu.

What is it exactly you trying to accomplish?

  • change/add a menu in a PopUpButton?
    • (just connect your IBOutlet to the menu of the Button)
  • add a general anywhere context Pop Up Menu to your view?

==============================================================================
Few things I suggest:

  1. You seem to know how to remove and add items to an existing menu:

    • learn to create your own menu.
      - learn the various ways to create menuItems and it’s properties:
      - Title, Tag, Target, Action, Identifier, Represented Object, keyEquivalent
      - learn how to add, remove menuItems to a menu
      - learn how to add dividers and subMenus to a menu
  2. Learn how to programatically select menu items, enable them and access whats selected

  3. Learn how to set your menu as the menu for an item (PopUpButton a NSView etc)

  4. Learn how to programatically select the Button/Views’s menu’s items and how to access what’s selected

  5. Learn how to programatically set the target/action

    • on a menuItem itself
    • on a menu as a whole (do you need to set separate actions for each menu?)
    • on the button/NSViews
  6. Learn how to set up an appropriate IBAction and receive the action from the sender
    and how to decode it.

  7. Learn how to customize the appearance of your Button, Menu, MenuItems.

  8. Learn some shortCuts for the above.
    ==============================================================================

But again before we can help you it needs. to be clear what your after.

Here’s examples:

https://macscripter.net/viewtopic.php?pid=207449

https://macscripter.net/viewtopic.php?pid=201876

https://stackoverflow.com/questions/63333714/enable-menu-items-and-call-function-when-activated-in-applescript

and lots of stuff here, though you have to translate each page as you go to it:
https://piyocast-com.translate.goog/as/archives/tag/nsmenu?_x_tr_sch=http&_x_tr_sl=ja&_x_tr_tl=en&_x_tr_hl=en&_x_tr_pto=wapp

technomorph, thanks for the pointers and suggestions, I have not been able to achieve exactly what I am trying to do, and it’s not for the lack of trying.

Basically, I am trying to progmatically or manually through bindings, have a NSPopUpButton, and then add to the NSMenu’s NSmenuItem another NSMenu, which is a subMenu, I cando this manually, but I cant seem to get the result of the selected items in the added NSMenu

I have tried the below

set theItemArray to menuOne's itemArray
    log theItemArray

which returns

so when I select Item 1 it only shows that a submenu exists, but I cannot select anything in menItem
so I am not even sure I can do this.

Read up on NSMenuItem enabling.
And the different approaches.

https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/MenuList/Articles/EnablingMenuItems.html

cheers tecnomorph, appreciated

making some great headway now, mostly bindings for this purpose, the result is pleasing and manageable while I learn more about menItem’s etc etc

current application's NSMenu's popUpContextMenu:(subConMenu) withEvent:(aMenuItem) forView:(theView)