Can AS detect being in a text field?

That’s the big Q. Can Applescript detect if the cursor is in a text field? If it can’t, is there something that can be triggered thru AS to do so? Jave? Unix?

Hi,

AppleScript Studio can,
in plain AppleScript it depends on the target application

hmm, well the the target app is Safari and its a webpage (http://allmusic.com/)

I’m reading up on ASS, (that looks really bad:/) and haven’t gotten to that part yet. Any chance you could point me in the right direction regarding the studio script?

So right now I have the cursor tabbing to the text field but every so often, amount of tabs changes. Naturally this isn’t consistent enough and I’m hoping there’s a better way. Yay for GUI…:frowning:

Browser: Safari 523.10.6
Operating System: Mac OS X (10.5)

If you are talking about HTML form text fields, then maybe the last bit in this script will do something like what you want (tested with Safari 3 under 10.4.11):

tell application "Safari"
	make new document with properties {URL:"http://allmusic.com/"}
	set endTime to (current date) + 30
	repeat until (do JavaScript "document.readyState" in first document) is "complete"
		delay 5
		if (current date) is greater than or equal to endTime then error "Timeout waiting for Safari to load the document"
	end repeat
	-- The whole point of the readyState loop was to try to make sure it was ready to go. But, alas, it does not seem to always be enough.
	delay 5
	do JavaScript "document.forms['search']['search_txt'].focus()" in first document
end tell

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari 3.0.4 (523.12)
Operating System: Mac OS X (10.4)

Chrys, it does work and thank you! It gets the cursor to the text field and right now its magic to me. I definetly want to understand how that line of javascript works.
I cut it down to this and just start with the AMG (www.allmusic.com) window open.


tell application "Safari"
	do JavaScript "document.forms['search']['search_txt'].focus()" in first document
end tell

When I run the script, it plops the cursor in the text field, but then when I try to script in text to be entered it doesn’t like it and throws an error.
Safari got an error: Can’t get keystroke “searchText”.


tell application "Safari"
	do JavaScript "document.forms['search']['search_txt'].focus()" in first document
	keystroke "searchText"
end tell

Also, I’m trying to get my variable text to be entered in that text field where the cursor is. I tried a ton of variations, all not working. So I’m at a loss with this one. Here’s the last attempt I had…


tell application "Safari"
	do JavaScript "document.forms['search']['search_txt'].focus()" in first document
end tell

tell application "System Events"
	set searchText to the clipboard
	return text of searchText
end tell

I tried nesting the system events tell inside the safari tell, no luck.

Model: Dual 2 GHz PowerPC G5
AppleScript: 2.2
Browser: Safari 523.10.6
Operating System: Mac OS X (10.5)

Hi,

try this


tell application "Safari"
	activate
	do JavaScript "document.forms['search']['search_txt'].focus()" in first document
end tell
set searchText to get the clipboard
tell application "System Events" to tell process "Safari" to keystroke searchText

Sweet. That’s exactly what I needed. I changed the syntax only because my preference is to see this way. Though, if there’s a good reason not to do it this way, let me know!


tell application "Safari"
	activate
	do JavaScript "document.forms['search']['search_txt'].focus()" in first document
end tell
set searchText to get the clipboard
tell application "System Events"
	tell process "Safari"
		keystroke searchText
	end tell
end tell

I gotta say, I love this forum and all the help!

Model: Dual 2 GHz PowerPC G5
AppleScript: 2.2
Browser: Safari 523.10.6
Operating System: Mac OS X (10.5)