2

Is it possible via Automator, an app, applescript, etc. to automatically press a key (i.e. "g") every 60 seconds?

2 Answers 2

5

You can do this in AppleScript, iff you've enabled access for assistive devices:

tell application "System Events" to keystroke "," using command down

This will (obviously) invoke cmd-,.

If, for some reason, you can't enable access for assistive devices, then it's pretty easy to do this in code (by creating a CGEventRef and then posting it to the system, essentially). If you'd like help with that, then head over to stackoverflow.com, where that question has been asked a few times.

As for getting this to happen every 60 seconds, you could turn it into a launch agent and let launchd do it for you:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
 "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.stackexchange.apple.12692</string>
    <key>Program</key>
    <string>/usr/bin/osascript</string>
    <key>ProgramArguments</key>
    <array>
        <string>osascript</string>
        <string>-e</string>
        <string>tell application "System Events" to keystroke "," using command down</string>
    </array>
    <key>ServiceDescription</key>
    <string>Auto Keypress</string>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Second</key>
        <integer>0</integer>
    </dict>
</dict>
</plist>

Toss that in ~/Library/LaunchAgents and you should be good to go (once you load the plist and/or log out and log back in).

7
  • @Dave thanks for your reply, unfortunately I can't seem to get this working. I've enabled access for AD and saved your XML as a .plist file in my USER/Library/LaunchAgents folder. Logged out back in, opened TextEdit and let it run for a couple minutes. No comma "," key was pressed. What am I doing wrong?
    – Ryan
    Apr 25, 2011 at 1:16
  • @Ryan it's possible I might have messed up the plist slightly, but it seems to work for me on my system. You can also try doing launchctl load ~/Library/LaunchAgents/filename.plist in Terminal to explicitly load it. Apr 25, 2011 at 1:38
  • @Dave - that did the trick. Thank you! One last question: how can I change it from "Command"+"," to simply "g"? I tried editing the .plist file but nothing seems to change the behavior.
    – Ryan
    Apr 25, 2011 at 6:29
  • @Ryan when you change the plist, you'll have to "unload" it and "load" it again. Apr 25, 2011 at 13:53
  • @Dave How exactly do I do that? Just change your Terminal command (above) from "load" to "unload"?
    – Ryan
    Apr 25, 2011 at 17:16
1

Use the "Watch Me Do" to enter "G," then use the pause action to pause for 60 sec, then the loop action and select "Loop Automatically."

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .