How do I verify that there is a blinking textcursor?

I’m generating a text string with Applescript. this works fine. The result
is stored in MytextString.

Now I want to insert the text in the place where the cursor is blinking.

I was thinking of something pretty standard like:


tell application "System Events"
    tell front window
        keystroke MytextString
    end tell
end tell

If there is no blinking cursor I want to put up an error message (stating
that you first have to click in a text field, wordprocessor doc or whatever)

How do I verify that the focus is in a text-field?

Thanks in advance!

Vincent

This may depend on the app you’re trying to insert text into, but some times it’s easier calling the text field itself, such as:

tell application "System Events"
	tell process "Safari"
		tell text field 1 of group 1 of splitter group 1 of window 1 --> address text field
			set currentContents to value
			set value to currentContents & " MORE TEXT HERE"
		end tell
	end tell
end tell

This is usually more reliable than keystrokes.

Thanks for your insights!

The problem is that I’m not inserting the text into a specific application: it might be Word, Excel, iCal, a Filemaker database, anything…

So I really have to check for a text focus (blinking cursor).

Maybe I could just let the Applescript try to paste the text into the field and if that fails, have it put up an error message…

From what I’ve found, there’s no good way to capture whether text was actually placed or not. If a text field is not currently set as first responder in an app, most apps will silently fail rather than post an error. This is done for good reason, as you wouldn’t want to throw up one (or multiple!) errors if a user inadvertently started typing or hits a few keys when no field is focused. OSX has a feature where it attempts to save keystrokes in memory when an app can’t immediately handle them, which could lead to big “user experience” problems if you were to build an app that would throw up an error every time a key was pressed that couldn’t be written to a text field. It wouldn’t be a big deal if you type as slow as I do :lol:, but a speedy typist might have lots of “OK’s” to click if they got crazy into punching buttons without realizing they weren’t focused on a text field.

There are lots of variables involved in doing what you propose, which are all dependent on what application you’re pasting into. This topic has been covered many times before and has no good answer. Unfortunately all apps handle text differently and have very different interfaces, so it’s virtually impossible to write something for ALL apps. The best you may find is to simply set the clipboard to your string value, and then navigate to your app of choice and manually paste (cmd+v). Check out our comments in…

[url=http://bbs.applescript.net/viewtopic.php?t=9946]http://bbs.applescript.net/viewtopic.php?t=9946[/url]

Cheers,
j

Yes, I was also thinking of a Paste approach.

The keystroke approach I mentioned before works quite well though.
If there is no text field available it just does nothing, no crashes or weird behaviour with the apps I tested so far.

Maybe I should just forget about the error message I wanted to put up.

Thanks again!