Take the 2-minute tour ×

There is a pretty awesome applescript called "Open Terminal Here" ( http://www.entropy.ch/software/applescript/ ) which you can add to your finder's toolbar and click when you want to launch a terminal console which is set to that directory.

Sometimes I need to be root, and so I end up starting terminal, doing something like sudo -i and then I have to change back to the previous directory because the sudo command is landing me in /var/root.

I'm using sudo -i because I like it to load things like aliases / the bash profile.

The script is applescript, and here's the important part of how it works:

    set cmd to "cd " & quoted form of the_path & " && echo $'\\ec'"
    tell application "Terminal"
        do script with command cmd

How do I get this to load as root?

share|improve this question
Just curious...Do you do sudo -i because you specifically want to run root's .profile, instead of your own? Because if it's just a case of wanting to make sure certain shell initialization files get run, that might be easier to fix and it would allow you to switch to using sudo -s to get a root shell without changing your working directory. –  Spiff Feb 23 '11 at 5:57
I have a /etc/profile file which I believe is like a global .bash_profile - that is supposed to load for all users. I have an alias command in there alias ll="ls -al" and if I run sudo -i then I have access to the ll command, but if I run sudo -s then the ll command doesn't work. But you're right, the directory doesn't change. So how can I use applescript to launch Terminal as root? –  cwd Feb 23 '11 at 14:47
FYI: ~/.bashrc is sourced for non-login shells. My own practice was to put all of my aliases and such in ~/.bashrc, and add a . ~/.bashrc line to the end of my ~/.bash_profile. The OS X bash(1) manpage doesn't indicate a system-wide equivalent to ~/.bashrc, though some other man pages suggest /etc/bash.bashrc is used. –  coneslayer Feb 24 '11 at 16:57
@coneslayer - hmm, i tried to edit all of those files. and no matter what i do, when i run sudo -s I can't get them to be loaded. I added alias ll="ls -al" to all of them but it's not working for any. This actually would be the simplest solution if I could get it to work! Also, I have an /etc/bashrc file, with a comment line # System-wide .bashrc file for interactive bash(1) shells. at the top. but it too, has let met down. –  cwd Mar 4 '11 at 15:24
i just earned the popular question badge for this one (1k view) but why has nobody upvoted? plz upvote if you found this question helpful. –  cwd Feb 20 '12 at 23:47

2 Answers 2

up vote 1 down vote accepted

It was actually quite complicated to figure out, but I ended up adding this to my /etc/profile file:

if [ -d "$directory" ]; then
    cd "$directory"

After that I used the "Open Terminal Here" script and simply modified it so that it copies the current path to the clipboard before launching the terminal

    tell application "Finder" to set the clipboard to the_path

and then I added the sudo -i part to the code where it activates terminal:

    set cmd to "cd " & quoted form of the_path & " && echo $'\\ec' && sudo -i"

    tell application "Terminal"

So, the path will be copied to the clipboard, I can use an icon to launch the applescript, and then when the profile is loaded (for root) it will change to the directory.

share|improve this answer

What's the directory you're trying to get to? The home alias ('~') doesn't work as root, so you might need to use an absolute path.

To use AppleScript to launch Terminal as root you could do something like the following (untested!):

tell application "Terminal"
    tell application "System Events" to keystroke "sudo -i" & return
    do script with command cmd

This will grant you administrator privileges before running the code that changes to the open Finder directory. That could be pretty risky if you weren't careful about the scripts you were running.

More generally, you can get AppleScript to do sudo commands with code like this:

set shellOutput to do shell script cmd with administrator privileges
display dialog shellOutput

where 'cmd' is the command to run. (This also displays a popup of the code results.)

share|improve this answer

Your Answer


