Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Applescript and sudo

Applescript and sudo
Thread Tools
Mac Elite
Join Date: Mar 2001
Location: Provo, UT
Status: Offline
Reply With Quote
Dec 14, 2002, 12:04 AM
 
Weird question. I have an Applescript that needs to execute a few shell commands as root. Now I can think of a few ways to do this. The easiest, of course, is simply to add yourself to sudoers so that you don't need to enter a password.

I was, however, curious of more general solutions.
     
Mac Elite
Join Date: May 1999
Location: San Jose, CA
Status: Offline
Reply With Quote
Dec 14, 2002, 04:33 AM
 
The do shell script event has the option to execute as root:

do shell script "<script goes here>" withadministrator privileges

It'll ask the user for their password before it executes.

According to the AppleScript dictionary, there should also be a 'with password <string>' option, but I can't seem to get that to work for some reason.
Gods don't kill people - people with Gods kill people.
     
Mac Elite
Join Date: Mar 2001
Location: Provo, UT
Status: Offline
Reply With Quote
Dec 14, 2002, 03:48 PM
 
Yes that does it, but it asks for permissions for every call to do shell script. Here's the problem. The command is inside a repeat. It's basically a small script to change the permissions of a bunch of files.


Code:
on open itemList set quot to ASCII character 34 repeat with thisItem in itemList do shell script ("sudo chmod auo+rw " & quot & POSIX path of thisItem & quot) do shell script ("sudo chown clarkgoble " & quot & POSIX path of thisItem & quot) end repeat end open

As I said, I simply modified sudoers and it runs great. I just was curious if there was a way to ask for administrator priviledges only once for the script.
     
Mac Elite
Join Date: May 1999
Location: San Jose, CA
Status: Offline
Reply With Quote
Dec 14, 2002, 04:25 PM
 
Originally posted by clarkgoble:
[snip]As I said, I simply modified sudoers and it runs great. I just was curious if there was a way to ask for administrator priviledges only once for the script.
Using the with password syntax seems to work for me, so you could:

Code:
set thePW to text returned of (display dialog "Enter your administrator password:" default answer "") try do shell script "chmod a+w..." password thePW with administrator privileges do shell script "chown..." password thePW with administrator privileges on error display dialog "Oops. Wrong password?" end try
Gods don't kill people - people with Gods kill people.
     
Mac Elite
Join Date: Mar 2001
Location: Provo, UT
Status: Offline
Reply With Quote
Dec 14, 2002, 08:18 PM
 
That's a good idea except that it displays your password as you type it. Does Applescript have a password dialog box the way Javascript does?

As I said, it isn't that big a deal. The sudoers solution seemed best. It was interesting to come up against these limitations however.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:57 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2