Declaring and passing non-existent objects

I am working to make a script to interact with flash and in doing so I am forced to deal with non-existent objects. That is, at any point in time the object may or may not exist. I started out by writing a very rough conceptual program to make sure I could successfully timeout until objects exist and then click on them and through some effort I got that to work. However, I want modularity in the program and need to find a way to pass non-existent objects into a function that waits for the object to exist.

An example of what I mean -

This is the code that does not work - I get the error that the passed object does not exist or it does exist and immediately returns true. Alternately if I wrote out the function LoadObject each time I used it then it does work - in a very lengthy manner.

on log_event(themessage)
	do shell script "echo " & themessage & ¬
		" >> ~/Library/Logs/AppleScript-events.log"
end log_event

on LoadObject(targetObject, targetName, loadThreshold)
	set loadTime to 0
	log_event("Loading " & targetName & "... " & loadTime)
	repeat with loadTime from 0 to loadThreshold
			if targetObject exists then
				log_event("Loaded " & targetName)
				return true
				delay 1
				log_event("Loading " & targetName & "... " & loadTime)
			end if
		end try
	end repeat
	log_event("Failed to load " & targetName)
	return false
end LoadObject

display dialog LoadObject(window 2 of application "Safari", "Safari", 5)

Furthermore, in this code, I get the error that the object does not exist and cannot pass it

tell application "System Events"
set theElement to UI element 1 of scroll area 1 of group 2 of window "Window-Name" of application process "Safari"
end tell

Any tips would be much appreciated!

You might want to give a variable a dummy value (like false) that can be passed until the object exists, at which point the variable is assigned to the object.

Hi, jester831.

The last line in your first script asks Safari to resolve the index reference window 2 of application “Safari” in order to pass the object to the handler. (The index in this case is 2.) Safari would normally respond with an ID reference (an absolute number preceeded by id), but if it doesn’t have at least two windows open, the index reference causes an error and stops the script before the handler’s called.

You could “freeze” the index reference by using the a reference to operator. That way, Safari doesn’t try to resolve the reference until the exists line in the handler. Note that exists is an application keyword, not an AppleScript language keyword. It’s executed by Safari because the reference belongs to Safari and Safari implements exists. If you pass a reference belonging to an application that doesn’t implement exists, that line will error too.

display dialog LoadObject(a reference to window 2 of application "Safari", "Safari", 5)

Wow. One of the very first things I did was try adding as reference and that didn’t work. Why is a reference to window 2 of application "Safari different than window 2 of application "Safari as reference?

Hmm, well this certainly is curious. It would seem that the reference operator only stops Safari from resolving a reference. In order to pass flash elements, I need to have System Events skip it’s reference operator. Any ideas?

tell application "System Events"
	display dialog LoadObject(a reference to group 2 of window 1 of application "Safari", "Safari", 5)
end tell

Ultimately I’d like to pass something like

LoadObject(a reference to group 12 of UI element 1 of scroll area 1 of group 10 of UI element 1 of scroll area 1 of group 2 of window 1 of application process "Safari", "Flash Element", 30)

Because there’s no coercion to reference in AppleScript, just the reference-preserving a reference to operator.

The reference is incomplete here. application process “Safari” belongs to application “System Events”. But just sticking of application “System Events” on the end doesn’t compile for me. It has to be something like this:

tell application "System Events" to set objectRef to a reference to group 12 of UI element 1 of scroll area 1 of group 10 of UI element 1 of scroll area 1 of group 2 of window 1 of application process "Safari"

LoadObject(objectRef, "Flash Element", 30)