The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   Applications (http://hintsforums.macworld.com/forumdisplay.php?f=5)
-   -   Hey anyone know how to simulate keys to an app?! (http://hintsforums.macworld.com/showthread.php?t=81618)

yalag 11-19-2007 07:08 PM

Hey anyone know how to simulate keys to an app?!
 
I have an app that requires a key press every 2 minute. Anyone know of a way to simulatte key press to an app without focus? In windows I used to use autoit a script language to send keys to apps. Anything like that mac?

trevor 11-19-2007 07:12 PM

Yes, Applescript is built in. There are some examples on the following page that shows how to simulate a key press with Applescript:

http://dougscripts.com/itunes/itinfo/keycodes.php

Trevor

yalag 11-19-2007 07:54 PM

thank you for your help but the problem is that method only worksfoe the frontmost application

hayne 11-20-2007 12:37 AM

There are various third-party utilities that provide "hot keys" or "key macros" - search on www.update.com

yalag 11-20-2007 12:54 AM

I'm not sure those hot keys programs does what I want, I think they watch your key presses and perform some actions? It doesnt actually send keys?

Las_Vegas 11-20-2007 01:21 AM

Quote:

Originally Posted by yalag (Post 426767)
thank you for your help but the problem is that method only worksfoe the frontmost application

You can send an Applescript command to any open application.

yalag 11-21-2007 06:16 PM

What is the code for applescript to send to any open application?

hayne 11-21-2007 06:54 PM

You need to specify which app you want to send the command to by starting with something like:

tell application "Safari"

yalag 11-21-2007 08:56 PM

what is the next line?

hayne 11-22-2007 12:06 AM

Quote:

Originally Posted by yalag (Post 427459)
what is the next line?

Look at the examples on the page linked to by trevor in post #2

yalag 11-22-2007 12:32 AM

No, try that, it doesnt work, those lines only work for frontmost applications, read that page

Quote:

System Events" will send a key code or a keystroke to the front application. So, you have to make sure that iTunes is frontmost.

hayne 11-22-2007 12:37 AM

The "activate" in the example scripts makes the app come to the foreground. You could put it back in the background afterwards.

Maybe you want to back up a bit and tell us why you need to send key presses to an app that is in the background - maybe we can help solve your higher-level problem.

yalag 11-22-2007 01:21 AM

Hayne, its an old app that requires me to press space every 2 min or so before it can continue. All I want to do is send a key, its not complicated. I dont want it to ever come to the foreground because I want to continue to use my computer with it doing its thing in the background.

hayne 11-22-2007 01:25 AM

Quote:

Originally Posted by yalag (Post 427520)
its an old app that requires me to press space every 2 min or so before it can continue

Yeah - but which "old app" is it? Maybe someone here knows a better solution.

yalag 11-22-2007 01:49 AM

Its an app that my company wrote, small. I wish someone could tell me how to send keys :(

bramley 11-22-2007 05:57 AM

Quote:

Originally Posted by yalag (Post 427525)
Its an app that my company wrote, small.

Edit the requirement for the space bar out of the source code and recompile the app.

yalag 11-22-2007 11:32 AM

so source code is provided. This cant be true, OSX CANNOT send keys to unfocused applications????

trevor 11-22-2007 11:36 AM

Hayne already explained that it can.

Post #4 in this thread includes an applescript by son_t that presses the keys that invert the screen, for example.

Trevor

yalag 11-22-2007 12:10 PM

Thanks but, so I tried that script and tried to change it to an targeted application and then it will give me an script error, saying syntax error

tell application "TextEdit"
tell application processes
key code 28 using {command down, option down, control down}
end tell
end tell

hayne 11-22-2007 06:12 PM

I think the first two lines need to be instead:
Code:

tell application "System Events"
        tell application process "TextEdit"

I.e. you are telling the System Events process to talk to TextEdit

yalag 11-22-2007 07:17 PM

Try this

tell application "System Events"
tell application process "TextEdit"
key code 28
end tell
end tell


Does not send a key to textedit


All times are GMT -5. The time now is 10:33 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.