Launch ‘Apache Runner’ AppleScript (or any AppleScript) from TextMate Menus

You can run any AppleScript on your system from TextMate by following this method, though the focus here is on the ‘recursive’ use of Applescript – i.e. the use of an external AppleScript launched from a TextMate menu to act upon TextMate itself and do something with its currently active document.

The AppleScript I wrote (in a previous blogpost here →‘RunPHP from TextMate’), was launchable from the FastScripts menu in the Apple Status bar menu (at top right of the screen), however you can’t attach a keyboard shortcut to it without first paying the very high price of $40 to redsweater.com, the owners of FastScripts, to gain this extra feature!

Having said that, navigating the mouse across the screen in order to launch a script that runs the currently edited TextMate document through the local XAMPP Apache server becomes very annoying very quickly, particularly when such tests may have to be run repeatedly.
 

There is a better way

Add a new command to an existing Bundle menu item

In TextMate 2 select Edit Bundles… from the Bundles menu and then select the PHP item.
Select File, New from the TextMate menu and add a New Command. Call it Apache Run

Click on the Apache Run command , which has been added to the bottom of the command list, and enter the following code for it to execute, ensuring the path to your Applescript file in the osascript string is changed to the location of your file:


#!/usr/bin/env ruby

require ENV['TM_SUPPORT_PATH'] + '/lib/escape.rb'

def terminal_script_filepath
  %|tell application "Terminal"
    
      activate
      do shell script "osascript '/Users/paulvallance/Scripts/RunPHP.scpt'"
   
    quit    
    end tell|
end

open("|osascript", "w") { |io| io << terminal_script_filepath }

 

  • set the name to Apache Run
  • set the scope to html.php.js
  • set the key equivalent to Cmd-R
  • set save = nothing
  • set input = document
  • output = discard

before closing and saving the update to the bundle, right click on your new command and copy its ‘UUID’

Put your custom command at the top of the PHP menu

In Finder, hold down the option key and click on Library in the Go menu , then navigate to Application Support / TextMate / Bundles / Managed / Bundles folder and right click on PHP.tmbundle and show package contents. Duplicate (backup) the info.plist file and then edit it and add your UUID to the top of the list of UUIDs. The UUID order determines the menu items order.
See the following image:

Best way to Remove Key Equivalent Conflicts

The original Run command in this PHP Bundle is also set to Cmd-R so you’ll have to change it or just remove its Key Equivalent for the new one to work. The easiest way is to choose the Select Bundle Item… menu and scroll to both commands in the Action list, select each one and change the Key Equivalents there. Set the Apache Run command to Cmd-R and the original Run command (phpMate version) to Cmd-Shift-R.

In my ignorance I initially set the Key Equivalents this way:

In the Commands subdirectory of the package, duplicate the Run PHP.plist and rename the copy to Apache Run.plist and add the @R Keyboard Equivalent as well as the UUID of your custom command from the previous step above to get this:


{	autoScrollOutput = :true;
	beforeRunningCommand = "nop";
	command = "#!/bin/sh\nruby18 -- \"$TM_BUNDLE_SUPPORT/lib/escape.rb\"";
	input = "document";
	inputFormat = "text";
	keyEquivalent = "@R";
	name = "Apache Run";
	outputCaret = "afterOutput";
	outputFormat = "html";
	outputLocation = "newWindow";
	scope = "text.html.php";
	semanticClass = "process.run.php";
	uuid = "B66C4FB9-1680-4AFB-9729-FFCCC3EAD65F";
	version = 2;
}

 

I also changed the command line to reflect the use of escape.rb rather than phpMate.rb in the code section of the bundle command editor. Whether this is necessary I don’t know.

Remove the @R from Run PHP.plist so that it looks as follows:


keyEquivalent = "";

 

For completeness the RunPHP script used is listed below:

RunPHP.scpt


--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