How to make window with list in focus when launched from Script Menu or osascript?

I do something like this:

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 have tried

tell me/System Events/Finder to activate

but it made no difference.

Neither of those are relevant in this scenario.

Try

tell application "SystemUIServer" to activate

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 activate System 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.

I did

tell application SystemUIServer to activate|
set selectedOption to choose from list options with prompt Please select an option:|

But it didn’t work. The dialogue/window is still not in focus.

Are you sure? It doesn’t work for me when I execute it using osascript.

Didn’t work when I tried running it from the command line/with osascript.

Mockman’s suggestion worked perfectly for me.

Screen recording here:

1 Like

"I think it’s probably an issue with TCC permissions in Accessibility settings.

Try checking:
System Settings > Privacy & Security > Automation
x-apple.systempreferences:com.apple.settings.PrivacySecurity.extension?Privacy_Automation

System Settings > Privacy & Security > Accessibility
x-apple.systempreferences:com.apple.settings.PrivacySecurity.extension?Privacy_Accessibility

Maybe the script menu just doesn’t have the necessary permissions?
Please check! :blush:"**

–add

(post deleted by author)

4 Likes

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! :blush:

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?

Yes, You would have to create a list of routine names, and have a corresponding if-then-else if-else if…end if statement.

p.s. Did the loginwindow solve the problems for you? It does for me.

The only script here that I tried was yours, and the “loginwindow” functioned perfectly.

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.
20250331-042032-1_668x234-2

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

Just a heads-up! Hope this helps

Did you try my suggestion above about using ‘loginwindow’

Who are you addressing?

Sorry, I was addressing IceFole