Narcolepsy 2.0 for Mac OS X [Update]

For about 2 years I have been looking for a solution to the sleep problems of my Mac Mini, which serves as a mediacenter in the living room. The main problem was that the Mac (Snow Leopard) won’t sleep by itself. When googling for these problems a million of different reasons seem to be responsible for this. However, the number of solutions is limited and I tried them all. I am not alone with this.

The problem with trying to find the actual causes

I’ve spent many hours trying to find the causes why the Mac won’t sleep: ranging from resetting PRAM regularly, varying wakeup options for WOL and Bluetooth, trying to identify individual applications or daemons, which might be responsible for it and so on. It seems that there are just too many different causes that have the same symptom: not sleeping.

That is why I finally gave up looking for the actual cause. Instead I decided to fight the symptoms with additional tooling.

The problem with PleaseSleep

In discussion boards, PleaseSleep is often celebrated as a reasonable solution killing the symptoms. However, the main problem with it is that it cannot be configured easily to not interrupt an EyeTV recording. That is PleaseSleep is pretty much useless for my scenario.

The problem with Narcolepsy 1.0

This is a small script (AppleScript) which is less well known and less often propagated in the web than PleaseSleep. However, it is quite capable of sending the Mac to sleep and even respecting EyeTV recordings and playbacks. The main issue with it is that it simply ignores user interaction (e.g. moving the mouse or typing the keyboard). So, for example when browsing the Web in Firefox, the Mac will just go to sleep – which is annoying.

The solution: Narcolepsy 2.0

I recently put some effort in enhancing the original script from 2008 by Kynan Shook to serve my needs better. I have been using this script for a while now and it works great for me. I believe that it will be useful for a number of people out there as well.

This is the complete information about this version:

This script will attempt to sleep the computer after (approximately) the time specified in Energy Saver.  It will not sleep under certain conditions (as originally distributed, if iTunes is playing, EyeTV is playing or recording, DVD Player is open, AirServer is playing Audio or Video content, the local Plex client is playing Audio or Video content or showing pictures).  This script is copyright © 2008-2012 Kynan Shook, Torben Schreiter.  Permission is granted to modify and redistribute this script, but not for profit.

version 2.0:
modification 2012 by Torben Schreiter to extend the original script’s functionality:
– replaced idle check with HIDIdleTime because a wrong number format was returned with the original idle check which resulted in a problem where the Mac goes to sleep despite user interaction –thanks to inspiration by Jason Salaz (http://apple.stackexchange.com/questions/39085/how-can-i-automatically-launch-an-application-whenever-the-mac-goes-idle)
– fixed a problem with sleepDelays smaller than 5 minutes (retryDelay)
– added subroutine to check for CPU load of a process
– added subroutine to retrieve URL
– added default configuration for not sleeping if AirServer is playing Audio or Video content
– added default configuration for not sleeping if the local Plex client is playing Audio or Video content or showing pictures
– added default configuration for not sleeping if the local Plex Media Server is transcoding/streaming (e.g. iPad client)

version 2.0.1
– Stay awake if any application registered for one of the kernel assertions named PreventSystemSleep or PreventUserIdleSystemSleep or PreventUserIdleDisplaySleep (to monitor type in terminal: „pmset -g assertions“)
– Stay awake if Caffeine is activated
– Stay awake if Finder is copying a file
– added NSUIElement = 1 to Info.plist to not show Narcolepsy in the dock
– Stay awake if the system is printing (note that also open/unprinted print jobs with switched off printer will prevent sleep)
– Stay awake if EyeTV is exporting (e.g. for later iOS playback)
– Stay awake if EyeTV is streaming live TV to iOS App
– added experimental default configuration for not sleeping if EyeTV is streaming to an iOS device (does not work reliably)
– prevent sleep: changed activity detection method from CPU usage to # work queue threads for AirServer

version 2.0.2 (January 30, 2013)
– fix systemSleepDelay detection for Lion and Mountain Lion
– rewrote/refactored timer structure to be easier to understand
– better support for waking up through remote events (e.g. Wake on LAN)
– added debug mode to write debug messages to a log file
– added workaround for iTunes 11 + iOS Remote App bug where iTunes would prevent sleep if an iOS device has a (background) running Remote App without actually playing

version 2.0.3 (March 10, 2013)
– uncommented Caffeine in default configuration because of annoying dialog asking for the location of Caffeine if it is not installed on the current system

version 2.0.4 (November 3, 2013)
– Mavericks compatibility: uncommented Printer Setup Utility in default configuration, because it was deprecated
– Mavericks: Please note that you need to enable the Narcolepsy 2.0 script here: Settings > Security > Privacy > Accessibility
– fix error breaking the sleep check after a fresh reboot
– better error output messages to log for the different applications

version 2.0.5 (April 24, 2014)
– fixed frequently occurring „General error checking Kernel Assertions“
– since OS X 10.9.2: sometimes the own Applescript .app process is creating an assertion blocking going to sleep => we have to ignore all PIDs originating from the script with our name
– removed redundant code

version 2.0.6 (May 29, 2014)
– added some better robustness & debugging output for the getMyName() method

How to install:
– extract .zip
– put a link to the user’s start items for automatic start of the script
– OS X Mavericks: Please note that you need to enable the Narcolepsy 2.0 script here: Settings > Security > Privacy > Accessibility

Download Narcolepsy 2.0.6 here

Stichworte: ,,,

24 Gedanken zu “Narcolepsy 2.0 for Mac OS X [Update]

  1. John Sawyer

    Very cool–thanks. I used PleaseSleep until recently, but stopped using it when I found it was randomly generating a lot of disk accesses, which was slowing down my older Mac (a dual G4 MDD). I started using Narcolepsy 1.0 instead, and it’s been working fine for me, but I’ll try your 2.0 version and see how well it works for me.

  2. John Sawyer

    Narcolepsy 2.0 is working fine for me, though it’s not being hidden–it appears in the Dock even though I’ve selected the „Hide“ checkbox for it in System Preferences > Accounts > Login Items. Hiding works with version 1.0.

  3. mesnchmaschine

    I use Lion.
    And so far as I could say it dosen’t behaive as it Should be. No Matrter what time i set in Energy Saver Prefs the script set my Mac to sleep after 5 minutes.

    I take now a closer look and report back.
    greetz

  4. 7s

    Very handy utility – many thanks!

    I have one issue to report though – my mac mini is scheduled to go to sleep at 23.30 every night and wake up at 7.00. It finally did sleep the whole night, but it failed to wake up in the morning..what can be the issue?

    I’m running OS X 10.6.8

    Keep up the good work 😉

  5. Thor

    Great Script! Tanks a lot Torben! Works perfect on my media system running iTunes and Plex on OSX.
    iTunes is so stupidly made that if it is open, the computer will NOT go into standby.
    I use the Remote App a lot (on iPad and iPhone), to control my iTunes library. I have found an interesting thing while using the Remtoe App:
    If my Mac running iTunes is in standby (thanks to Narcolepsy 2.0), launching the Remote App will bring the computer out of standby.
    My setup is now saving lots of power, while being at service when ever I need! It is great!

    I can not understand why Apple have made iTunes so it prevents standby while running. Fine if iTunes was playing content, but it still prevents standby while not playing any content at all!! Foolish design made by Apple. Fixed by Narcolepsy 2.0!

    Thanks again Torben!

    Best Regards Thor.

  6. calibius

    @ John Sawyer Add

    LSUIElement
    1

    to the info.plist in the Narcolepsy package to hide it from the dock again.

  7. Per

    Hi,
    Thanks for this script. Works perfectly except one thing.
    I have all my EyeTV recordings on my iMac and whenever I stream one recording on another device, Narcolepsy will try to sleep the iMac!
    If I shut down Narcolepsy, it isn’t a problem.
    Is there a workaround? Can it be incorporated into the script?
    Thanks again!

  8. Frédéric

    Copying iTunes’s clause and substituting VLC doesn’t work. Can anyone tell me how I can add VLC?

  9. Torben Artikel Autor

    I posted a major update (2.0.2) that I’ve been testing for a while already. It should greatly improve compatibility with other Audio/Video applications and fix a number of problems.

    Please give it a try.

    Cheers,
    Torben

  10. Per

    Hi Torben,
    Thanks for the update to the script! Nice to see you’re still doing work on it!
    I think I didn’t express myself clearly enough regarding EyeTV and streaming…
    The problem is not streaming, but more an ability of the script to detect network traffic I guess.

    As I mentioned, I have my EyeTV recordings on my iMac and the way it works is that these recordings are being shown on a computer with XBMC installed, making it more a file transfer from the iMac to the computer running XBMC.

    Can the script be made to detect network traffic somehow (not running wireless) and then NOT go to sleep??

    Thanks again for the script!
    /Per

  11. Torben Artikel Autor

    I looked into this and I cannot find a way to detect network traffic. The easiest would be if you could find some shell command that returns the current network bandwidth utilization in percent or better in absolute numbers.

    If you have something like that it wouldn’t be a problem to put it into the script.

    Cheers,
    Torben

  12. Per

    Thanks for the fast reply.
    What about netstat?
    $netstat -I en0 -b -w 2
    This shows the input/output bytes on en0 network card with an interval of 2 seconds in absolute numbers.
    With a current „file transfer“ (EyeTV recording to XBMC) this number grows at a large rate compared to no activity on the network card…

  13. Per

    Furthermore, with the v2.0.2 script, when I put it into the start items, a dialogue box pops up at boot asking me, where Caffeine is installed?!
    I don’t have Caffeine installed…
    When I press „cancel“, the iMac goes to sleep…

  14. Per

    Hi,
    it seems, if I do this command:

    netstat -I en0 -b | grep local | awk ‚{ print $10 }‘

    I get the output (corresponding to Obytes on my en0 network card) shown in an absolute number.

  15. Eric

    Using the bandwidth could be helpful if you have streaming apps running in the background all the time and you want to know if they are still actively streaming content.
    You could download the command line utility, bandwidth monitor, from here:
    http://sourceforge.net/projects/bwmng/

    Compile it and make it available for download here with download instructions (for those who don’t want to compile themselves).

    Then you could send a query to the bandwidth monitor that actually returns values you can work with (better than raw netstat data I could imagine).

    In a script it would look something like this, I guess:

    set csv to do shell script „bwm-ng -I en0 -o csv -c 1“
    set AppleScript’s text item delimiters to {„;“}
    set Tx to text item 3 of csv
    set Tx to round ((round Tx) / 1024)
    if Tx < 50 then –set your threshold in KB/s here
    set readytosleep to true
    end if

    I think it might be a good idea to send several queries (with 0.5 delays inbetween for example) and then have the average value compared to the threshold.

  16. Torben Artikel Autor

    Hi,

    I just uploaded 2.0.3 which is just the same as 2.0.2 without Caffeine dialog box. I didn’t find any solution to check dynamically if it is installed or not. AppleScript seems to need a static reference to bind to at first run which prevents dynamic checking. Please comment back in if you are using Caffeine.

    About streaming: I did a bit of experimenting with that. Mainly with the streaming functionality of EyeTV and Plex. The main problem is not so much the detection of active streaming. The main problem is the buffering of the streaming implementations. If the buffer is full, no streaming data is actually transferred for several minutes. It is almost impossible to tell if such a „pause“ in between the streaming data transfers are permanent or only temporary. That is why I stopped looking into it for the moment.

    Cheers,
    Torben

  17. Torben Artikel Autor

    version 2.0.4 (November 3, 2013)
    – Mavericks compatibility: uncommented Printer Setup Utility in default configuration, because it was deprecated
    – Mavericks: Please note that you need to enable the Narcolepsy 2.0 script here: Settings > Security > Privacy > Accessibility
    – fix error breaking the sleep check after a fresh reboot
    – better error output messages to log for the different applications

  18. Torben Artikel Autor

    version 2.0.5 (April 24, 2014)
    – fixed frequently occurring “General error checking Kernel Assertions”
    – since OS X 10.9.2: sometimes the own Applescript .app process is creating an assertion blocking going to sleep => we have to ignore all PIDs originating from the script with our name
    – removed redundant code

  19. Torben Artikel Autor

    version 2.0.6 (May 29, 2014)
    – added some better robustness & debugging output for the getMyName() method

  20. Andree

    You can get rid off your stringToLocalizedNumber function by doing the conversion within the shell command.

    Just add a pipe and TR at the end of command
    | TR „,“ „.“
    this will return 9.4 EyeTV instead of 9.4 EyeTV

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *