Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I made a script to automatically shut down my computer. The problem is that it always asks the password. How should this be changed?

do shell script "shutdown -h now" with administrator privileges and password
share|improve this question
    
Why wouldn't you just use System Prefs -> Energy Saver -> Schedule (button on lower right)? –  ricbax Nov 17 '10 at 1:53
1  
Because I don't have a set time when I want to turn it off. –  tony_sid Nov 17 '10 at 5:06
    
Do you want to force a shutdown, or are you happy with stopping it if you have unsaved changes interrupt? –  Daniel Beck Nov 17 '10 at 12:10
    
I'm trying to force a shutdown, that's why I made the script. The only thing that is interrupting it is the password request. –  tony_sid Nov 18 '10 at 20:48

4 Answers 4

Here you go:

do shell script "shutdown -h now" user name "me" password "mypassword" with administrator privileges
share|improve this answer

Although a security risk, one way to do this would be to change the owner of your script to root and then change the setuid bit.

1) create the apple script as you described and save it to your Desktop as shut_me_down

2) open a terminal, cd to your Desktop folder and run:

chmod -R u+s shut_me_down.app/
sudo chown -R root shut_me_down.app/

3) Now when you double-click on the shut_me_down icon on your desktop it should shutdown without any prompts.

If you're looking for a command line way to do this (rather than clicking on a desktop icon) then you could skip the setuid/chmod thing and instead edit your sudoers file using

sudo visudo

Add your userid as someone that can run sudo without a password and then try running your script using "sudo scriptname"

Here's a link which may help too:

Making an executable run as root every time - MacOSX.com

share|improve this answer
    
That's not going to make it suid. If you were going to do this you need to make the real binary suid which will be whatever.app/Contents/MacOS/whatever –  bahamat Dec 30 '10 at 23:40
    
The -R option of chmod should take care of that. –  Dale Hille Dec 31 '10 at 15:03

Would this not work?

tell application "Finder" to shut down
share|improve this answer
1  
Finder will quit, not shut the system down. –  Josh K Nov 17 '10 at 1:55
1  
@Josh wrong. I tried it and it wanted to save unsaved changes in all applications, and quit python in Terminal. Restarting Finder doesn't do that. –  Daniel Beck Nov 17 '10 at 6:27
    
@Daniel: I don't do Apple scripting, isn't "shut down" telling it to Quit? –  Josh K Nov 17 '10 at 15:02
1  
Nope. If you wanted to tell an application to quit you'd do tell application "Finder" to quit. Shut down does what it says on the tin, as do restart, sleep and so on. I believe telling the Finder to shut down is a hold over from Mac OS where the Finder "was" the operating system, rather than the clearer distinction between the kernel and userland programs that Mac OS X inherits from Unix. –  Scott Nov 17 '10 at 15:32
    
On a related note, if you want to find out about AppleScript commands, open up /Applications/Utilities/AppleScript Editor and go to Window -> Library which gives you access to documentation on commands that scriptable applications support. –  Scott Nov 17 '10 at 15:45

do shell script "shutdown -h now", in theory would....but that results in a "non-super user" warning message.

As far as I can tell, and it makes sense, you have to be super-user to reboot the system from the command line. Why does this make sense? because you don't want someone rebooting the computer from an ssh session, if they don't have Super-user / root access...

I'd be interested if anyone found a way to do this....

Scott is accurate...

Tell application "finder" to restart -- Restart system Tell application "finder" to shutdown -- Shutdown system

But that will prompt the user to save any unsaved files... So if your trying to bypass prompts, it won't work.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.