2011-05-24 49 views

回答

11

代碼:

NSAppleScript *script= [[NSAppleScript alloc] initWithSource:@"tell application \"Safari\" to return URL of front document as string"]; 
NSDictionary *scriptError = nil; 
NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&scriptError]; 
if(scriptError) { 
    NSLog(@"Error: %@",scriptError); 
} else {   
    NSAppleEventDescriptor *unicode = [descriptor coerceToDescriptorType:typeUnicodeText]; 
    NSData *data = [unicode data]; 
    NSString *result = [[NSString alloc] initWithCharacters:(unichar*)[data bytes] length:[data length]/sizeof(unichar)]; 
    NSLog(@"Result: %@",result); 
} 

輸出:

Result: http://stackoverflow.com/questions/6111275/how-to-copy-the-current-active-browser-url/6111592#6111592 
+0

我很高興它工作:) – Anne 2011-05-24 14:19:59

+1

對於Chrome,使用上面的代碼,但用下面的代碼替換applescript:「告訴應用程序谷歌瀏覽器」獲取前窗活動標籤的URL作爲字符串「 。 – ThomasW 2011-05-24 14:29:39

+0

不幸的是Firefox AppleScript支持早已被破解:https://bugzilla.mozilla.org/show_bug.cgi?id=125419 – ThomasW 2011-05-24 14:32:31

1

如果瀏覽器在其字典中公開這些信息,我認爲這將通過Applescript來完成。

以下網址給出瞭如何從Objective-C的調用AppleScript的一些有用的例子: Objective-C & Applescript

+0

確實在Windows AppleScript的運行或linux。我認爲它只適用於MAC – 2016-05-30 12:32:37

相關問題