从macOS中的浏览器获取当前URL

时间:2017-02-21 13:52:31

标签: swift macos

有没有办法使用Swift从当前打开的浏览器(Chrome,Firefox或Safari)获取当前网址?

2 个答案:

答案 0 :(得分:2)

你可以使用这样的Apple Script

set myURL to "No browser active"
set nameOfActiveApp to (path to frontmost application as text)
if "Safari" is in nameOfActiveApp then
    tell application "Safari"
        set myURL to the URL of the current tab of the front window
    end tell
else if "Chrome" is in nameOfActiveApp then
    tell application "Google Chrome"
        set myURL to the URL of the active tab of the front window
    end tell
end if

display dialog (myURL)

答案 1 :(得分:0)

使用AppleScript的快速解决方案

"${PWD##*/}"