How to copy the current active browser URL?

I am trying to get the current active browser URL of the active browser window. Any pointers or sample code?

+3
source share
2 answers

the code:

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);
}

Conclusion:

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

I would think that this should be done through Applescript if the browser provides such information in its dictionary.

The following URL gives some useful examples of how to call Applescript from Objective-C: Objective-C and Applescript

+1
source

All Articles