Just yesterday I was trying to write a script so that I could open a custom alert dialog and enter some text into a combobox (adapting some code I found elsewhere). It works fine when run from the Script Editor, but when I run it from the Script Menu, any text I try to enter goes to whatever app is behind the alert, not into the combo box itself (e.g., if the alert is over a Script Editor window, the text gets typed into SE at the current insertion point). Here’s the code I’m using:
use AppleScript version "2.4" -- Yosemite 10.10 or later
use framework "Cocoa"
use scripting additions
property ca : current application
property NSAlert : class "NSAlert"
property NSComboBox : class "NSComboBox"
property NSArray : class "NSArray"
set alert to NSAlert's alloc()'s init
alert's addButtonWithTitle:"Select"
alert's addButtonWithTitle:"Cancel"
set alert's informativeText to ""
set alert's messageText to "Enter text"
set alert's |window|'s floatingPanel to true
set cframe to ca's NSMakeRect(0, 0, 215, 26)
set combobox to NSComboBox's alloc()'s initWithFrame:cframe
set display_list to NSArray's arrayWithArray:{"alpha", "beta", "gamma"}
combobox's addItemsWithObjectValues:display_list
combobox's setCompletes:true
combobox's setHasVerticalScroller:true
-- place the combo box on the alert panel as an accessory
alert's setAccessoryView:combobox
set modalResponse to alert's runModal()
I’m sure this has something to do with the fact that NSAlert is app-modal, and the ‘app’ in this question is the background process that runs the Script Menu, but I can’t see how to get around it. Is there a trick I’m missing, or is this a lost cause? I mean, I know this is possible — ‘display dialog’ is a modal alert that takes text, even when run from the Script Menu — but I don’t know how ‘display dialog’ is constructed to achieve that.
I’ve tested with the standard NSSave and NSOpen alerts, and see the same problem. I suppose I could rewrite this as an applet and run that (which I’m sure would solve the problem), but still… Any insights?
P.s. Catalina, with a 2019 MacBook Pro, if that makes any difference…