Efficiently test BBEdit web code through Localhost Apache

What most annoys me about testing HTML, PHP, JQuery, JavaScript etc etc is that you have to run your code through the Apache Web Server in order to test. In my case I use XAMPP and edit the code files directly in the htdocs folder or subfolders thereof. Rather than manually write the URL of the file to be tested into the browser and then save their links into the favourites bar or in the Bookmarks), I run an AppleScript from BBEdits Scripts menu. This script formulates the correct URL of the file using its filename, folder and subfolder in which it resides and opens up Safari or Firefox page in which the server outputs the processed code. So it doesn’t matter where you place the file in the host directory.

Shell script test for Apache status rather than testing for Manager-osx

Apache and mySQL can be started silently with one click of a script from BBEdit’s scripts menu, or FastScripts menu, or even Script Editor’s menu like so:


do shell script "/Applications/xampp/xamppfiles/xampp start" password "[your mac admin password]" with administrator privileges

This bypasses XAMPPs Manager-osx app and so our PHP Runner script below needs to instead check whether Apache is running rather than Manager-osx.


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

--dynamically add the path of the currently edited file to the safari URL
--file can be in any recursive subfolder of htdocs folder 

global folderName, foldrList, newList, theURL
set folderName to ""
set folderList to {}
set newList to {}
set counter to 1

tell application "BBEdit"
	activate
	
	try
		set doc to active document of text window 1
		set docName to name of doc
		set theURL to URL of doc
		
		tell me
			set AppleScript's text item delimiters to "/"
			--set folderList to GetTextItems(theURL, "/")
			set folderList to get every text item of theURL
			set folderList to reverse of folderList
			set AppleScript's text item delimiters to ""
		end tell
		
		tell me
			set listCount to count items in folderList
			repeat with i from 1 to listCount
				if item i of folderList ≠ "htdocs" then
					set end of newList to item i of folderList
					set counter to counter + 1
				else
					exit repeat
				end if
			end repeat
			
		end tell
		
		
		repeat with i from (count of items in newList) to 1 by -1
			set folderName to folderName & "/" & item i of newList
		end repeat
		
		
	on error
		display alert ¬
			"No active document." message ¬
			"You must have a document open" buttons {"OK"} ¬
			cancel button 1
	end try
end tell


--check whether Apache is running
set theResult to do shell script "/Applications/xampp/xamppfiles/xampp status"

set AppleScript's text item delimiters to "
"
set thelist to every text item of theResult
if item 2 of thelist ≠ "Apache is running." then
	display dialog "Please start Apache"
else
	set theURL to "http://127.0.0.1" & folderName as text
	tell application "Safari"
		make new document at front with properties {URL:theURL}
		activate
	end tell
	
end if

set AppleScript's text item delimiters to ""

Previous, obsolete, Macscript version


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

--dynamically add the path of the currently edited file to the safari URL
--file can be in any recursive subfolder of htdocs folder 

--use MacScript Library functions - Macsript.com Library
property parent : load script alias (((path to scripting additions from local domain) as text) & "Macscript.com Library")

global counter, safariURL

set newList to {}
set folderName to ""
set counter to 0
set safariURL to ""

tell application "BBEdit"
	activate
	
	try
		set doc to active document of text window 1
		set docName to name of doc
		set theURL to URL of doc
		
		tell me
			set folderList to GetTextItems(theURL, "/")
			set folderList to reverse of folderList
		end tell
		
		tell me
			set listCount to count items in folderList
			repeat with i from 1 to listCount
				if item i of folderList ≠ "htdocs" then
					set end of newList to item i of folderList
					set counter to counter + 1
				else
					exit repeat
				end if
			end repeat
			
		end tell
		
		
		repeat with i from counter to 2 by -1
			set folderName to folderName & "/" & item i of newList
		end repeat
		
		set safariURL to "http://127.0.0.1" & folderName & "/" & docName as text
		
		
		
		
	on error
		display alert ¬
			"No active document." message ¬
			"You must have a document open to rename one, silly!" buttons {"OK"} ¬
			cancel button 1
	end try
end tell

--check whether XAMPP is running
tell application "Finder"
	if application "manager-osx" is running then
		tell application "Safari"
			make new document at front with properties {URL:safariURL}
			activate
		end tell
	else
		activate "Finder"
		display dialog "XAMPP & Apache is not running"
	end if
end tell