question on accessing background processes

hello there,
I have just started with applescripting, and already have run into a wall. I want an applet to be able to send commands to an application in the background.

here is what I want to do:

tell application "System Events" 
	tell application "Text Edit"
		keystroke "hello world!"
	end tell
end tell

but for some reason, I cannot get around adding activate before the keystroke command. anyone have any suggestions?

Every scriptable application has its own set of common and unique scripting methods. This provides for a multitude of methods to perform the same function. For example, here is one way you could script your proposed TextEdit background function:

tell application "TextEdit"
	make new document
	set text of front document to "hello world!"
end tell

Another way to do the same thing would be like this:

tell application "TextEdit" to set text of (make new document) to "hello world!"

For a script like this, you do not need System Events. In many cases, you only need System Events when an application has limited or no scriptability.

In the unScripted section at MacScripter, we have a variety of archived articles and tips on beginning Applescript, as well as other topics. You would certainly benefit by spending some time there and searching around the site for other tutorials.

Hope this helps, and good luck!

Good stuff, Craig. Just for good measure, here’s yet another (slightly faster) variation:

tell application "TextEdit" to make new document with properties {text:"hello world!"}

ok, but I was just using text edit as an example, and unsuccessfully as I can plainly see. The application I am attempting to script is unscriptable, and probably because its not a very widely used program. How do I use system events to send text to the program without bringing it to the front? I am still unable to figure it out.

thanks for the tips tho, as they will help me in my future scripting projects, just for now, I have to focus on doing this.

No problem, what is the program? There is always the chance that someone here has some experience with it.

Oh, and kai, thanks for the cool insight on the one-line-record-tell. Am I to assume that this style of one-liner (phrased as a record) is typically the fastest way to do various things?

alright, the application is ventrilo… is there really no easy way to send keystrokes or key codes to a non scriptable application in the background?

I want to be able to set a comment automatically, I can work out accesssing the UI elements, but I cant get it to do anything while its in the background. I play games while I use it, and those require full-screen accessibility, where ventrilo is not easily pulled up front without bugging the viewer out.


I don’t currently use Ventrilo, Ken ” but, going back to the TextEdit example, you can use System Events to set the text of its front window something like this:

tell application "System Events" to tell text area 1 of scroll area 1 of window 1 of application process "TextEdit" to if exists then set value to "hello world!"

That’s a reasonable assumption, Craig ” since the method generally reduces the operations (not to mention the external application calls) required to achieve a given aim. In fact, apart from syntax, there’s little difference between the two scripts that you suggested. They both tell TextEdit to:

Since the one-line version executes the statement in parentheses first, and then goes on to complete the rest of the line, it performs the same functions as the multi-line version. This is borne out by Script Editor’s Event Log which, in both cases, returns:

The variation that I suggested performs the entire operation in one hit. Again, from the Event Log:

With this type of operation, timing can be subject to a variety of factors. Nevertheless, the latter version generally executes here about 50% faster than the earlier examples.