Wait for window to be activate and "ready to go"?

I’m not an expert at all when it comes to AppleScript, but I can understand a few things when I look at the code. I downloaded this script to activate a few keystrokes, wait a few seconds and then perform the Select All operation.

tell application "Finder" to activate
tell application "System Events"
	keystroke "d" using {command down, option down, control down}
	delay 5
	keystroke "a" using {command down}
end tell

Now instead of waiting 5 seconds, is there a way to make it check if the window is ready to go, meaning, it’s upfront and ready to accept the “Select All” operation?
My goal is to perform that operation, regardless of Finder taking 1 second to open or 30 seconds.

Hope it makes sense. Thank you!

Model: MacBook Pro 2012
AppleScript: 2.11 (208)
Browser: Firefox 99.0
Operating System: macOS 12

This should work for you.

tell application "Finder" to activate
tell application "System Events"
	set frontmost of application process "Finder" to true
	repeat until application process "Finder" is frontmost
		delay 0.1
	end repeat
	keystroke "d" using {command down, option down, control down}
	delay 0.1
	keystroke "a" using {command down}
end tell
1 Like

Finder has an Applescript dictionary. If you tell us what “keystroke “d” using {command down, option down, control down}” does on your computer, there might be a better way to accomplish whatever you’re doing than UI scripting it.