Firefox 使用AppleScript从web浏览器中最前面的窗口获取URL:最终列表

Firefox 使用AppleScript从web浏览器中最前面的窗口获取URL:最终列表,firefox,safari,applescript,flock,camino,Firefox,Safari,Applescript,Flock,Camino,我构建了一个[widget][1],它从Safari最前面的窗口获取URL,然后允许您使用tr.imAPI缩短它。工作起来很甜蜜 我想让它更灵活,所以我正在研究如何从其他浏览器获取URL。以下是在Safari中使用的AppleScript: tell application "Safari" return URL of front document as string end tell 之后,我确定以下内容可能适用于Firefox(尽管有人告诉我它不适用于他,可能与某些扩展冲突?) 注

我构建了一个[widget][1],它从Safari最前面的窗口获取URL,然后允许您使用tr.imAPI缩短它。工作起来很甜蜜

我想让它更灵活,所以我正在研究如何从其他浏览器获取URL。以下是在Safari中使用的AppleScript:

tell application "Safari"
    return URL of front document as string
end tell
之后,我确定以下内容可能适用于Firefox(尽管有人告诉我它不适用于他,可能与某些扩展冲突?)

注:以上是一个低于最佳实践的示例,取决于列表项的位置。有关Firefox的更好解决方案,请参见下文

我想做的是在这里建立一个列表,列出Mac上每一款现代浏览器的最终等价物:Opera、Camino、Flock等

更新:在我对这一主题的研究中,我发现了一条有用的线索。我下面的大部分答案都是基于这个讨论

更新2:我已将此页面上的AppleScript合并到[widget][1]中。它似乎正在工作。

Opera(在9.21和9.62版本上测试):

Firefox(在版本2.0.0.14和3.0.1上测试):

Camino(在1.6.4版上测试):

Flock(在2.0版上测试):

这取决于列表项的位置,但据我所知,这是获得此值的唯一方法。该属性名为
address
,尽管苹果的文档中没有这样说,但它在AppleScript中似乎是一个保留字。

OmniWeb(在5.8版上测试):


Firefox 3.03中目前存在一个bug,如果您以前使用过类似于以下的语句,它将从AppleScript中隐藏所有窗口属性,包括«class curl»:

tell application "Firefox" to activate

解决方法是使用以下代码:

tell application "System Events"
 tell process "Firefox"
  set frontmost to true
  set xsist to (front window) exists
  (* keep xsist value to test later on *)
 end tell
end tell

注意:在下一次重新启动Firefox之前,该窗口的属性将保持不可用

这是Piero用新id再次返回(我在尝试重新安装Firefox时丢失了cookie!!!)

我刚刚试过Firefox3.04,但appleScript的支持和可靠性没有改变。 还是一样的错误

我通过web进行的测试和搜索得出的结论是,您无法在同一脚本中访问窗口的名称和窗口的其他属性,如«class curl»

如果您正在使用窗口的名称,并且突然无法再访问它(获取随机二进制字符串),则必须再次调用此代码:

tell application "Firefox" to activate
在Firefox中使用任何会产生错误的语句也可以正常工作,使窗口名再次可用,但重新启动Mac不会改变任何事情

正如我前面提到的,一旦您完成了,您就不能再访问«class curl»,直到下一次重新启动Firefox

在Mac上为Firefox编写脚本真的是不可能的任务

如果你想在Firefox上支持AppleScript,告诉它,并投票支持这个bug

Camino 1.6及以上版本:

tell application "Camino"
    return URL of current tab of front browser window as text
end tell

与前面的答案不同,这将获得焦点选项卡的URL。

激活UI脚本并运行下面的代码。然后,您将把URL放在剪贴板中,并可以粘贴它

tell application "Firefox" to activate
tell application "System Events"
    keystroke "l" using command down
    keystroke "c" using command down
end tell

Google Chrome for Mac增加了获取URL的应用程序描述方法

这是Chromium AppleScript SDK

下面链接的页面中的示例: 更多示例如下:
多亏了上面的Brian,这是防弹版本

他的代码要求您粘贴URL,但此代码将URL设置为“FrontDocumentURL”,然后您可以将其用作脚本中的变量

tell application "Firefox" to activate

tell application "System Events"
keystroke "l" using command down
keystroke "c" using command down
end tell

set FrontDocumentURL to the clipboard

谢谢你的评论!我很高兴知道这可能会有所帮助,而且这个bug不是由于我的配置造成的。这个脚本总是返回Camino中第一个选项卡的URL,不管哪个选项卡有焦点。有更好的办法吗?亲爱的,谢谢!你知道这对Firefox也适用吗?不,我们的AppleScript实现与Firefox完全不同。
tell application "Firefox" to activate
tell application "Firefox"
 if (front window) exists then do_something()
end tell
tell application "System Events"
 tell process "Firefox"
  set frontmost to true
  set xsist to (front window) exists
  (* keep xsist value to test later on *)
 end tell
end tell
tell application "Firefox" to activate
tell application "Camino"
    return URL of current tab of front browser window as text
end tell
tell application "Firefox" to activate
tell application "System Events"
    keystroke "l" using command down
    keystroke "c" using command down
end tell
   tell application "Google Chrome"
     get URL of active tab of window 1
   end tell
tell application "Firefox" to activate

tell application "System Events"
keystroke "l" using command down
keystroke "c" using command down
end tell

set FrontDocumentURL to the clipboard