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

I found the problem. This works:

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

But this doesn’t work:

set theFruitChoices to {"Apple", "Banana", "Orange"}
tell application "SystemUIServer" to activate
set theFavoriteFruit to choose from list theFruitChoices with prompt "Select your favorite fruit:" default items {"Apple"}

So activate and the dialog has to be within a tell-block.

3 Likes