Use Automator to quick edit "hosts" in Mac OS X
A web developer often edit hosts file for developing use. In windows, you can create a shortcut to use notepad to open and edit the hosts file. In Mac OS X, I like using Spotlight to quick open the stuffs I want. But the hosts files is hidden by default and can not be found in Spotlight. So I use Automator to triger TextMate to open hosts file for editing.
Here’s what I did in Mac OS X 10.5.6:
- Open Automator
- In Library of Automator, choose “Get Specified Finder Items” in “Files & Folders” category. Drag it to the right panel.
- Open Finder, select “Go -> Go to Folder…” from menu (or use shortcut key “CMD+SHIFT+G“), input “/private/etc/“. You can find “hosts” file here. Drag it to Automator, the file list area in “Get Specified Finder Items”.
- In Library of Automator, choose “Open Finder Items” in “Files & Folders” category. Drag it to the right panel. Choose the application you like to open the hosts file. As to me, I chose TextMate.
- Click “Run” button on the top right to test it.
- Save it as application in “Application” folder.
This works alright in 10.5.6. But I just upgrade to 10.5.7 a few days ago and I found that it does not work. When test run, the automator can find out the file but cannot open it, even after I repairing the disk permissions. Then I changed another way for step 4 here:
4. In Library of Automator, choose “Run AppleScript” in “Utilities” category. Input following lines:
on run {input, parameters}
tell application “TextMate”
open input
end tell
return input
end run
Test run and bingo!
Jorge responded:
No, it doesn’t work in 10.5.7 on my MacBook Pro… Can you post the script?? Thanks!
Tkjune responded:
@Jorge: The scripts are above lines
on run {input, parameters}
tell application “TextMate”
open input
end tell
return input
end run
I’ve been experienced the failure in 10.5.7, that’s why I used the AppleScript.
hot trends responded:
I feel a lot more people need to read this, very good info! . . . . . .
ds responded:
was trying to do this via a shell script like so…
#!/bin/bash
sudo vim /private/etc/hosts
but was getting annoyed with it. Your steps worked for me right off the bat. Many thanks.
Tkjune responded:
@ds I’m always so happy of providing some helpful tip 🙂 enjoy.
Ændrew responded:
Here’s an easy way:
1. Open Finder. Click Go -> “Go To Folder…” and type in “/etc”.
2. Right-click “hosts” and click “Make Alias”.
3. Move the Alias to somewhere useful — I have a folder in /Applications full of aliases to various web-dev related files, which I’ve then dragged to the Dock to be used as a stack.
4. Open the alias to edit the file.
For some files (httpd.conf, for instance), you might have to open the original file first — telling it to always open in your preferred text editor — in order to prevent having to select said-text editor every time you edit that file.
Hope this helps!
Joe responded:
@aendrew Quite helpful tip! Thanks!