Sunday, September 22, 2019

#1 2009-06-23 05:07:20 pm

bulbous
Member
Registered: 2009-06-23
Posts: 2

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:

Applescript:


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


Filed under: System

Offline

 

#2 2009-06-23 08:50:13 pm

chrys
Member
From:: McKinney, TX, USA
Registered: 2007-06-26
Posts: 442

Re: sending keystrokes to a non-scriptable background application

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

Notice that the 'keystroke' command does not have to be sent to a specific
application process. System Events will send it to the frontmost
application, automatically.


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").


--
Chris

Offline

 

#3 2009-06-24 11:57:01 am

bulbous
Member
Registered: 2009-06-23
Posts: 2

Re: sending keystrokes to a non-scriptable background application

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.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)