can a choose dialog window be dismissed/closed without user input

Hi,
This seems a silly question :rolleyes:

try
	with timeout of 5 seconds ” just for testing
		set theItems to choose from list {"2", "3", "4"} with title "abc"
	end timeout
on error
	error number -128 --> (error "User canceled." number -128 from «script» to item) as expected,
	-- but the choose dialog window is left open.
end try

How can the choose dialog window be dismissed/closed without user input?

I searched this forum (probably not well enough ;)) but found no answer to this. Plenty about timeout error, but nothing about automatic dismissal of the choose dialog upon scripted error # −128.

Please, does anybody have a clever solution, even if to my shame, a well known one?

Well, it is fairly easy to achieve with a dialog, but a dialog has giving up after.

I have never done this with a list box, but I think I know of a way that may work. (This is not something that is easy to work in all situations, then you would have to know how finder is configured in spaces.

The sceheme is as follows.

Tell app “finder” : do everything in a finder tell block, tell it first to activate.
Then: use the code like you have it. within the finder tell block.
When a timeout have occured, then tell app Finder to keystroke esc.

If you can’t make something like that to work, then I really don’t know how.

This works for me in Mountain Lion:

tell application "Finder"
	try
		with timeout of 5 seconds -- just for testing
			set theItems to choose from list {"2", "3", "4"} with title "abc"
		end timeout
	on error
		error number -128 --> (error "User canceled." number -128 from «script» to item) as expected,
		-- but the choose dialog window is left open.
	end try
end tell

try
	with timeout of 5 seconds -- just for testing
		set theItems to choose from list {"2", "3", "4"} with title "abc"
	end timeout
on error errMsg number errNbr
	if errNbr = -1712 then
		tell application "System Events" to tell (first application process whose frontmost is true)
			key code 53
		end tell
	end if
end try

May do the trick.

Yvan KOENIG (VALLAURIS, France) mardi 12 février 2013 17:42:29

I found this… http://macscripter.net/viewtopic.php?id=29898
I think this corrects it

It doesn’t, because choose from list doesn’t have a giving up after clause.

No, it doesn’t for me. It’s the same result as in my initial post. I too have an iMac and 10.8.2.

It certainly does the trick but, only if the choose dialog stays the frontmost window.
If the user clicks on another app or finder window, then the dialog is left open. And that is what actually happens. Otherwise the user could simply wait and click on Cancel - and many don’t. That’s why I need a scripting solution.

Maybe, the dialog window needs be called by name, promoted and then closed but, I can’t get the syntax right to get the window “abc”. Can you help please?

Cheers, Chris

Here is an edited version :


set dialogName to "abc"
set theApp to name of current application
try
	with timeout of 5 seconds -- just for testing
		set theItems to choose from list {"2", "3", "4"} with title dialogName
	end timeout
on error errMsg number errNbr
	if errNbr = -1712 then
		activate application theApp
		tell application "System Events" to tell application process theApp
			tell window dialogName
				click button 2
			end tell
		end tell
	end if
end try

Yvan KOENIG (VALLAURIS, France) mardi 12 février 2013 21:44:54

Many thanks Yvan. It works perfectly.

Cheers, Chris