1

我正在尝试为 Apptivate 编写一个 AppleScript,以在 Google Chrome 中打开当前的 Firefox 4 或 Safari 选项卡。Safari 部分有效,if 语句进入 Firefox,但它不复制 URL。

这是我的applescript,它不起作用:

tell application "System Events"
set myApp to name of first application process whose frontmost is true
if myApp is "firefox-bin" then
    tell application "Firefox" to set theURL to the «class curl» of frontmost of window 1
    tell application "Google Chrome"
        activate
        tell application "System Events" to keystroke "t" using {command down}
        set URL of last tab of window 1 to theURL
    end tell
else if myApp is "Safari" then
    tell application "Safari" to set theURL to the URL in document 1
    tell application "Google Chrome"
        activate
        tell application "System Events" to keystroke "t" using {command down}
        set URL of last tab of window 1 to theURL
    end tell
end if
end tell

我错过了什么?

4

2 回答 2

1

这可能很棘手,但您可以使用键盘快捷键在 Chrome 中复制和打开所需的 URL。检查代码:

set backupClipboard to the clipboard

on ApplicationIsRunning(appName)
    tell application "System Events" to set appNameIsRunning to exists (processes where name is appName)
    return appNameIsRunning
end ApplicationIsRunning

on copyURL(appName)
    tell application appName to activate
    tell application "System Events"
        keystroke "lc" using command down
    end tell
    delay 0.2 -- to make sure keystroke will hit cmd+l & cmd+c
end copyURL

if ApplicationIsRunning("firefox-bin") then
    copyURL("Firefox")
else if ApplicationIsRunning("Safari") then
    copyURL("Safari")
else
    return
end if

tell application "Google Chrome"
    open location (the clipboard)
    activate
end tell

set the clipboard to backupClipboard

此脚本将首先保存您当前的剪贴板,然后检查打开的浏览器。您可以通过对 if 语句进行排序来定义浏览器的优先级。

这与在浏览器上按 cmd+L(选择 url)和 cmd+C(复制)相同。

此外,您可以调整代码以支持其他浏览器。

(ApplicationIsRunning 函数来自:http: //vgable.com/blog/2009/04/24/how-to-check-if-an-application-is-running-with-applescript/

于 2011-02-07T20:22:50.747 回答
0

我在 PPC 上运行 OS 10.4.11,我似乎发现脚本有问题,比如“foo tab”。我认为问题是我有这么旧的设备,脚本无法识别这些新命令。也许是我。

无论如何,我希望能够使用我的 Magic Mouse 触发从 Firefox 到 Safari 的 URL 副本,运行 magicmouse.jp 驱动程序。我使用以下方法让它工作:

tell application "Firefox" to activate

tell application "System Events"

keystroke "lc" using command down

end tell



tell application "Safari" to activate

delay 1

tell application "System Events" to keystroke "l" using {command down}

--delay 0.2

tell application "System Events" to keystroke "v" using {command down}

tell application "System Events" to keystroke return

##############

干杯。

PS 附带说明一下,对此进行的一些研究表明,一些人在 Safari 中禁用了 flash,然后触发复制的 URL 在 Chrome 中打开,这显然必须是一个更安全的容器,因为它的价值。

于 2012-08-22T21:12:30.407 回答