0

我正在尝试为 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 的经验太少,无法实现这一点。

4

3 回答 3

0

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

第一个假设您要打开的文件位于根 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
于 2013-11-18T05:58:39.217 回答
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());")

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

于 2013-08-10T04:38:38.810 回答
0

如果你只想打开那个 url,你会使用这样的东西:

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

希望有帮助。

于 2013-08-09T23:08:50.077 回答