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
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…
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.