以编程方式访问Web浏览器选项卡|斯威夫特3 [英] Access web browser tabs programatically | Swift 3

查看:135
本文介绍了以编程方式访问Web浏览器选项卡|斯威夫特3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以访问Safari或Google Chrome的开放标签? URL是好还是标签的标题或两者?

然后用户可以指定一些网站并向它们添加标签,应用程序将测量在这些网站上花费了多少,该应用程序将被允许通过可访问性。

解决方案

使用AppleScript获取每个标签的标题和URL。

您可以在Swift中使用 NSAppleScript 来运行AppleScript。



strong> Safari )

  let myAppleScript =set r to \\\\\
+
告诉应用程序\Safari \\\\
+
在窗口中用w重复\+
如果存在w的当前选项卡那么\ +
在w \ n的标签中重复t+
告诉t将r设置为r& \标题:\& name& \,URL :\& URL& linefeed\\\
+
end repeat \\\
+
end if \\\
+
end repeat \\\
+
end tel l\\\
+
return r

var error:NSDictionary?
let scriptObject = NSAppleScript(source:myAppleScript)
如果输出:NSAppleEventDescriptor = scriptObject?.executeAndReturnError(& error){
let titlesAndURLs = output.stringValue!
print(titlesAndURLs)
} else if(error!= nil){
print(error:\(error))
}






AppleScript 返回一个字符串,如下所示:


 标题:第一个标签的标题,URL:第一个标签的网址
标题:第二个标签的标题,URL:第二个标签的网址
标题:第三个标签的标题,URL:第三个标签的网址
....







一个例子> Google Chrome )

  let myAppleScript =set r to \\\\\
+
告诉应用程序\Google Chrome \\\\
+
在windows中用w重复\\
在w \ n的标签中重复t +
告诉您将r设置为r& \\Title:\& title& \,URL:\&URL& linefeed\\\
+
结束重复N +
结束repeat\\\
+
结束tell\\\
+
返回R
变种错误:NSDictionary的?
let scriptObject = NSAppleScript(source:myAppleScript)
如果输出:NSAppleEventDescriptor = scriptObject?.executeAndReturnError(& error){
let titlesAndURLs = output.stringValue!
print(titlesAndURLs)
} else if(error!= nil){
print(error:\(error))
}






更新:

您可以在脚本编辑器应用程序中运行它。

 将r设置为 - 一个用于附加字符串的空变量
告诉应用程序Safari
在窗口中用w重复 - loop对于每个窗口,w是一个包含窗口对象的变量
(如果存在)w的当前标签那么 - 是一个有效的浏览器窗口
在w - 标签中为每个标签重复t窗口,t是一个变量,它包含标签对象
- 获得该标签的标题(名称)并获取该标签的url
告诉t将r设置为r& 标题:&名称& ,网址:& URL& linefeed - 在变量(r)后附加一行
(*
'linefeed'表示换行符
'tell t'表示w(窗口)的标签
' &'用于连接字符串,与Swift中的+运算符相同
*)
结束重复
结束如果
结束重复
结束tell
返回r - 返回字符串(每行包含一个标题和一个URL)


Is it possible to access the open tabs of Safari or Google Chrome? A URL would be good or the title of the tab or both?

The purpose of the app then the user could specify some websites and add labels to them and the app would measure how much is spent on those websites, the app would be allowed through accessibility.

解决方案

Use an AppleScript to get the title and the URL of each tab.

You can use NSAppleScript in Swift to run an AppleScript.

An example (Safari)

let myAppleScript = "set r to \"\"\n" +
    "tell application \"Safari\"\n" +
    "repeat with w in windows\n" +
    "if exists current tab of w then\n" +
    "repeat with t in tabs of w\n" +
    "tell t to set r to r & \"Title : \" & name & \", URL : \" & URL & linefeed\n" +
    "end repeat\n" +
    "end if\n" +
    "end repeat\n" +
    "end tell\n" +
"return r"

var error: NSDictionary?
let scriptObject = NSAppleScript(source: myAppleScript)
if let output: NSAppleEventDescriptor = scriptObject?.executeAndReturnError(&error) {
    let titlesAndURLs = output.stringValue!
    print(titlesAndURLs)
} else if (error != nil) {
    print("error: \(error)")
}


The AppleScript return a string, like this:

Title : the title of the first tab, URL : the url of the first tab
Title : the title of the second tab, URL : the url of the second tab
Title : the title of the third tab, URL : the url of the third tab
....


An example (Google Chrome)

let myAppleScript = "set r to \"\"\n" +
    "tell application \"Google Chrome\"\n" +
    "repeat with w in windows\n" +
    "repeat with t in tabs of w\n" +
    "tell t to set r to r & \"Title : \" & title & \", URL : \" & URL & linefeed\n" +
    "end repeat\n" +
    "end repeat\n" +
    "end tell\n" +
"return r"
var error: NSDictionary?
let scriptObject = NSAppleScript(source: myAppleScript)
if let output: NSAppleEventDescriptor = scriptObject?.executeAndReturnError(&error) {
    let titlesAndURLs = output.stringValue!
    print(titlesAndURLs)
} else if (error != nil) {
    print("error: \(error)")
}


Update:

Here's the AppleScript with the comments.

You can run it in the "Script Editor" application.

set r to "" -- an empty variable for appending a string
tell application "Safari"
    repeat with w in windows -- loop for each window, w is a variable which contain the window object
        if exists current tab of w then --  is a valid browser window
            repeat with t in tabs of w -- loop for each tab of this window, , t is a variable which contain the tab object
                -- get the title (name) of this tab and get the url of this tab
                tell t to set r to r & "Title : " & name & ", URL : " & URL & linefeed -- append a line to the variable (r)
                (*
                 'linefeed'  mean a line break
                 'tell t' mean a tab of w (window)
                 '&'  is for  concatenate strings, same as the + operator in Swift
                *)
            end repeat
        end if
    end repeat
end tell
return r -- return the string (each line contains a title and an URL)

这篇关于以编程方式访问Web浏览器选项卡|斯威夫特3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆