Wait for Finder window to open before displaying dialog?

I’m working on a script that is designed to open a Finder window and then display a dialog. The relevant part of the code looks something like this (simplified):


set unixPosixPath to "/Users/someone/Documents" -- this variable is set earlier in the script
do shell script "open" & space & quoted form of unixPosixPath
-- something needed here to wait until the Finder window opens
tell me to activate
display dialog "Drag a file into the newly-opened window." buttons {"OK"} giving up after 10

As written, the Finder window opens after the display dialog command and hides it. I don’t want to insert an arbitrary “delay n” command. Is there a way to detect when this specific new Finder window has opened, so that I can then use the display dialog command?

Thanks for any advice on this one.

Hello. I’ll sketch it to put you in the right direction.

You will need to have a repat loop with a little delay inside, then you test if the posix path of the target window is the same as the posix path you opened, when it is so, then you have success and can exit the loop, and display your dialog.

The target of a finder window is an alias, and posix path is a text property of it.

This is as far as I’ve got, but I think the simplest part of it is the one part I can’t get right - how to extract the POSIX path from the alias. I shouldn’t be asking anything as simple as this, but I’m completely baffled:


set unixPosixPath to "/Users/somename/Documents" -- this variable is set earlier in the script
do shell script "open" & space & quoted form of unixPosixPath

tell application "Finder"
	repeat
		if exists window 1 then
			set targetAlias to the target of window 1
			set targetPosix to (the POSIX path of targetAlias) -- this is wrong!!!
			if targetPosix is equal to unixPosixPath then
				exit repeat
			end if
		end if
		delay 0.2
	end repeat
end tell

tell me to activate
display dialog "Drag a file into the newly-opened window." buttons {"OK"} giving up after 10

Any further help would be greatly appreciated.

OK, I figured it out with a little more effort. One thing that was important was that the path had to end with a forward slash:

set unixPosixPath to "/Users/edward/Documents/" -- this variable is set earlier in the script
do shell script "open" & space & quoted form of unixPosixPath

tell application "Finder"
	repeat
		if exists window 1 then
			set targetPosix to POSIX path of (target of window 1 as alias)
			if targetPosix is equal to unixPosixPath then
				exit repeat
			end if
		end if
		delay 0.5
	end repeat
end tell

tell me to activate
display dialog "Drag a file into the newly-opened window." buttons {"OK"} giving up after 10

Thank you again - this was exactly what I needed.

I am so sorry about it, I was totally sure that the target was an alias and not a reference.

I didn’t realize that you had told me anything wrong! Now I need to figure out the difference between a reference and an alias…!

Thank you again!

Excellent! Thank you!

EDIT: But doesn’t it need the repeat loop in the tell block? Your revision works when I test it, but I wonder if the repeat loop is completely irrelevant. Any insight gratefully received!

Hi,

why not let the Finder open the window?


set documentFolder to path to documents folder -- this variable is set earlier in the script

tell application "Finder"
	activate
	open documentFolder
	display alert "Drag a file into the newly-opened window." buttons {"OK"} giving up after 10
end tell

That is obviously the best way to do it! I got into the bad habit of using shell scripts for the sake of speed - but here the shell script only slows down the AppleScript!

I agree. Shell is usually slower, when it runs just one or two commands.
I recently began timing these things, using the Laptime osax. Here’s an example:

property loopCount : 100
set looptimes to {}
set tm to start timer

repeat loopCount times
	set thisLocale to do shell script "defaults read -g AppleLocale"
end repeat
set end of looptimes to lap time tm

repeat loopCount times
	set thisLocale to user locale of (system info)
end repeat
set end of looptimes to lap time tm

looptimes --> typical result: {1400, 400}
-- Standard Addition is about 3 times faster

Much of the speed difference is probably caused by the overhead in do shell, which will be less important when you can give it much more to do.

The difference also depends a bit on where you run it. For example, in ASE I get about 1100/200, and as a script applet I get about 1100/180. But if I save the same code as a Cocoa-AS app from ASE, I get 1200/75.

I also tried this saved as a Cocoa-AS app from ASE:

property loopCount : 100
set looptimes to {}
set tm to start timer

repeat loopCount times
	set thisLocale to do shell script "defaults read -g AppleLocale"
end repeat
set end of looptimes to lap time tm

repeat loopCount times
	set thisLocale to user locale of (system info)
end repeat
set end of looptimes to lap time tm

repeat loopCount times
	set thisLocale to current application's NSLocale's currentLocale()'s localeIdentifier() as text
end repeat
set end of looptimes to lap time tm

set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {", "}
set x to looptimes as text
set AppleScript's text item delimiters to saveTID
display dialog x
tell me to quit

The results were more like 1200/75/13.