Applescript shell script won't run when display is sleeping

Discussion in 'Mac Programming' started by BlueRevolution, Feb 18, 2011.

  1. macrumors 603

    BlueRevolution

    Joined:
    Jul 26, 2004
    Location:
    Montreal, QC
    #1
    I wrote the following shell script to sleep my computer from the command line:

    /usr/bin/sleepy:
    #!/usr/bin/osascript
    tell application "system events" to sleep


    It works perfectly when run from Terminal or over a ssh connection while the computer is running normally, but while the display is asleep and the command is run over ssh, it does nothing, even though all other commands work fine. Since the point was to be able to sleep the computer remotely, that's a bit of a serious flaw.

    Any suggestions?
     
  2. Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Kepler-452b
    #2
    Just to get the obvious question out of the way, are you sure that it's the case where they display is sleeping, as opposed to when the computer is sleeping? Do you actually get a connection in which to use the command?
     
  3. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #3
    I'm surprised at this. I would have the thought the network activity produced by the remote connection would have prevented the computer from sleeping.
     
  4. macrumors 603

    Joined:
    Aug 9, 2009
    #4
    This worked for me, with display sleeping or not:
    Code:
    [B]cat sleepy[/B]
    #!/bin/bash
    /usr/bin/osascript -e 'tell app "System Events" to sleep'
    
    So did this command:
    Code:
    sudo pmset sleepnow
     
  5. thread starter macrumors 603

    BlueRevolution

    Joined:
    Jul 26, 2004
    Location:
    Montreal, QC
    #5
    Yes. As I said, I can ssh in and run other commands while the display is asleep.

    Nope, when the computer sleeps it just stops responding to subsequent commands. Can't wake it up again remotely via ssh, only (by my understanding) with a wake-on-wireless-LAN–enabled computer and an Apple router.

    No good. That's the same command.

    Perfect! I'd rather not have to sudo up to run it, but it seems to work fine without sudo. Just rewrote that as a shell script and presto.

    Thanks for the help, folks.
     
  6. macrumors 603

    Joined:
    Aug 9, 2009
    #6
    If pmset doesn't need sudo, then you're probably logged in as root. That might affect the running of AppleScripts.

    I wasn't logged in as root when I ran my tests.
     
  7. thread starter macrumors 603

    BlueRevolution

    Joined:
    Jul 26, 2004
    Location:
    Montreal, QC
    #7
    I'm not. My user account is admin, but it's not root.
     

Share This Page