Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.6: Easily manage the built-in web server with AppleScript Internet
Snow Leopard only hintIf you work with the built-in web server a lot, it's helpful to write some simple AppleScripts to open the files in your text editor, and to restart the server. Use AppleScript Editor to create the following scripts in your user's Library/Scripts folder.

A script to edit your httpd.conf file:
tell application "TextWrangler"
  open "/private/etc/apache2/httpd.conf"
  activate
end tell
A script to edit your user-specific httpd.conf file (if you use one):
tell application "TextWrangler"
  open "/private/etc/apache2/users/username.conf"
  activate
end tell
A script to edit your php.ini file (if you use PHP):
tell application "TextWrangler"
  open "/etc/php.ini"
  activate
end tell


A script to restart Apache:
try
  do shell script "/usr/sbin/apachectl configtest && /usr/sbin/apachectl restart" with administrator privileges
on error errorMsg number errorNum
  display alert "Error " & errorNum message errorMsg buttons "OK" default button 1
end try
Finally, make sure the Script menu is visible, by checking the appropriate box in the General section of AppleScript Editor's Preferences. Now you can run these scripts from the Script menu in any application.

If you use TextEdit instead of TextWrangler, you'll need to modify the scripts to launch TextEditor, which must be done differently. Replace each editing script above with one command, like this:
do shell script "/Applications/TextEdit.app/Contents/MacOS/TextEdit /private/etc/apache2/httpd.conf &> /dev/null &" with administrator privileges
That version will open the httpd.conf file for editing in a new TextEdit instance, running with root privileges so you can save your changes. The other editing scripts must be similarly modified.
    •    
  • Currently 1.81 / 5
  You rated: 3 / 5 (27 votes cast)
 
[9,713 views]  

10.6: Easily manage the built-in web server with AppleScript | 7 comments | Create New Account
Click here to return to the '10.6: Easily manage the built-in web server with AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.6: Easily manage the built-in web server with AppleScript
Authored by: arcticmac on Dec 14, '09 08:36:47AM

An easier way that would just open them in whatever your default text editor happens to be (defined by LaunchServices?) would be

do shell script "open -e /etc/php.ini"

[ Reply to This | # ]
10.6: Easily manage the built-in web server with AppleScript
Authored by: tedw on Dec 14, '09 09:16:59AM
Be careful with this: open -e /etc/php.ini will actually open the file in TextEdit, not TextWrangler, and TextEdit can save the file in rich format (styled text rather than plain text) which will make the php.ini file unreadable by the system. Make sure you're saving as plain text.

Personally, I've added /etc as a folder in TextWrangler's open/save dialogs so that I can access any files I need there directly. The script approach isn't half bad, though.

[ Reply to This | # ]

Re: 10.6: Easily manage the built-in web server with AppleScript
Authored by: Uncle Asad on Dec 14, '09 01:30:53PM

Or, if you’ve installed TextWrangler’s command-line tools, you can use "edit" instead of "open -e" and avoid the guesswork of having to interact with LaunchServices :P



[ Reply to This | # ]
10.6: Easily manage the built-in web server with AppleScript
Authored by: jweber on Dec 14, '09 03:29:50PM

That will work as long as your text editor can save files that you don't have write-permission for. TextWrangler can do this, but TextEdit can't.



[ Reply to This | # ]
10.6: Easily manage the built-in web server with AppleScript
Authored by: cnotarianni on Dec 16, '09 09:56:26AM
If you install the command line tools in TextWrangler will be possible to use the command edit in the Terminal. For example:
tell application "TextWrangler"
  open "/private/etc/apache2/httpd.conf"
  activate
end tell

Can be written in a single line:
do shell script "edit /private/etc/apache2/httpd.conf"
It is not necessary to issue the command "activate" since "edit" active TextWrangler and puts the file in the foreground.
Edited on Dec 16, '09 10:06:16AM by cnotarianni


[ Reply to This | # ]
10.6: Easily manage the built-in web server with AppleScript
Authored by: cnotarianni on Dec 16, '09 01:30:30PM
If should it be necessary to edit a file with root privileges you can always get by using a single line of code:

do shell script "sudo edit /var/root/Library/Preferences/loginwindow.plist" with administrator privileges

Edited on Dec 16, '09 01:32:03PM by cnotarianni


[ Reply to This | # ]
10.6: Easily manage the built-in web server with AppleScript
Authored by: geert2705 on Dec 18, '09 11:17:05PM

or -- use textmate as your editor -- does all this out of the box, besides being the most wonderful programmer's editor in the world :)



[ Reply to This | # ]