BhishanPoudel
BhishanPoudel

Reputation: 17164

AppleScript for getting the frontmost url from Safari and open it in Google Chrome Incognito

I am trying to write an applescript that will get the current frontmost url from Safari and opens the same url in Google Chrome Incognito.

So far I know how to get the frontmost url from Safari, I am not sure how to open that url in Chrome incognito.

MWE

tell application "Safari" to return URL of front document
tell application "Safari" to return name of front document

References:

Upvotes: 0

Views: 782

Answers (2)

BhishanPoudel
BhishanPoudel

Reputation: 17164

If you don't mind using JavaScript, it's just few lines of code:

const chrome = Application('Google Chrome')
const safari_url = Application('Safari').documents[0].url()
chrome.Window({ mode: 'incognito' }).make()
chrome.windows[0].activeTab.url = safari_url

Upvotes: 0

user3439894
user3439894

Reputation: 7565

The following example AppleScript code assumes Safari is opened to a page with a URL and that Google Chrome exists on the system, however, if so, will do as you've requested.

Note: There is no error handling included to ensure Safari is opened to a page with a URL or whether or not Google Chrome exists on the system. That is for you to add.

tell application "Safari" to set theURL to the URL of the front document

do shell script "open -na 'Google Chrome' --args -incognito " & theURL's quoted form

The Safari line of code can also be written as:

tell application "Safari" to set theURL to the URL of the current tab of the front window

Here is an example with some error handling that uses UI Scripting and System Events instead of a do shell script command to open the incognito window in Google Chrome:

Example AppleScript code:

if running of application "Safari" then
    
    tell application "Safari"
        if the URL of ¬
            the current tab of ¬
                the front window ¬
                    does not contain missing value then
            set theURL to ¬
                the URL of ¬
                    the current tab of ¬
                        the front window
        else
            return
        end if
    end tell
    
    tell application "Google Chrome" to activate
    
    delay 1
    
    tell application "System Events" to ¬
        keystroke "n" using ¬
            {shift down, command down}
    
    delay 1
    
    tell application "Google Chrome" to ¬
        set the URL of ¬
            the active tab of ¬
                the front window to theURL
    
end if


Update to address comment:

... is there a way if there is an incongito window already open, then open new url in a new tab?

Example AppleScript code:

if running of application "Safari" then
    
    tell application "Safari"
        if the URL of ¬
            the current tab of ¬
                the front window ¬
                    does not contain missing value then
            set theURL to ¬
                the URL of ¬
                    the current tab of ¬
                        the front window
        else
            return
        end if
    end tell
    
    if running of application "Google Chrome" then
        tell application "Google Chrome"
            if mode of front window is "incognito" then
                make new tab at end of ¬
                    front window with properties {URL:theURL}
            end if
        end tell
    end if

end if


Update to address additional comments:

The following example AppleScript code contains the necessary error handling to accommodate normal possible scenarios of the state of Google Chrome in regards to opening the URL of the current tab of the front window of Safari in an incognito window/tab of Google Chrome:

Example AppleScript code:

if running of application "Safari" then
    
    tell application "Safari"
        if exists front window then
            if the URL of ¬
                the current tab of ¬
                    the front window ¬
                        does not contain missing value then
                set theURL to ¬
                    the URL of ¬
                        the current tab of ¬
                            the front window
            else
                return
            end if
        else
            return
        end if
    end tell
    
    if running of application "Google Chrome" then
        
        tell application "Google Chrome"
            set incognitoWindowIDs to ¬
                the id of every window ¬
                    whose mode is "incognito"
            if incognitoWindowIDs is {} then
                activate
                my openNewIncognitoWindow()
                delay 1
                set URL of ¬
                    the active tab of ¬
                        the front window to theURL
            else
                make new tab at end of ¬
                    window id (first item of incognitoWindowIDs) ¬
                        with properties {URL:theURL}
            end if
        end tell
    else
        tell application "Google Chrome"
            activate
            delay 1
            my openNewIncognitoWindow()
            delay 1
            set URL of ¬
                the active tab of ¬
                    the front window to theURL
        end tell
    end if
    
end if

on openNewIncognitoWindow()
    tell application "System Events" to ¬
        keystroke "n" using ¬
            {shift down, command down}
end openNewIncognitoWindow

Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

Upvotes: 2

Related Questions