0

I have this chunk of code:

on run
    delay 10
    set lidState to do shell script "ioreg -r -k AppleClamshellState | awk -F \" = \" '/AppleClamshellState/ {print $2}'"
    if lidState = "Yes" then
        tell application "Finder" to sleep
    end if
end run

What I want it to do (at least for the first step) is to wait 10 seconds, and then sleep my computer if my lid is closed by the end of those 10 seconds. Is my script right, or am I missing something?

  • If your system is in a default configuration and not using anything to keep it from sleeping, e.g. external display and keyboard attached, then it probably is sleeping immediately when you close the lid and the script is being suspended and not completing as expected. I tested it on my MBP and that's appears to be what's happening. – user3439894 Mar 3 '16 at 3:53
  • The thing is, my Mac doesn't sleep when I close the lid (see my other question), which is why I need this script to work. – atirit Mar 3 '16 at 3:55
  • I've tested your code on my BMP by itself and with an external display and keyboard attached and closing the lid immediately after running the code. Without the external display and keyboard attached the program is suspended when the lid is shut and doesn't complete as expected, until I wake it up. However with an external display and keyboard attached and I run the program and close the lid it runs as expected putting the MBP to sleep after the 10 sec delay. Place a say command, e.g say "going to sleep" in the script in the if block so you can hear if it triggers. Do you hear it say? – user3439894 Mar 3 '16 at 4:25
  • Yes, I do. I didn't the first time I tried it but that was just my volume muted :-) So it does trigger the if thing... Maybe it's me saying Finder instead of computer or something else? – atirit Mar 3 '16 at 4:29
  • tell application "Finder" to sleep is proper. If it's triggering and not actually sleeping, then something is either intercepting the call or blocking the system from sleeping. Have your reviewed the system.log for any clues? – user3439894 Mar 3 '16 at 4:38

You must log in to answer this question.

Browse other questions tagged .