I’m running 10.4.2 with all the recent updates and I can’t get this Prefab script to work…it falls down on the line “set value of attribute “AXFocused” of text field 1 to true”. I have tried various combinations e.g. replacing true with one, set focused to true etc. but with no avail.
I keep getting NSReceiverEvaluationScriptError: 4 error
Is it just muppetry on my part?
– This script demonstrates how to fill in a text field on a Web site, using Safari 2.0. Setting the value of the text field does not “stick”. Instead, assign keyboard focus to the text field and then send the text to the target application using the ‘keystroke’ command.
– Written by Bill Cheeseman, PreFab Software, Inc., April 2005
get system attribute "sysv"
if result is greater than or equal to 4160 then -- 4160 is Mac OS X 10.4.0
tell current application to set searchString to text returned of (display dialog "Search Google for:" default answer "")
tell application "Safari"
activate
open location "http://www.google.com"
delay 1
end tell
tell application "System Events"
tell process "Safari"
tell group 4 of UI element 1 of scroll area 1 of group 2 of window "Google"
set value of attribute "AXFocused" of text field 1 to true
keystroke (searchString)
click button "Google Search"
end tell
end tell
end tell
else
tell current application to display dialog "This script requires Mac OS X 10.4 Tiger or newer" buttons {"OK"} default button "OK"
end if
Not really, tricky999. With error dialogs as informative as that, how could anyone (outside Apple) possibly be expected to figure out the problem?
The script frequently works here - but not always. I think this could be down to a timing issue, causing the “AXFocused” command to be sent before the target UI element has completed loading. If this is the case, then issues such as internet connection and traffic could have an effect. (I can reproduce the problem fairly consistently here - but only if, before running the script, I clear Safari’s cache and quit it.)
One way around this might be to introduce a checking routine that delays the critical command until the web page is ready. (This approach also seems to remove the need for the earlier 1-second delay):
get system attribute "sysv"
if result is greater than or equal to 4160 then -- 4160 is Mac OS X 10.4.0
tell current application to set searchString to text returned of (display dialog "Search Google for:" default answer "")
tell application "Safari"
activate
open location "http://www.google.com"
end tell
tell application "System Events" to tell process "Safari"
tell group 4 of UI element 1 of scroll area 1 of group 2 of window "Google"
tell text field 1
repeat until exists
delay 0.2
end repeat
set value of attribute "AXFocused" to true
end tell
keystroke (searchString)
click button "Google Search"
end tell
end tell
else
tell current application to display dialog "This script requires Mac OS X 10.4 Tiger or newer" buttons {"OK"} default button "OK"
end if