‘RunPHP’ AppleScript – send current TextMate doc as a URL thru Safari to Apache

…for an improved version of this solution where the RunPHP script is initiated from within a TextMate menu item and/or with a TextMate keyboard shortcut please click the following link: → ‘Launch AppleScripts from TextMate menus’

A quick AppleScript to show the usage of MacScript.com Library functions in simplifying running a PHP file through a local Web Server for testing using TextMate.

The script formats the window name of the currently displayed Textmate 2 document (.php) being edited and saved in a ‘php’ folder under ‘htdocs’, the web folder for the local XAMPP Apache Server. The document name and path to the local server are then formatted into a URL and automatically presented to Safari, which locates said php file on the server and accesses the processed output.

One way to run this script is from the FastScripts menu.


--RunPHP.scpt  
--create a <named>.php file in any subfolder of xamppfile/htdocs and start XAMPP and the apache server
--to test your <named>.php file, run this script from the Fastscripts menu (RunPHP) 
--with the <named>.php file open in textmate 2


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

--uses MacScript Library functions. Macsript.com Library available for download on this website
property parent : load script alias (((path to scripting additions from local domain) as text) & "Macscript.com Library")


tell application "TextMate"
	get name of window 1
	set theName to the result
	
end tell

tell me
	set myCount to count characters in theName
	GetOffsetInString("—", theName)
	set theCount to the result
	TruncateString(theName, theCount - 2)
	set docName to the result
	TruncateString(theName, -(myCount - (theCount + 1)))
	set docPath to the result
	
	
end tell

--check whether XAMPP is running
tell application "Finder"
	if application "manager-osx" is running then
		set theURL to "http://127.0.0.1/" & docPath & "/" & docName as text
		tell application "Safari"
			make new document at front with properties {URL:theURL}
			activate
		end tell
	else
		activate "Finder"
		display dialog "XAMPP & Apache is not running"
	end if
end tell


The script use the GetOffsetInString and TruncateString functions from the MacScript.com Library


RunPHP Quick Action via Automator

Alternatively the above AppleScript can be modified a little and added to an Automator Workflow and saved as an ‘Action’ so that any php file can be selected in the Finder and run through Safari and the Web Server. Right clicking on the selected file brings up the the Quick Actions Menu from which you can RunPHP.

Whereas in the previous script where we had to finesse the name of a Window rather than a file name (due to TextMate’s poor scriptability), here we can get the name of the file directly from the Finder, so we don’t need the TruncateString function from the MacScript.com Library nor need to load it.

In a new Automator file add an empty ‘Run Applescript’ Action to the workflow by dragging this item from the list in the library into the workflow and set ‘Workflow receives current files or folders’ in ‘Finder’. Add the following Applescript between the ‘on run’ and ‘end run’ statements:


tell application "Finder"
		set theFilename to selection
		set theName to name of item 1 of theFilename
	end tell
	
	
	--check whether XAMPP is running
	tell application "Finder"
		if application "manager-osx" is running then
			set theURL to "http://127.0.0.1/" & "php" & "/" & theName as text
			tell application "Safari"
				make new document at front with properties {URL:theURL}
				activate
			end tell
		else
			activate "Finder"
			display dialog "XAMPP & Apache is not running"
		end if
	end tell