2

Specifically, I want to simplify editing the hosts file, which requires root privileges.

The following command works in the shell:

sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts

From what I am able to learn, the following should work using Automator:

-- Run AppleScript
on run {input, parameters}
    do shell script ¬
        "/Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts" with administrator privileges   
end run

… the key being that with administrator privileges is the equivalent of sudo and will ask for a password.

However, when I run the script, I get the following error:

The command terminated due to receipt of a signal.

and then

TextEdit quit unexpectedly.

… which looks serious.

If I try the same thing with Atom text editor, it works as intended.

How do I get TextEdit to run as root?

4

On the current version of macOS (10.12.6 at the time of this writing), this will not work from the shell either:

$ sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit 
Password:
Illegal instruction: 4

And if you check the crash logs, you'll find the following:

Sandbox registration internal error: Incoming message euid:0 does not match secinitd uid:501.

This is by design; TextEdit is a sandboxed app, and running it as root would defeat much of the purpose of sandboxing. I don't think you will be able to find a way to get it to run as root. This isn't a bad thing, though, as running a GUI app as root is generally a terrible idea for security, as there are many ways to surreptitiously inject code into a Cocoa app, and if that app runs as root, you can open up some pretty serious security vulnerabilities this way.

To edit files like /etc/hosts, I recommend finding another way:

  1. Use a command-line editor such as emacs, pico, or vi.

  2. Use a GUI-based text editor that has the feature to prompt for your admin password when needed (I believe BBEdit can do this, for example)

  3. Simply copy the /etc/hosts file to another location, edit it there, and then use sudo to copy the modified file back to /etc.

1
  • Actually, you’re right in that command doesn’t work in the shell either — only for Atom. The reason I wanted to use TextEdit is to my students easy access. Thanks for the answer, though — at least it explains why. – Manngo Sep 12 '17 at 6:39
2

Not really an answer to what you asked, but as an alternative, have you tried Gas Mask - freeware Hosts file editor.
It can edit, store multiple versions, & switch on the fly.

enter image description here

1
  • Nice catch. I have been looking for something like this, but couldn’t find anything, which is why I gave up and tried the TextEdit approach. – Manngo Sep 12 '17 at 6:41
0

Just to close the loop on this (because it was one of the top hits on Google).

Here is how to open any file

terminal (bash/zsh):

sudo open /Applications/Developers\ Tools/Sublime\ Text.app /etc/hosts
sudo open "/Applications/Developers Tools/Sublime Text.app" /etc/hosts

alias - entry:

alias edit="sudo open /Applications/Developers\ Tools/Sublime\ Text.app"

alias - usage:

edit /etc/hosts

Automator - Quick Action (for use in Finder):

  1. New > Quick Action

  2. Add 'Run Shell Script' Action to workflow

  3. Configure workflow options to be - Workflow receives current files or folders in Finder.app (bolded words are options to select)

  4. Paste the snippet below in the Shell Script action box

    open -a /Applications/Developers\ Tools/Sublime\ Text.app "$@"

  5. Save workflow - suggested name "Open in Sublime Text (as root)

  6. Open System Preferences > Keyboard > Services and verify that the script appears and is checked off.

  7. Go to Finder and right click and you shall find the option in the context menu to launch the input (the selected file) as root / superuser

Hope this helps anyone else who comes across this.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .