Hi @Lucky_Magician
Try this script from Script Debugger and let me know if it can help.
It should display a unique window with only a pop-up menu, an OK button, a second button and a cancel button. The window title, the button names and the popup menus are customizable.
When the window displays, the pop-up menu is open (just as if you clicked on it).
use framework "Foundation"
use framework "AppKit"
use scripting additions
-- ------------------------------------------------------------------
property theWindow : missing value
property theCancel : missing value
property theSecond : missing value
property theOK : missing value
property thePopup : missing value
property theField : missing value
-- ------------------------------------------------------------------
property globalResult : missing value
property exitCode : missing value
-- ------------------------------------------------------------------
-- buttons titles
set windowTitle to "Pop-up Menu"
set okButton to "OK"
set secondButton to "Second Button"
set cancelButton to "Cancel"
-- field content when no menu has been selected
set emptyChoice to "Empty Choice"
-- menu items
set menuList to {"CacheDelete", "Caches", "CardKit", "Classroom", "Colors", "ColorSync", "Components", "Compositions", "ConfigurationProfiles", "CoreAccessories", "CoreImage", "CoreServices", "CryptoTokenKit", "DefaultsConfigurations", "Desktop Pictures"}
-- ------------------------------------------------------------------
my performSelectorOnMainThread:"createWindow:" withObject:{menuList, emptyChoice, windowTitle, okButton, secondButton, cancelButton} waitUntilDone:true
-- ------------------------------------------------------------------
-- click on main button
if my exitCode = 1 then
say "your choice is: " & (my globalResult)
return (my globalResult)
end if
-- click on second button
if my exitCode = 2 then
say "you have pressed the second button"
end if
-- ------------------------------------------------------------------
on createWindow:theArgs ## build the window and its content
set {menuList, choosenMenu, winTitle, okButton, secondButton, cancelButton} to theArgs as list
set {winW, winH, winHI, winVI, butW, butH} to {560, 120, 20, 10, 120, 32}
-- build the Main Field
set theField to current application's NSTextField's alloc()'s initWithFrame:(current application's NSMakeRect(winHI, (winH / 2) - 20, (winW - butW - (winHI * 2)), 40))
tell theField
its setPlaceholderString:choosenMenu
its setFont:(current application's NSFont's labelFontOfSize:32)
its setTextColor:(current application's NSColor's controlAccentColor())
its setEditable:false
its setBordered:false
its setDrawsBackground:false
its (cell()'s setWraps:false)
its (cell()'s setLineBreakMode:5) -- NSLineBreakByTruncatingMiddle
its setFocusRingType:1
end tell
-- build the Popup button
set anArray to current application's NSArray's arrayWithArray:menuList
set thePopup to current application's NSPopUpButton's alloc()'s initWithFrame:(current application's NSMakeRect(winHI, (winH / 2) - 20, (winW - butW - (winHI * 2)), 40)) pullsDown:false
tell thePopup
its setTarget:me
its setAction:("buttonClicked:")
its setAutoenablesItems:false
its setTransparent:true
-- check if choosen menu is in meniList
set theSetA to current application's NSSet's setWithArray:{choosenMenu}
set theSetB to current application's NSSet's setWithArray:anArray
if ((theSetA's isSubsetOfSet:theSetB) as boolean) then
theField's setStringValue:choosenMenu
else
theField's setPlaceholderString:choosenMenu
end if
-- build the menu
repeat with anEntry in anArray -- menu construction reworked to allow menu separators
if (anEntry's hasSuffix:"-***") then
(thePopup's |menu|()'s addItem:(current application's NSMenuItem's separatorItem()))
set anEntry to (anEntry's substringToIndex:((anEntry's |length|()) - 4))
(thePopup's addItemWithTitle:anEntry)
((thePopup's itemWithTitle:anEntry)'s setEnabled:false)
else if anEntry as text = "" then
(thePopup's |menu|()'s addItem:(current application's NSMenuItem's separatorItem()))
else
(thePopup's addItemWithTitle:anEntry)
((thePopup's itemWithTitle:anEntry)'s setEnabled:true)
end if
end repeat
thePopup's selectItemWithTitle:choosenMenu
end tell
-- build the Cancel button
set theCancel to current application's NSButton's buttonWithTitle:cancelButton target:me action:"buttonClicked:"
tell theCancel
its setFrame:{{winW - winHI - butW, winVI}, {butW, butH}}
its setKeyEquivalent:(character id 27)
its setEnabled:true
its setTag:0
end tell
-- build the Other button
set theSecond to current application's NSButton's buttonWithTitle:secondButton target:me action:"buttonClicked:"
tell theSecond
its setFrame:{{winW - winHI - butW, winVI + 29}, {butW, butH}}
its setKeyEquivalent:tab
its setEnabled:false
its setTag:2
end tell
-- build the OK button
set theOK to current application's NSButton's buttonWithTitle:okButton target:me action:"buttonClicked:"
tell theOK
its setFrame:{{winW - winHI - butW, winVI + 58}, {butW, butH}}
its setKeyEquivalent:return
its setEnabled:false
its setTag:1
end tell
-- make the container view
set containerView to current application's NSView's alloc()'s initWithFrame:(current application's NSMakeRect(0, 0, winW, winH))
containerView's setSubviews:{theField, thePopup, theCancel, theOK, theSecond}
-- make the window
set theWindow to current application's NSWindow's new()
tell theWindow
its setDelegate:me
its setTitlebarAppearsTransparent:true
its setHidesOnDeactivate:false
its setTitle:winTitle
its setStyleMask:(1 + 2 + 16 + 128 + 32768) -- NSWindowStyleMaskBorderless = 0 ; NSWindowStyleMaskClosable = 2 ; NSWindowStyleMaskUtilityWindow =16 ; NSWindowStyleMaskNonactivatingPanel = 128 ; NSWindowStyleMaskFullSizeContentView = 32768 ;
its setContentView:(containerView)
its setContentSize:{winW, winH}
its setMovableByWindowBackground:true
its setLevel:8
its |center|()
end tell
-- display the window
my performSelectorOnMainThread:"windowDisplay:" withObject:theWindow waitUntilDone:true
delay 0.01 -- force the window to display before the next step in parent script
end createWindow:
-- ------------------------------------------------------------------
on windowDisplay:theWindow ## display modal window returning the button's tag
theWindow's makeKeyAndOrderFront:me
thePopup's performClick:me
set my exitCode to current application's NSApp's runModalForWindow:theWindow
theWindow's orderOut:me
end windowDisplay:
-- ------------------------------------------------------------------
on windowClose:theButton ## manage closing mode according to clicked button
set my exitCode to (theButton's tag())
(current application's NSApp)'s stopModalWithCode:(theButton's tag())
end windowClose:
-- ------------------------------------------------------------------
on windowWillClose:notif ## manage click on titlebar close button (do not rename this delegate method)
set choosenMenu to theField's stringValue()
set my globalResult to choosenMenu
set my exitCode to 0
(current application's NSApp)'s stopModalWithCode:0
end windowWillClose:
-- ------------------------------------------------------------------
on buttonClicked:sender ## retreive results according to clicked button
if sender's isEqual:theOK then
set my globalResult to (thePopup's titleOfSelectedItem()) as string
my windowClose:sender
else if sender's isEqual:theSecond then
set my globalResult to (thePopup's titleOfSelectedItem()) as string
my windowClose:sender
else if sender's isEqual:theCancel then
set my globalResult to missing value
my windowClose:sender
else if sender's isEqual:thePopup then
theField's setStringValue:(thePopup's titleOfSelectedItem())
theOK's setEnabled:true
theSecond's setEnabled:true
end if
end buttonClicked:
-- ------------------------------------------------------------------