set theFruitChoices to {"Apple", "Banana", "Orange"}
set theFavoriteFruit to choose from list theFruitChoices with prompt "Select your favorite fruit:" default items {"Apple"}
theFavoriteFruit
When I execute this from Script Editor the window with the list of items to select from is in focus but when I execute it from Script Menu or osascript it is out of focus and requires me to click in it to activate it.
How can I make it in focus when executed from Script Menu or osascript?
I cannot confirm that the solution provided by @Mockman is valid or not because I have never tested it. However, I do know for a fact that simply placing the activate command directly before any AppleScript âuser interactionâ type of commands (such as display dialog , display alert, or choose from list), will ensure that those dialogs do appear in front of everything else.
set theFruitChoices to {"Apple", "Banana", "Orange"}
activate
set theFavoriteFruit to choose from list theFruitChoices with prompt "Select your favorite fruit:" default items {"Apple"}
theFavoriteFruit
Hi lagr, nice to meet you.
Iâm not very good at English, so I might have misunderstood your question, butâŚ
You want to bring the list selection window to the front, right?
Iâm running this on macOS 15.3.1, so the behavior might differ if youâre on a different version.
Sorry for the slightly long code, but if you add use scripting additions and then activateSystem Events before calling it, I think it should come to the front.
Please give it a try!
I hope this helps.
#!/usr/bin/env osascript
use AppleScript version "2.8"
use scripting additions
set listChoices to {"Apple", "Banana", "Orange", "Dekopon(Japanese Orange)"} as list
#
set strName to (name of current application) as text
if strName is "osascript" then
tell application "System Events" to activate
else
tell current application to activate
end if
###
set strTitle to ("Select your favorite fruit:") as text
set strPrompt to ("Select one") as text
try
tell application "System Events"
#Activate is absolutely necessary
activate
set valueResponse to (choose from list listChoices with title strTitle with prompt strPrompt default items (item 4 of listChoices) OK button name "OK" cancel button name "I hate Monday" with empty selection allowed without multiple selections allowed)
end tell
on error
log "Error choose from list"
return false
end try
if (class of valueResponse) is boolean then
log "Cancel Response false You too?"
return false
else if (class of valueResponse) is list then
if valueResponse is {} then
log "empty selection Response"
return false
else
set strResponse to (item 1 of valueResponse) as text
end if
end if
----Self-reply
I misunderstood. That was simpler than I thought. Sorry about that.
set theFruitChoices to {"Apple", "Banana", "Orange"}
tell application "SystemUIServer"
activate
set theFavoriteFruit to choose from list theFruitChoices with prompt "Select your favorite fruit:" default items {"Apple"}
end tell
theFavoriteFruit
set theFruitChoices to {"Apple", "Banana", "Orange"}
tell application "System Event"
activate
set theFavoriteFruit to choose from list theFruitChoices with prompt "Select your favorite fruit:" default items {"Apple"}
end tell
theFavoriteFruit
Running both from the script menu, as well as from Terminal or VSCode, brings the list selection window to the front and keeps it active. My apologies.
After seeing this post, I started using Mockmanâs method with
tell application âSystemUIServerâ to call the UI within SystemUIServer,
and so far, itâs working really well.
But I wanted to add one thing I noticed.
When calling the UI within SystemUIServer
Iâve experienced frequent issues,
like Finder windows not being able to come to the front properly.
It seems like quitting SystemUIServer after handling the UI,
with
tell application "SystemUIServer" to quit
reduces the frequency of these issues in Finder.
Hope this helps!
set theFruitChoices to {"Apple", "Banana", "Orange"}
tell application "SystemUIServer"
activate
set theFavoriteFruit to choose from list theFruitChoices with prompt "Select your favorite fruit:" default items {"Apple"}
end tell
#ADD
tell application "SystemUIServer" to quit
I use âloginwindowâ for dialogs run from osascript or command line.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
on run
local listChoices, strName, strTitle, strPrompt
set listChoices to {"Apple", "Banana", "Orange", "Dekopon(Japanese Orange)"} as list
set strName to name of current application
set strTitle to "Select your favorite fruit:"
set strPrompt to "Select one"
tell application "loginwindow"
activate
set theFavoriteFruit to choose from list listChoices with title strTitle with prompt strPrompt default items (item 4 of listChoices) OK button name "OK" cancel button name "I hate Monday" with empty selection allowed without multiple selections allowed
end tell
end run
This has given me an idea. Would it be possible, rather than Apple, Banana, etc., to have a selection of script #1, script #2, etc., and then have the selected script activate and run?
About SystemUIServer
I noticed some interesting behavior and wanted to share an update.
With âchoose from listâ, everything works as expected. However, when using âdisplay dialogâ, thereâs an issue:
tell application "SystemUIServer"
activate
set strResponse to display dialog "Error 404: Coffee Not Found"
end tell
If a dialog is shown via âdisplay dialogâ and I activate Finder, the dialog loses focus. After that, clicking on the dialog doesnât always bring it back into focus.
It seems like calling âdisplay dialogâ from System Events might work better in this case.
#!/usr/bin/env osascript
use AppleScript version "2.8"
use scripting additions
set strName to (name of current application) as text
if strName is "osascript" then
tell application "System Events" to activate
else
tell current application to activate
end if
tell application "System Events"
activate
set strResponse to display dialog "Error 404: Coffee Not Found"
end tell