使用http:// localhost with applescript在浏览器中打开php文件

时间:2013-08-09 22:49:16

标签: macos applescript mamp textwrangler

我正在尝试为textwrangler编写一个Applescript,它将在Chrome中打开活动文档。这就是脚本目前的样子:

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

假设我正在使用绝对路径'Applications / MAMP / www / index.php'处理文件。该脚本将在浏览器中以“file:// localhost / Applications / MAMP / www / index.php”打开该文件,显示php代码。

而不是这个,我需要一个脚本,将'file:// localhost / Applications / MAMP /'替换为显示实际网站的'http:// localhost /'。

我尝试了一些我在网上找到的东西,但是我对Applescript的经验太少了。

3 个答案:

答案 0 :(得分:0)

如果您想打开该网址,可以使用以下内容:

tell application "Safari"
    activate
    do JavaScript "window.open('http:// localhost/')" in document 1
end tell
希望有所帮助。

答案 1 :(得分:0)

这是怎么回事,跟随Chase的领先并使用javascript来替换字符串。

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());")

使用多语言连接和字符串分隔使代码难以读取,但它应该可以工作。

答案 2 :(得分:0)

这里有几个不同的选择..

第一个假定您要打开的文件位于root localhost目录中。该脚本将指示Safari打开 URL并将文件名附加到路径。

第一次运行此时,您可能需要授权Mac Accessibility,以便Finder完成必要的操作。

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

另一种选择是复制所选文件的POSIX路径并在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