10.6: Easily manage the built-in web server with AppleScript

Dec 14, '09 07:30:00AM

Contributed by: jweber

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.

Comments (7)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20091203153915222