3 Replies Latest reply: Dec 23, 2012 1:33 PM by red_menace
MacOSXNoob Level 1 Level 1 (5 points)

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 script hostToAdd & 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)
  • 1. Re: AppleScript to Edit hosts  File?
    red_menace Level 6 Level 6 (14,615 points)

    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

     

    set hostToAdd to "echo " & linefeed & quoted form of x & 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.

  • 2. Re: AppleScript to Edit hosts  File?
    MacOSXNoob Level 1 Level 1 (5 points)

    That still didn't work. Does it make a difference that I already have blocked things in my hosts file (I have all ads blocked)?

     

    ...And also, I already have Gas Mask, but I wanted an easy thing to click, type, and boom, done

  • 3. Re: AppleScript to Edit hosts  File?
    red_menace Level 6 Level 6 (14,615 points)

    You should be able to see the additions you are making to the file.  Not sure how much easier it is, but I just open BBEdit (the hosts file is in the recent documents), edit, and done (I try to stay away from booms).

     

    Edit:  could the Gas Mask settings be getting in the way?