My query is very similar to: Can a Mac be programmed to simulate pressing a key at a certain rate via software? whereby need to have script, so the Macbook won't lock automatically (caffienate -t is not working due to policy). The keystroke also needs to be a safe key (like SCROLL LOCK2 or very passive key) which doesn't do any harm.

Do you know how to replace the keystroke of SCROLL LOCK2 or "fn" key via command

# below one just inserts "fn" characters, which  is dangerous 
echo "set i to 0
repeat while i < 15
set i to i + 1
delay 5
# Trying out fn key, but not sure what is the keystroke value of it?
tell application \"System Events\" to keystroke \"fn\"
end repeat" | osascript
  • If this is whilst unattended, why not just use a hot corner? – Tetsujin Jun 7 at 11:56
  • @Tetsujin, quite didn't get you. How can it send a "keystorke" or move the mouse automatically every 60 seconds? – diaryfolio Jun 7 at 12:30
  • You don't need to, if all you're trying to do is keep the Mac awake whilst unattended. Your question seems to be an XY Problem - You want to do X, & you think in order to achieve X you must do Y. However you don't know how to do Y, so you ask how to do that… when in fact what you needed to do was X. – Tetsujin Jun 7 at 12:33
  • Not actually true. The organisation policy does NOT allow me to change the sleep, power settings or how long it can be awake. So I've to simulate the key – diaryfolio Jun 7 at 12:37
  • I didn't suggest changing any of those. You're still on the Y of the XY problem. – Tetsujin Jun 7 at 12:43

I realize that this question was asking for a software solution, but given the comments on the question it sounds like this would be much easier with a hardware solution. With typical IT governance tools it may be effectively impossible to do this in software on the work laptop.

Without the software requirement, the clear solution to this issue is an auto clicker/tapper. You can get an "auto clicker" on Amazon for <$30. It plugs into USB and can tap at various rates.

Searching for auto clicker also brings up various software solutions, so you might try that out before trying to code this yourself or buy a hardware solution.

  • Ignoring for the moment that circumventing security measures often isn't a good idea: Isn't it rather cumbersome to work on a computer if the mouse auto-clicks every 30 seconds? – nohillside Jun 7 at 14:20
  • 1
    @nohillside, agreed. But one of the clients computer policy is not practical and locks out in 60 seconds ! by the time you stand and stretch it get's locked out. Security is a compromise with user friendliness – diaryfolio Jun 7 at 14:38

I'm going to add this as an answer anyway, though it wasn't given much credence in comments;)

Set a hot corner to 'Disable Screen Saver' then as you leave your desk, just push the cursor into that corner. If the screen doesn't sleep, neither does the Mac.
This requires no admin permissions as far as I'm aware. The screensaver control panel is not locked by default.

enter image description here


Below link gives the details of various keystrokes Also a nice list of key-codes https://css-tricks.com/snippets/javascript/javascript-keycodes/

Based on above, the idea to do programatically (via applescript) is

  • To create a script file with detail
  • Load it and run every 59 seconds (eg if the lockout period is 60 seconds)

Create applescript with scroll-lock press: 145_scrolllock.scpt

    tell application "System Events"
        key code 145 using {shift down, command down} -- shift-command-left
    end tell

Now create a plist: 145_scrolllock.plist

<?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">

Load it whenever you want and will execute automatically

launchctl load ~/somelocation/145_scrolllock.plist 
launchctl start 145_scrolllock
  • Let us know how that works out if you're typing at the same time... – Tetsujin Jun 13 at 14:14

You must log in to answer this question.

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