Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dongyuwei/a1c9d67e4af6bbbd999c to your computer and use it in GitHub Desktop.
Save dongyuwei/a1c9d67e4af6bbbd999c to your computer and use it in GitHub Desktop.
Applescript to get frontmost tab’s url and title of various browsers.
# Keep in mind that when asking for a `return` after another, only the first one will be output.
# This example is meant as a simple starting point, to show how to get the information in the simplest available way.
# Google Chrome
tell application "Google Chrome" to return URL of active tab of front window
tell application "Google Chrome" to return title of active tab of front window
# Google Chrome Canary
tell application "Google Chrome Canary" to return URL of active tab of front window
tell application "Google Chrome Canary" to return title of active tab of front window
# Chromium
tell application "Chromium" to return URL of active tab of front window
tell application "Chromium" to return title of active tab of front window
# Safari
tell application "Safari" to return URL of front document
tell application "Safari" to return name of front document
# Webkit
tell application "Webkit" to return URL of front document
tell application "Webkit" to return name of front document
# This example will return both the URL and title for the frontmost tab of the active browser, separated by a newline.
# Keep in mind that by using `using terms from`, we’re basically requiring that referenced browser to be available on the system
# (i.e., to use this on "Google Chrome Canary" or "Chromium", "Google Chrome" needs to be installed).
# This is required to be able to use a variable in `tell application`. If it is undesirable, the accompanying example should be used instead.
tell application "System Events" to set frontApp to name of first process whose frontmost is true
if (frontApp = "Safari") or (frontApp = "Webkit") then
using terms from application "Safari"
tell application frontApp to set currentTabUrl to URL of front document
tell application frontApp to set currentTabTitle to name of front document
end using terms from
else if (frontApp = "Google Chrome") or (frontApp = "Google Chrome Canary") or (frontApp = "Chromium") then
using terms from application "Google Chrome"
tell application frontApp to set currentTabUrl to URL of active tab of front window
tell application frontApp to set currentTabTitle to title of active tab of front window
end using terms from
else
return "You need a supported browser as your frontmost app"
end if
return currentTabUrl & "\n" & currentTabTitle
@dongyuwei
Copy link
Author

http://stackoverflow.com/questions/6111275/how-to-copy-the-current-active-browser-url

do this in objc

NSAppleScript *script= [[NSAppleScript alloc] initWithSource:@"tell application \"Google Chrome\" to return URL of active tab of front window"];
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);
}

NSAppleScript *script= [[NSAppleScript alloc] initWithSource:@"tell application \"Safari\" to return URL of front document as string"];

@dongyuwei
Copy link
Author

NSURLComponents *components = [[NSURLComponents alloc] initWithString:@"http://stackoverflow.com:8080/questions/2333972/objective-c-parse-domain-name-from-url-string"];
    NSLog(@"%@://%@:%@", components.scheme, components.host, components.port);

@nickav
Copy link

nickav commented Jun 8, 2016

When running this script if you have Parallels and Google Chrome installed you may get the following message: "A property can’t go after this identifier." To fix this, just remove the Google Chrome shortcut from ~/Applications (Parallels)/Windows 7 Applications/Google Chrome
I know this is a very specific error, but hopefully this saves someone time.

@pradeepb28
Copy link

is it possible to store window object using objc or in applescript in userdefaults?

@golangcpp
Copy link

For chrome - in windows : echo PlaceholderChromeURL, in mac : osascript -e 'tell application "Google Chrome" to return URL of active tab of front window', For Firefox - echo PlaceholderFirefoxURL & osascript -e 'tell application "Firefox" to activate' -e 'tell application "System Events" to keystroke "l" using command down' -e 'tell application "System Events" to keystroke "c" using command down' -e 'delay 0.5' -e 'the clipboard'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment