15

我构建了一个[widget][1],它从 Safari 最前面的窗口中获取 URL,然后允许您使用 tr.im API 缩短它。像一样甜。

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

tell application "Safari"
    return URL of front document as string
end tell

经过一番挖掘,我确定以下内容可能适用于 Firefox(尽管有人告诉我这对他不起作用,可能与某些扩展发生冲突?):

tell application "Firefox"
    set myFirefox to properties of front window as list
    return item 3 of myFirefox
end tell

注意:以上是一个不太理想的实践示例,依赖于列表项的位置。有关 Firefox 的更好解决方案,请参见下文。

我想做的是在这里为 Mac 上的每个现代浏览器建立一个明确的等价物列表:Opera、Camino、Flock 等。

更新:在我对该主题的研究中,我在MacOSXHints.com上遇到了一个有用的帖子。我下面的大部分答案都是基于该讨论。

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

4

11 回答 11

3

Google Chrome for Mac 添加了用于获取 URL 的 AppleScripting 方法。

这是 Chromium AppleScript SDK

https://sites.google.com/a/chromium.org/dev/developers/design-documents/applescript

下面链接的页面示例:

   tell application "Google Chrome"
     get URL of active tab of window 1
   end tell

这里有更多例子:

http://laclfyoshi.blogspot.com/2010/10/google-chrome-ver.html

于 2011-03-07T06:01:53.147 回答
3

激活 UI 脚本并运行以下代码。然后,您将在剪贴板中拥有该 URL,您可以将其粘贴。

tell application "Firefox" to activate
tell application "System Events"
    keystroke "l" using command down
    keystroke "c" using command down
end tell
于 2009-10-21T04:38:47.970 回答
2

这又是 Piero 带着新 id 回来了(我在尝试重新安装 Firefox 时丢失了我的 cookie !!!)。

我刚刚尝试了 Firefox 3.04,关于 appleScript 的支持和可靠性没有任何改变。还是一样的bug...

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

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

tell application "Firefox" to activate

使用任何会在 Firefox 中产生错误的语句也可以正常工作,以使窗口名称再次可用,但重新启动 Mac 不会改变任何东西!

一旦你这样做了,正如我之前提到的,你不能再访问 «class curl»,直到下一次 Firefox 重新启动......

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

如果您希望 Firefox 支持 AppleScript,请告诉它,并为这个错误投票!!!

https://bugzilla.mozilla.org/show_bug.cgi?id=464701

于 2008-11-13T16:19:14.170 回答
2

当前 Firefox 3.03 中存在一个错误,如果您之前使用过如下语句,它将从 AppleScript 隐藏所有窗口属性,包括 «class curl»:

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

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

于 2008-11-07T10:12:48.457 回答
2

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

tell application "Firefox"
    set myURL to «class curl» of window 1
    return myURL
end tell
于 2008-11-06T10:15:53.893 回答
1

卡米诺 1.6 及以上:

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

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

于 2009-04-29T00:53:02.450 回答
1

感谢上面的 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
于 2013-01-04T18:18:23.973 回答
0

OmniWeb(在 5.8 版上测试):

tell application "OmniWeb"
    set myInfo to GetWindowInfo
    return item 1 of myInfo
end tell
于 2008-11-06T10:46:46.033 回答
0

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

tell application "Opera"
    return URL of front document as string
end tell
于 2008-11-06T10:13:29.610 回答
0

卡米诺(在 1.6.4 版本上测试):

tell application "Camino"
    set p to properties of front tab of front window
    return |currentURI| of p as string
end tell
于 2008-11-06T10:21:00.967 回答
0

Flock(在 2.0 版上测试):

tell application "Flock"
    set p to properties of front window as list
    return item 3 of p
end tell

这取决于列表项的位置,但据我所知,这是获得该值的唯一方法。该属性的名称address虽然 Apple 的文档没有说明,但它似乎是 AppleScript 中的保留字。

于 2008-11-06T10:26:00.287 回答