sending keystrokes to a non-scriptable background application

The subject mostly states my question – I’m trying to send keystrokes to a background application that does not support applescript. It looks like I should be able to use the “System Events” application to do this, but I have tried something like this:


tell application "System Events"
	tell process "sdfsdf"
		keystroke "A"
	end tell
end tell

and it doesn’t work – no matter what I put in the quotes after process, the keystroke “A” ends up in the script editor window.

it seems like this should be very easy – I have looked around the site, and have been unable to find anything that addresses this specific issue.

In case it was not clear, I would like to send the keystrokes without making the process “sdfsdf” foreground, and I would also like to be able to pick a specific window that is open from that process to send the keys to.

I’m trying to port an application I wrote once upon a time on Windows to the mac – on windows, I basically just sent a keydown message to a specific window id.

Anyway, thanks much for any help,

Kem

When using keystroke my advice is to always use something like activate application “foo” before using keystroke and then hope that the user (or some other piece of software) does not make some other app frontmost before your keystrokes have been delivered.

If you are dealing with an application that does not respond to the activate command by making itself frontmost and if the application can not be made frontmost by some other means then you may not be able to reliably use keystroke. Then again in such a case how would a normal user ever be able to send the app actual keystrokes anyway? If there is some way, you could try to emulate such preparatory actions with other UI scripting means (clicking some UI element?).

As far as I have been able to determine, the tell application process “foo” part of calls to System Event’s keystroke command is ignored. Including it in the program text leaves the reader with the impression that simulated keystrokes can be targeted at individual applications. In my experience that it not what actually happens. The simulated keystrokes always go to the frontmost application. Any targeting to a process (or application process) is ignored as far as the keystroke command is concerned. That kind of targeting is essential when dealing with actual UI elements, but seems to be completely ignored for keystrokes.

In an email to the AppleScript users mailing list, from Bill Cheeseman wrote

Cheeseman does not state exactly what I claim above, but it does not rule it out. Certain interpretations could be taken as implying my claim (e.g. if there is an “always” implied between “System Events will” and “send”).

Thanks much for the detailed response chrys – it sounds like what I am wanting to do is not possible in Leopard yet, hopefully apple will enhance their UI scripting at some point in the future.