I have an AppleScript OBj-C application that has a window containing 11 text fields for input.
I already have it set up, so that when the window first appears, the first text field, has the focus for the user to start typing in (which is what I want). At that point, the user can jump to any text field he/she wants.
The problem is that after clicking a button, and performing a specific action, I want to insure that the first Text Field has the focus again. Nothing I’ve found online has helped me up to this point.
The Text Field is called shotFilename
Any help here would be greatly appreciated. Thank you.
Model: Mac Pro, Xcode 3.2.5
AppleScript: 2.1.2
Browser: Safari 533.19.4
Operating System: Mac OS X (10.6)
How did you do this? I am trying to get this to work for places like amazon.com and bingiton.com and my current script doesn’t work. It works just fine for google, and bing where you start off on the search field when the page loads.
display dialog "Search Google" default answer "" buttons {"Cancel", "Search"} default button 2
copy the result as list to {textReturned} --Not sure why you need "buttonPressed" I even tried it and it works fine without it.
set search to textReturned
tell application "Safari"
launch
activate
open location "http://www.bingiton.com"
end tell
tell application "System Events"
delay 1
keystroke search
keystroke return
end tell
That second line is coercing a record to a list, which is an inherently dangerous thing to do – there is no guarantee of order in a record. Use something like:
Great thank you for the help. Any idea how to make this work with amazon or other sites that don’t start you in the right field when you land on the page?
I think I’d try to use System Events to send a tab so I ended up in the right field. assuming they have set taborder on the pages, or I’d use UI Scripting to set focus to the right textbox, which I think is doable.
display dialog "Search Amazon" default answer "" buttons {"Cancel", "Search"} default button 2
set textReturned to text returned of result
tell application "Safari"
launch
activate
open location "http://www.amazon.com"
delay 1
tell application "System Events" to key code 48 --Tab
tell application "System Events" to key code 48 --Tab
tell application "System Events" to key code 48 --Tab
(*
tell application "System Events" to keystroke "tab"
tell application "System Events" to keystroke "tab"
tell application "System Events" to keystroke "tab"
*)
end tell
tell application "System Events"
delay 1
keystroke textReturned
keystroke return
end tell
That works if it loaded in time depends on Internet and computer speed. The following might work better.
display dialog "Search Amazon" default answer "" buttons {"Cancel", "Search"} default button 2
set textReturned to text returned of result
tell application "Safari" to activate
open location "http://www.amazon.com"
if page_loaded(20) then
tell application "System Events" to key code 48 --Tab
tell application "System Events" to key code 48 --Tab
tell application "System Events" to key code 48 --Tab
--tell application "System Events" to keystroke "tab"
--tell application "System Events" to keystroke "tab"
--tell application "System Events" to keystroke "tab"
tell application "System Events"
delay 1
keystroke textReturned
keystroke return
end tell
else
say "Amazon Didn't Load"
end if
on page_loaded(timeout_value) -- in seconds
delay 1
repeat with i from 1 to timeout_value
tell application "Safari"
if name of current tab of window 1 is not "Loading" then exit repeat
end tell
--delay 1
delay 2
end repeat
if i is timeout_value then return false
tell application "Safari"
repeat until (do JavaScript "document.readyState" in document 1) is "complete"
delay 0.5
end repeat
end tell
return true
end page_loaded
I was curious as to whether UI Element Inspector would work or not, it did yesteryears, that’s why I recommended trying it. I have also just tried at at the text fields on the post at Macscripters, but it works there, I suppose it has something to do with the web-technologies involved.
When UI Scripting, I find it indispensable to set focus on something, that way it stays that way until I’m done!
I am glad it helped, though it wasn’t much help though. Sometimes it helps with another angle.