Saturday, September 30, 2006

Running shell commands with Automator or AppleScript and executing with sudo privileges

Automator is very cool because it will give you a compiled app that can run AppleScript and execute the more powerful Unix shell commands as well.  Actually AppleScript gives you this ability too via the do shell script command i.e:
 do shell script "mkdir /Library/StartupItems" 


What this example shows you:
  1. How to show a dialog with AppleScript where you can ask for a hidden password that is not visible (not shown) as typed.
  2. How to run commands in a bash shell with sudo access in Automator.
  3. How to run bash shell commands in AppleScript with sudo access.

Ok, so now if you are like me, you know the power of Unix shell scripting, you also know it won't be long before you'll need sudo to run one or more commands in your script.  Now I did a quick search on the internet to just find some sample code, and in all my searching I found most people have not posted a good solution.  Most people in their sample scripts or Automator workflows resorted to storing the password in clear text within the script.  Not only is that a security risk, your script is no longer portable and has to be edited for every user.  I found responses like this basically saying it couldn't be done.

Ok, so here's what you do.  I created an Automator script, that first runs the following AppleScript (this would also work, just in plain AppleScript without Automator):

on run {input, parameters} 
    tell application "System Events"
        set the_username to do shell script "whoami"
        set the_password to "password"
        display dialog "You now need to enter the password for the currently logged in account: " & the_username & "

This account must have Administrator access to this computer." default answer "password" buttons {"OK", "Cancel"} default button "OK" with icon 2 with title "Password" with hidden answer
        set the_password to text returned of the result
    end tell
    return {the_password}
end run

Ok in the Automator script, then my next step in Run Shell Script (in bash of course).  That script looks like:

exec echo $1 | sudo -v -S; 
if [ $? -eq 1 ] then
    echo "you are not a sudoer"; else
    echo "you are a sudoer";  # replace this with your entire script.
    exec echo $1 | sudo -S ls /private/var/root/;
fi

Now all you have to do is put your sudo needing commands inside the else fi block and you're set.  If you want to download an Automator script with this code as an example, click here.
BTW if you are running within an AppleScript app you can call sudo like this:
do shell script "sudo ls /private/var/root/" password the_password with administrator privileges  

Monday, September 25, 2006

Fetch Art Beta Testers Needed

I need a few beta testers, to test some fixes for Fetch Art.

If you've read some of the comments, many people are getting this error:
Can't locate LWP/Simple.pm in @INC (@INC contains: /System/Library/Perl/5.8.6/darwin-thread-multi-2level /System/Library/Perl/5.8.6...


I haven't been able to reproduce this error on my system. In fact Fetch Art is working great on all the systems I've tested.

I need some beta testers that meet the following criteria:
1) You get the error shown above when you run Fetch Art
2) You are running a PowerPC based Mac (sorry no Intels at the moment)
3) You are running the latest Mac OS X (10.4.7) and iTunes 7
4) You are willing to have me send you via email a .zip beta version and install it.
5) You'll report back the results to me.

If you meet the above criteria and would like to help, please send email to me at here. (Privacy promise: I will not give your email out to anyone else or post it here.)

Tuesday, September 12, 2006

Fetch Art 1.2 and iTunes 7

Today apple announced iTunes 7. I'm currently at work and cannot test it with Fetch Art (although I am downloading the new version, it's going slow, the servers must be busy.) However, they claim they've added an album art download feature which may make Fetch Art obsolete.

I'll have to do some testing to see how it works (and how well it works) to see if this will signal the end of development on Fetch Art which of course will break my heart.

UPDATE: I've completed testing with iTunes 7 on several PowerPC Macs and am happy to report Fetch Art runs great on all the machines I tested on. Still working towards Intel Mac support though.

I'd love to hear you comments as well. Let me know if you think the new feature of iTunes successfully replaces Fetch Art or if there are still reasons to continue developing the application.