Spin Down Startup Drive...

Hi:

On occasion, I’d like to be able to spin down (or sleep) the startup drive on my PowerBook… for instance, when doing low key tasks like word processing.

Is there a simple, safe way to script this? A shell perhaps? That I might call from FastScripts as a keyboard command?

Incidentally, thus far, UNIX scripting is opaque to me… including making sense of man pages. Is there a decent beginner’s reference available specific to Mac and integration with AppleScript?

Thanks much for any/all replies.

And thanks once again for this resource… I solved a couple of nagging script problems this morning by searching the Forums.

Peter B.


Model: PowerBook G4
Browser: Safari 416.12
Operating System: Mac OS X (10.4)

pmset is the shell function that controls spindown and the shortest possible setting is 1 minute (0 is interpreted as ‘never’).

To see what disksleep is set to, run this:

do shell script "pmset -g | grep disksleep"

The answer is in minutes and the default is 15

To set the spindown time to 1 minute:

do shell script "pmset -c spindown 1" password "YoursHere" with administrator privileges

You’ll be asked for a password if you don’t include it in the script.

A setting of 0 means ‘never’.

Adam:

Thank You…

A quick try yielded the desired result at one minute… even while on the web with a dialup connection… and the drive spins back up ‘on demand’… (perhaps no surprise).

Will have to play some more with timing to find an optimum… but this may be it.

Best Wishes.

Peter B.


Glad it worked for you. You can combine statements to make the time toggle back and forth each time you invoke this:


if last word of (do shell script "pmset -g | grep disksleep") is "15" then
	set T to 1
else
	set T to 15
end if
do shell script "pmset -c spindown " & T password "YoursHere" with administrator privileges
-- the coercion of T to text is implicit because it is concatinated with previous text

Or… set a time of your choosing.

Does this work for you?

Peter B.




display dialog "Spin Down Hard Drive After... ?  Minute(s)" & return ¬
	default answer "1" with title "Set Hard Drive Spin Down Interval"

set time_til_spin_down to text returned of result as integer

do shell script "pmset -c spindown " & time_til_spin_down password "Your Password" with administrator privileges

set spindown_time to do shell script "pmset -g | grep disksleep"

set _Priors_ to AppleScript's text item delimiters
set AppleScript's text item delimiters to " disksleep	"
set spindown_time to text item 2 of spindown_time
set AppleScript's text item delimiters to _Priors_

display dialog "Hard Drive Is Set To Spin Down After..." & return & return & spindown_time & " Minute(s)" & return & return with title "Hard Drive Spin Down Interval"



It’s a slow night, Peter. How about this:


-- get current setting
set spindown_time to do shell script "pmset -g | grep disksleep"
-- ask for new one
set Q to (display dialog "Enter Hard Drive Spindown in  Minute(s)" & return & "Now set to " & SDTime(spindown_time) & " minute(s)" & return default answer "Enter an Integer" with title "Set Hard Drive Spin Down Interval" buttons {"Leave As Is", "Change It"} default button "Change It")
if button returned of Q is "Leave As Is" then return -- quit if no change
set time_til_spin_down to text returned of Q
-- change the setting
do shell script "pmset -c spindown " & time_til_spin_down password "YourPW" with administrator privileges
-- get changed setting
set spindown_time to do shell script "pmset -g | grep disksleep"
-- tell me that the setting was successful
display dialog "Hard Drive will now Spin Down in " & SDTime(spindown_time) & " Minute(s)" & return & return with title "Hard Drive Spin Down Interval"

to SDTime(spindown)
	set _Priors_ to AppleScript's text item delimiters
	set AppleScript's text item delimiters to tab
	set SDT to text item -1 of spindown
	set AppleScript's text item delimiters to _Priors_
	return SDT
end SDTime

Indeed… though I seem to be in one of my compulsive modes… but I think I’m going to (try to) let the spindown script go for now.

Something else I’ve been playing with off and on follows. Save as an app or app bundle with the name "Look Up’ and try it in the Dock (it will likely fail as run from a script editor).

Thanks to Jacques for his example calling the ‘lastmost’ app.

Peter B.


 

tell application "Finder"
	set visible of process "Look Up" to false
	set last_app to item 1 of (get name of processes whose frontmost is true)
end tell

tell application "System Events"
	tell application last_app
		keystroke "c" using command down
	end tell
end tell

tell me to activate

delay 0.2

set look_up to the clipboard

if (count of look_up) is greater than 25 then
	set look_up to ""
end if

tell application "Dictionary" to activate

tell application "System Events"
	keystroke "a" using command down
	keystroke look_up
	--keystroke (ASCII character 3)
end tell

 

It just says “Process Not Found” when I run it as an application.

Be sure to name the app “Look Up” (no quotes).

Peter B.


Same problem, Peter - simply doesn’t work for me.

Oh, well. Sorry, Adam… I don’t have any other suggestions.

Thanks again for the spin down help.

Peter B.


Hi.

I’m sure that scripting an entry in Dictionary is way off-topic for this thread; but still…

This version of Peter’s script seems fairly reliable on my machine. It can be run from the Dock or even double-clicked, but it must be run as an application.

-- Hide this just activated applet.
tell application "System Events" to set visible of 1st application process whose frontmost is true to false
-- Make sure the Finder hasn't come to the front through double-clicking.
tell application "Finder" to set frontmost to false
-- The frontmost application is now the one that was frontmost before invoking the applet. Copy the selection.
tell application "System Events" to keystroke "c" using command down

tell application (path to frontmost application as Unicode text) to set look_up to the clipboard

if ((count look_up) is greater than 25) then set look_up to ""

tell application "Dictionary" to activate

tell application "System Events"
	repeat until (application process "Dictionary" exists) and (frontmost of application process "Dictionary" is true)
		delay 0.2
	end repeat
	keystroke "a" using command down
	keystroke look_up & return
end tell

I myself use a script that runs from Script Menu and gets the name of the source application by parsing its own path for the name of the application-specific script folder in which it resides. There are two versions, in fact. One is similar to Peter’s and uses various keystrokes to get the text into the Dictionary window. The other clicks the “Look Up in Dictionary” item in the Application/Services menu. The latter method, however, doesn’t show Thesaurus information. It’s also slightly infuriating because it implies that there may be some way to to get the look-up text into Dictionary without using the GUI…

Nigel Garvey wrote:

True. I probably should have posted the Dictionary script elsewhere, but…

Thanks for the additional example. I’m not all that concerned about speed, but your app runs a bit faster than mine and showed me another way to ‘address’ the previous (last frontmost) app, which I was never able to figure out on my own. Adapting Jacques’ example, I’m now parsing my app’s process name from its path, then setting its visible to false, per above. The name of the app need not then be hard coded to use a similar routine… but the method is clunky and long winded… and well, yours is far preferable.

I still live very much in the realm of ‘what works here’ and do a lot of headbanging to get my own stuff to work… and I still get frustrated by how finicky AS can be about which machines and systems it likes… even when they’re of similar vintage… per above.

Such is life.

Thanks again.

Peter B.


By the way, bringing the thread back to topic, I discovered that Adam’s original snippet:


do shell script "pmset -c spindown 1" password "YoursHere" with administrator privileges

… only works when the laptop is plugged into a charger. And:


do shell script "pmset -b spindown 1" password "YoursHere" with administrator privileges

… works only when using battery power.

I incorporated both into the same script. And can still have independent settings for either mode.

I think -a sets both… but it wasn’t clear to me, so I didn’t try.

Peter B.


I’m not very good with ‘man’ “explanations”, but it does look as though -a should cover all situations.

According to the ‘man’ on my Tiger machine, “spindown” is deprecated in Tiger in favour of “disksleep”, but will continue to work. On my PowerBook, which runs Jaguar, there is only “spindown” for this purpose.