我正在尝试为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
我错过了什么?
答案 0 :(得分:1)
这可能很棘手,但您可以使用键盘快捷键复制并打开Chrome中所需的网址。检查代码:
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(选择网址)和cmd + C(复制)相同。
此外,您可以调整代码以支持其他浏览器。
(来自http://vgable.com/blog/2009/04/24/how-to-check-if-an-application-is-running-with-applescript/的ApplicationIsRunning函数)
答案 1 :(得分: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
##############
欢呼声。
P.S。作为旁注,对此进行的一些调查显示,许多人在Safari中禁用Flash,然后在Chrome中触发复制的URL,这显然必须是一个更安全的容器。