AppleScript Help Needed = Eject Drives Before Sleep

Discussion in 'Mac Programming' started by Osymandias, Apr 22, 2010.

  1. Osymandias macrumors newbie

    Joined:
    Apr 22, 2010
    Location:
    Texas
    #1
    Okay, so I have no programming knowledge whatsoever. I took Visual Basic as a freshman in High School many years ago, and that is the limit of my experience.

    I had a problem with a Device Removal error that I would get every time my Mac went to sleep. Realized it was my external Hard Drive still being connected. So I came up with a very basic script to eject the drive before my Mac went to sleep. Here's the code:

    ===============================================

    on idle

    tell application "System Events"
    if exists (process "ScreenSaverEngine") then
    delay 180
    tell application "Finder" to eject "Bamboo"
    else
    return 90
    end if
    end tell

    tell application "System Events"
    sleep
    end tell

    end idle
    -- Bamboo is the name assigned to the external drive.
    ============================================

    Functions perfectly in order to always eject the drive before my Mac goes to sleep.

    the PROBLEM: If I come back while the screen saver is running and before the Mac sleeps, it does not interrupt the process, and my comp still sleeps at the end of the delay timer.

    Is there an efficient way to add in another check before the system sleeps? So that if I manage to catch my computer just on screensaver it won't automatically go to sleep.

    As a side note, if there is a command I can use to make this script run automatically on startup, or am I required to launch the script from a folder every time I reboot?

    Keep in mind my limited programming knowledge, so I may not understand a lot of acronyms or jargon.
    Any help is appreciated. Thanks in advance.


    P.S. If anyone knows of a more efficient script or code to achieve this same effect, feel free to post. However, I am not looking for a third-party program or background app.
     
  2. macsmurf macrumors 65816

    macsmurf

    Joined:
    Aug 3, 2007
    #2
    Well, that is too bad because SleepWatcher would probably solve your problem.
     
  3. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #3
    AppleScript does a pretty lousy job on these idle scripts. You might want to direct your energies at finding some kind of alternative to AS. Having said that, there might be a few ways to improve the script and get what you want.

    One problem is this:

    delay 180

    You're essentially telling the computer to do nothing for three minutes, which includes ignoring user input.

    You might do better at something like this:

    Code:
    property secondTimeAround : 0
    
    on idle
    	
    	tell application "System Events"
    		if exists (process "ScreenSaverEngine") then
    			if secondTimeAround = 0 then
    				set secondTimeAround to 1
    				return 180
    			else
    				set secondTimeAround to 0
    				tell application "Finder" to eject "Bamboo"
    			end if
    		else
    			set secondTimeAround to 0
    			return 90
    		end if
    	end tell	
    end idle
    mt
     
  4. Osymandias thread starter macrumors newbie

    Joined:
    Apr 22, 2010
    Location:
    Texas
    #4
    MT,

    Thank you! I'll type this in tonight and see if it works.
     

Share This Page