1

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?

2

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.

  • 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
1

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

  • 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

You must log in to answer this question.

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