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

2
  • 3
    There might be different ways to accomplish this. Which problem are you trying to solve with these automated key presses?
    – nohillside
    Jan 30, 2021 at 15:42
  • What have you tried? Have you experimented with using an AppleScript to press a key? – does this work for your situation? If so, are you seeking a way to run a script at a certain time? If not, how does your situation differ? Jan 30, 2021 at 18:33

2 Answers 2

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.

9
  • 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. Feb 5, 2021 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. Feb 6, 2021 at 7: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!" Feb 9, 2021 at 3: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. Feb 9, 2021 at 3: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). Feb 10, 2021 at 11:03
-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
1
  • 1
    Explanation?ㅤㅤㅤ
    – Thinkr
    Apr 11 at 20:45

You must log in to answer this question.

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