2017-02-21 89 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)