Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript to Edit hosts File?

I edit my hosts file a lot, and I am trying to edit it with one click, or something similar. I found an Automator application that would modify the hosts file and open it to show the modifications. It worked once, but now I've re-downloaded it to try to change it in Automator. I found this in Automator:


Run AppleScript:

on run {input, parameters}

set hostToAdd to "echo '


# Block Adobe Activation

127.0.0.1 (A bunch of hostnames that would be pointless to share)

' >>"

set fileHosts to "/etc/hosts"


do shell script hostToAdd & fileHosts with adminstrator privileges

return input

end run


Get Specified Finder Items

hosts


Open Finder Items



Then, I modified the script to this:


set x to text returned of (display dialog "What do you want to add?" default answer "127.0.0.1 ")

set hostToAdd to "echo '


keystoke x

' >>"

set fileHosts to "/etc/hosts"



do shell scripthostToAdd & fileHosts with administrator privileges

return input

end run


it asked me what I wanted to add, asked for my password, opened my hosts, but nothing was changed. The original app didn't change anything either anymore.


Any advice? What do I need to change? Is it because I ran the original application previously?

Thanks for your help

MacBook Pro, OS X Mountain Lion (10.8.2)

Posted on Dec 23, 2012 9:14 AM

Reply
5 replies

Dec 23, 2012 1:04 PM in response to MacOSXNoob

Your echo command isn't right (keystroke would be a System Events command if it wasn't in a text string), but you should be seeing the incorrect text in the hosts file. The statement should be more like


sethostToAddto"echo " & linefeed & quoted formofx & linefeed & " >>"


... but really all you need to do is use a text editor that is designed for these kinds of things, such as TextWrangler or BBedit. Either of those can navigate to the hidden folders and edit the file directly, including admin authentication.

Jul 14, 2015 5:11 PM in response to MacOSXNoob

You Can use Apple Script along with Automator to create an App that will allow you to Edit the host file.


HOW TO:


1. Open Automator

2. Selection Application and click the choose button to continue.

User uploaded file


3. Under Library - Search for: Run AppleScript

4. Drag the "Run Apple Script" action model over to the right hand pain.

User uploaded file


5. Copy and paste the follow command: do shell script "sudo open -t /etc/hosts" with administrator privileges

You should be pasting the command where it says (* Your Script goes Here*)

6. Compile - using the Hammer icon


NOTE: The results of your efforts should mirror my example below.


User uploaded file


Lastly you can test things out to see if they work by click Run.


Once you confirm you have things working for your needs you can use the file menu to Save the finish product to your desktop as an app.


User uploaded file


NOTE: you will be prompted for your admin account and password


Keep In mind the apple script command being used along with Automator is going to run your default TextEditor.

Jul 15, 2015 10:14 AM in response to Joshua C C

Joshua


It appears that your duplicate post issue has been resolved - BUT - the bigger issue might be all the time you spent crafting a very nice post to a VERY old thread - thus creating what we fondly refer to as a "Zombie Thread"

User uploaded file


Worry not, we all have done such things - but most have learned a lesson 😉


Keep up the good work, though...


buenos tardes

ÇÇÇ

AppleScript to Edit hosts File?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.