从终端获取打开的标签/标签的URL


2

我在Google Chrome中打开了一个标签。是否有可能从终端获取或检查它的URL?如果没有,automator脚本怎么样?

也会对标题感到满意。

Answers:


4

要通过命令行在Google Chrome中获取标签的网址或标题,您可以在终端osascript中执行AppleScript 语句或程序文件

要查看Google Chrome AppleScript词典中的可用内容,可以使用“文件”>“打开词典...”命令在“脚本编辑器”中打开和查看词典。

这是一个简单的示例,用于获取最前面的Google Chrome窗口的第一个标签的网址:

osascript -e 'tell application "Google Chrome" to get URL of tab 1 of window 1'

对于相同条件下的标题:

osascript -e 'tell application "Google Chrome" to get title of tab 1 of window 1'

在相同的条件下同时获得它们:

osascript -e 'tell application "Google Chrome" to get {title, URL} of tab 1 of window 1'

根据你正在尝试完成的确切内容,语句可能会更复杂,并处理多个Tabs和/或Windows等。虽然这是使用osascript调用程序文件经文使用-e statement可能会更容易。


当此页面是目标选项卡时,这是终端的输出:

$ osascript -e 'tell application "Google Chrome" to get URL of tab 1 of window 1'
http://apple.stackexchange.com/questions/232565/get-url-of-opened-tab-tabs-from-terminal
$ osascript -e 'tell application "Google Chrome" to get title of tab 1 of window 1'
osx - Get URL of opened tab / tabs from terminal - Ask Different
$ osascript -e 'tell application "Google Chrome" to get {title, URL} of tab 1 of window 1'
osx - Get URL of opened tab / tabs from terminal - Ask Different, http://apple.stackexchange.com/questions/232565/get-url-of-opened-tab-tabs-from-terminal
$ 

非常便利。Safari的轻微变化:tell application "Safari" to get {name, URL} of tab 1 of window 1虽然由于某种原因,URL通常是“缺失值”。
Heath Raftery
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.