Choosing a specific Text Field to focus text input.

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)

Try:

shotFileName’s window()'s makeFirstResponder_(shotFileName)

or if your window has an outlet to it like theWindow, you can just use theWindow’s makeFirstResponder_(shotFileName)

Ric

Thank you for the quick reply!

I did bind the text field to the window as an initialFirstResponder as a reference outlet.

I tried both suggestions, and neither one of them worked…

I’m not getting any messages in the console when it’s running that code, so there is no error message to refer to.

Where in your code did you put the method I posted?

Ric

Inside a subroutine.

The code before and after it gets executed.

Inside the subroutine that’s called by the button push you mentioned? That’s where it should go.

Ric

Yes - that’s where it was…

And actually, your code is working perfectly. User error on my part. :slight_smile:

Thank you, sir. I greatly appreciate your help.

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:

set textReturned to text returned of result

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?

Hello.

I assume you are not using AsObjC.

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.

Consider that in many cases you could pass the search term directly in the URL.
For example

http://www.google.com/search?q=macscripter

If the search term contains spaces or special characters it must be URL-encoded

I couldn’t get UI Browser to find the field


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

Thanks you for your help.

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! :smiley:

I am glad it helped, though it wasn’t much help though. :slight_smile: Sometimes it helps with another angle.

Thanks for sharing your script Skilet