5

Is there a way to trigger the lid close action programmatically with a script or through terminal?

I am testing this by placing a small magnet near the SD slot on my MacBook Pro.

  • 3
    Does it have to be a "lid close action" specifically, or are you looking to programmatically put the machine to sleep? – TheWellington Dec 10 '13 at 19:18
3

There is no Apple supported programming interface to simulate the lid being closed.

You can put your Mac to sleep using this command line:

osascript -e 'tell application "System Events" to sleep'
  • Thanks for the answer, i was actually trying to get my mac to use external display as primary only. Upgrading to OSX Mavericks solved all my issues and it was free. – louisinhongkong Dec 21 '13 at 2:30
  • @louisinhongkong how did you get into clamshell mode without closing your laptop?! I'm on sierra – Pavan Mar 6 '17 at 22:52
3

You may put your machine to sleep with a shell script:

pmset sleepnow

P.S. If you intend to run this from a script, be prepared that machine won't sleep until script finishes, and if that's something like a daemon running in a loop, it won't ever work unless you run pmset in a subshell:

(pmset sleepnow &)

You must log in to answer this question.

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