2017-02-21 58 views
-1

有没有一种方法可以使用Swift从当前打开的浏览器(Chrome,Firefox或Safari)获取当前网址?在macOS中从浏览器获取当前网址

+0

什么代码,你写你自己尝试做到这一点?不要只是要求人们为你做这项工作。告诉我们你已经尝试了什么,什么不起作用,以表明你已经花时间去尝试帮助你自己了,它使我们无法提供明显的答案,最重要的是它可以帮助你获得更具体和相关的信息回答。另请参阅[Ask] –

+0

使用AppleScript可能会这样做。 –

+0

我发现[this](http://stackoverflow.com/a/6111592)方法,它使用AppleScript。但我无法正确地将其转换为Swift。特别是这一行 'NSString * result = [[NSString alloc] initWithCharacters:(unichar *)[data bytes] length:[data length]/sizeof(unichar)];'' – ttrs

回答

1

你可以使用一些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) 
相关问题