0

I want to press a certain key (e.g. 'p') in a certain time (e.g. at 00:00), how can I accomplish this? I'm aware of Can a Mac be programmed to simulate pressing a key at a certain rate via software? but this doesn't include a timer of any kind.

P.S. I'm running at macOS 10.13 High Seirra

nohillside
  • 100,768

2 Answers2

0

at + cliclick

You can use cliclick to execute your key presses and at (ships with macos) to schedule the execution of the command.

echo 'cliclick kp:"volume-up"' | at 09:00

If you want to schedule an event like to occur at some regular interval (e.g. some time every day, on Mondays, the first of each month, every ten minutes) you can use crontab. Open up the crontab list of jobs with crontab -e in a terminal. Then put in an entry like:

0 0 * * * cliclick kp:"volume-up"

Here are the man pages for at and cron if you want to see more details about how to get the behavior you want.

but first!

If you can run other at jobs but using cliclick with one is causing problems, just write a shell script with the command in it and run that with at. e.g:

echo 'zsh ~/clicktest.sh' | at 13:00

I also had problems just getting at to work at first. Apple seems to have made it pretty difficult to use at. You have to run the following command as root to enable the atrun program (which processes at jobs):

launchctl load -w /System/Library/LaunchDaemons/com.apple.atrun.plist

(To run it as root use sudo su to switch to root and then switch back with exit)

... just in case

Just to be safe I altered the file from the launchctl command - /System/Library/LaunchDaemons/com.apple.atrun.plist - changing the entry "Disabled" to false.

Since this is a read-only volume you need to have SIP turned off and then use mount -uw / to make it writable. Then you have to change the permissions on the file to make it writable. e.g.

chmod 777 /System/Library/LaunchDaemons/com.apple.atrun.plist

Then change the permissions back to their previous state or the OS will complain when it goes to load the plist again:

chmod 644 /System/Library/LaunchDaemons/com.apple.atrun.plist

I don't think you need to do all this I just did it because I was worried it might not launch atrun automatically on boot unless I changed the plist.

  • How should I write it in the terminal? I have already installed xcode brew and cliclick and at. When I tried your command it doesn't execute the cliclick argument but it does execute the at part. – Krist Dave Ferrer Feb 05 '21 at 16:03
  • @KristDaveFerrer I didn't realize there is some weirdness (intentional obstacles put in place by Apple) trying to use at on macos. I figured out how to get it working though and am editing the answer now. – yomotherboard Feb 06 '21 at 07:38
  • @KristDaveFerrer Verified your issue on my machine and got it working correctly, I added the instructions to enabled at (disabled by default on Macos) in the section "but first!" – yomotherboard Feb 09 '21 at 03:20
  • @KristDaveFerrer and by the way if you want to type charcter keys (e.g. "a", "s", "d", "f", ...) then you use the t command of cliclick and if you want to hold down a modifier key you use the kd (keydown) and ku (keyup) commands. For example cliclick kd:"cmd" t:"ac" ku:"cmd" will execute ⌘A then ⌘C. – yomotherboard Feb 09 '21 at 03:23
  • It still can't execute the at command (e.g. echo 'hello world' | at 6:00 PM) after enabling the at command (executed sudo su in the terminal then executed the launchctl command). – Krist Dave Ferrer Feb 10 '21 at 11:03
  • Tried to enable the at command again (by using launchctl command) and the terminal said "the service is active" or something. It still can't execute an at command. – Krist Dave Ferrer Feb 10 '21 at 11:06
  • @KristDaveFerrer Did you try what I wrote in the "just in case" section? The run the launchctl command again. Also did you run the launchctl command as root (not superuser)? – yomotherboard Feb 13 '21 at 15:23
  • When I tried the "just in case" section, the terminal said "operation not permitted" from the command "mount -uw /" and the chmod 777 part. Both commands result to "operation not permitted". I also tried it using sudo su – Krist Dave Ferrer Feb 16 '21 at 15:28
  • @KristDaveFerrer You have to turn off System Integrity Protection for it to work. You can turn it back on once you have made the change if you want to keep it on. It basically prevents writing to system files. I keep it off on my machine. – yomotherboard Feb 16 '21 at 17:41
-1

this AppleScript should do the trick:

—- time of day to press key is given in seconds
set timeToExecute to 70560 -- time in seconds =19h36
set currDate to current date

-- do nothing on Saturday and Sunday or this script is launched after 19h36 if weekday of currDate is in {Saturday, Sunday} or (time of currDate) > timeToQuit then return

delay (timeToQuit - (time of currDate)) -- wait

—- set the number of keypresses repeat 5 times

—- the “y” key is pressed tell application "System Events" to keystroke "y" using command down

—- every 1/10 of a seconds delay 0.1 end repeat end tell