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

Toggle Web Sharing with an AppleScript System 10.8

As mentioned in this hint, Mountain Lion removed the setting in the Sharing preference pane to turn Web Sharing on and off, even though Apache Web Server is still installed by default. That hint also mentions a third-party preference pane that you can install to toggle Web Sharing in Mountain Lion.

Here's another solution in the form of an AppleScript. If you copy the script to a .scpt file in ~/Library/Scripts, you can conveniently toggle Web Sharing by selecting the script in the Scripts menu.

Here's the AppleScript:

display dialog "Enter your password:" with title "AppleScript" ¬
	default answer "" with hidden answer
set pw to text returned of result

set resultMsg to do shell script "
	listResult=`launchctl list | grep org.apache.httpd`; 
	if [[ $listResult ]]; then 
		apachectl stop; 
		echo 'Apache Web Server stopped.'; 
		apachectl start; 
		echo 'Apache Web Server started.'; 
	fi" password pw with administrator privileges

tell application "Finder"
	set notifier to POSIX file "/Applications/"
	if exists notifier then
		do shell script POSIX path of notifier ¬
			& "/Contents/MacOS/terminal-notifier -message '" ¬
			& resultMsg & "'"
		display alert resultMsg
	end if
end tell

The script uses the free Terminal-Notifier app by Eloy Durán, if installed, to notify you that Apache Web Server has been started or stopped. If you have the app installed somewhere other than /Applications, be sure to edit the app's path in the script.

Note that the state of Web Sharing is persistent across reboots, whether you use this method to control it or the aforementioned preference pane.

  • Currently 3.00 / 5
  You rated: 2 / 5 (20 votes cast)

Toggle Web Sharing with an AppleScript | 1 comments | Create New Account
Click here to return to the 'Toggle Web Sharing with an AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Toggle Web Sharing with an AppleScript
Authored by: jaydisc on Aug 29, '12 04:25:59PM

I disagree with the method for password collecting.

The "password" argument could be removed from `do shell script` and that command would automatically prompt the user for authentication, NOT just a password, but also a username (in case the user running the script wasn't an admin themselves, which NO ONE should be day-to-day), and it would do it using the standard OS security escalation dialog. So, I would remove the first three lines, as well as "password pw " before "with administrator privileges".

[ Reply to This | # ]