Question

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.

Was it helpful?

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.

OTHER TIPS

If you want to just open that url, you would use something like this:

tell application "Safari"
    activate
    do JavaScript "window.open('http:// localhost/')" in document 1
end tell

hope that helps.

Here's a couple different options..

The first assumes that the file you want to open is at the root localhost directory. The script will instruct Safari to open the URL and append the filename to the path.

The first time you run this you may be required to authorize Mac Accessibility allowing Finder to complete the necessary actions.

try
    tell application "Finder" to set filePath to name of item 1 of (get selection)
        set the clipboard to filePath

        set localhost to "http://localhost/"

        tell application "Safari"
            activate
            tell application "System Events"
                tell process "Safari"
                    click menu item "New Tab" of menu "File" of menu bar 1

                end tell
            end tell
            set theURL to localhost & filePath
            set URL of document 1 to theURL
        end tell
    end tell
end try

Another option would be to copy the POSIX path of the selected file and open the local path in Safari..

try
    set filePath to {}
    tell application "Finder"
        repeat with objItem in (get selection)
            set end of filePath to POSIX path of (objItem as text)
        end repeat
    end tell

    set {strDelimeter, text item delimiters} to {text item delimiters, return}
    set the clipboard to filePath as text

    tell application "Safari" to open location filePath

end try
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top