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?


Solution 1:

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.

Solution 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