Finding the window for a specific Finder open command

When I use the “open” command in “Finder” (in a loop) to open a list of files, I believe “Finder” then issues a command to the default app based upon the type of file. The opening of the file by that app results in a window. In order to size and position that window, I need to unambiguously identify that specific window.

I’ve tried the usual solution (i.e. using “frontmost”), but it has proven to be somewhat unreliable. As an example of where this does not work well… If another app is running at the same time and tries to open the same type of file, the frontmost window could belong to either one.

I’d appreciate a code snippet to illustrate how to unambiguously determine the window that resulted from a specific open command in “Finder”, so I can position and resize the window.

Shane Stanley gave you part of the answer.

scheme 1

set theFile to (path to desktop as text) & "the encoding.numbers"


tell application "System Events"
	set theApp to name of default application of file theFile
end tell

set shortName to text 1 thru -5 of theApp
tell application shortName
	set nbw to count windows
end tell
tell application "Finder" to open file theFile
tell application shortName
	repeat until (count windows) = nbw + 1
		delay 0.1
	end repeat
	name of window 1
end tell

scheme 2
This one rely upon the fact that the script wait for the completion of the doc opening before triggering the application.

set theFile to (path to desktop as text) & "the encoding.numbers"

tell application "System Events"
	set theApp to name of default application of file theFile
end tell

set shortName to text 1 thru -5 of theApp

tell application shortName
	open file theFile
	name of window 1
end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 25 janvier 2019 18:05:30

Thank you Yvan.
I will see how these techniques work.

Yvan;

I ended up rewriting the script and managed to avoid the issue. I am now looking at some minor issues with the size of windows and some scaling of images. I’ve posted that in another thread, so I won’t repost it here.

Thank you again for your response.