5 Replies Latest reply: Jul 15, 2015 10:14 AM by ChitlinsCC Branched to a new discussion.
MacOSXNoob Level 1 Level 1

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)
  • red_menace Level 6 Level 6
    expertise.desktops
    Desktops

    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.

  • MacOSXNoob Level 1 Level 1

    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

  • red_menace Level 6 Level 6
    expertise.desktops
    Desktops

    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?

  • Joshua C C Level 1 Level 1

    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.

    1.png

     

    3. Under Library - Search for: Run AppleScript

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

    2.png

     

    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.

     

    Screen Shot 2015-07-14 at 4.27.58 PM.png

     

    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.

     

    3.png

     

    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.

  • ChitlinsCC Level 5 Level 5
    expertise.applepay
    Apple Pay

    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"

    Picture 6.png

     

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

     

    Keep up the good work, though...

     

    buenos tardes

    ÇÇÇ