Search code examples
macosapplescriptmamptextwrangler

Open php file in browser using http://localhost with applescript


I'm trying to write an Applescript for textwrangler that will open the active document in Chrome. This is what the script looks like at the moment:

tell application "TextWrangler" to set theFile to file of document 1
tell application "Finder" to open theFile using (path to application "Google Chrome")

Let's say I an working on a file with the absolute path 'Applications/MAMP/www/index.php'. The script will open that file in the browser as 'file:// localhost/Applications/MAMP/www/index.php', showing the php code.

Instead of this I need a script that will replace 'file:// localhost/Applications/MAMP/' with 'http:// localhost/' showing the actual site.

I have tried a bunch of stuff that I found online, but I have too little experience with Applescript to achieve this.


Solution

  • How about this, following Chase's lead and using javascript to do the string replacing.

    property delim : "MAMP" -- where to break the path and prepend localhost to
    
    tell application "TextWrangler" to set theFile to file of document 1
    
    tell application "Finder" to set p to the POSIX path of theFile -- convert it into a Unix path first
    
    tell application "Google Chrome" to execute front window's active tab javascript ("pth='" & p & "';window.open('http://localhost'+ pth.split('" & delim & "').pop());")
    

    Makes for hard to read code with the multi language concatenation and string delimiting, but it should work.