MacOS – Is it possible to maintain Chrome’s ⌘-shortcut for background tab, while using AppleScript to automate the creation of a new tab

applescriptgoogle-chromemacosshortcuttabs

I have a custom system Service on my Mac, entitled Google Search, that places the selected text within a defined URL and then opens the URL in a new tab (adjacent to the current tab) in Google Chrome.

My Service receives selected text in any application. The Service is exclusively triggered via the right-click context menu for selected text, systemwide and in all applications. No third-party app or keyboard shortcut is involved.

By default, whenever one clicks on a link that opens a new tab in Chrome while holding ⌘ command, the current tab in Chrome does not change. The new tab is opened to the right of and immediately adjacent to the current tab, but the new tab is not made the active tab.

I would like the ⌘ command key to have the same effect when I run my Service. So that:

if <the command key is being pressed when the Service is triggered> then
    Open URL in a new, adjacent tab.
    (Do not change the active tab.)
else
    Open URL in a new, adjacent tab.
    Change the active tab to the new tab.

My Service consists of one "Run AppleScript" action. Here is the full code:

on run {input, parameters}

(*
    When triggering this Service in applications other than Google Chrome, such as TextEdit, the Chrome window opens in the background. This command brings the Chrome window to the foreground:
*)
activate application "Google Chrome"

(*
    Converting the selected text to plain text to remove any formatting:
        From: http://lifehacker.com/127683/clear-text-formatting-on-os-x
*)
set selectedText to input
set selectedText to (selectedText as text)

(*
    Removing any line breaks and indentations in the selected text:
        From: http://stackoverflow.com/a/12546965 
*)

set AppleScript's text item delimiters to {return & linefeed, return, linefeed, character id 8233, character id 8232}
set plainTextSelectedText to text items of (selectedText as text)
set AppleScript's text item delimiters to {" "}
set plainTextSelectedText to plainTextSelectedText as text

(* Assigning variables: *)
set baseURL to "https://www.google.com/search?q="
set finalLink to baseURL & plainTextSelectedText

(* Opening webpage in Chrome: *)
(*
    The following tell block creates a new tab, located immediately after the currently open tab, which is what I want to occur.
        From: http://apple.stackexchange.com/questions/271702/applescript-how-to-open-a-link-in-google-chrome-in-a-new-adjacent-tab/271709#271709
*)
tell application "Google Chrome"
    activate
    tell front window to make new tab at after (get active tab) with properties {URL:finalLink} -- open a new tab after the current tab
end tell

end run

My complaint with the above code is that it sets the current tab to the new tab, even if ⌘ command is held down when the Service initiates.

Is it possible to have the current tab not be changed to the new tab if and only if the user holds down ⌘ command when the Service is run?

I only expect the ⌘ command key functionality to work when the right-click context menu is clicked in Chrome.app. For example, if this Service is triggered from within Preview.app, while it would be nice to still have at my disposal the ability to use the ⌘ command key to not change the active tab of the Chrome window, I understand that this is probably asking for too much.

I understand that AppleScript has no mechanism to check if a key is being pressed mid-script. However, I wonder if there is an alternate method to create a new tab in AppleScript that makes Chrome do all of the listening so that Chrome can respond to ⌘ command as it does naturally.

Best Answer

I think this will do what you're asking. I've modified your original code to see what process is frontmost at the time it's run, in order to branch and test appropriately based on the conditions expressed in your question by use of checkModifierKeys*. to see if the ⌘ command key was pressed when Google Chrome is the frontmost process at the time the service is run. *(I have no affiliation with Charles Poynton’s blog or Stefan Klieme’s checkModifierKeys other than to have been using this program for a few years without issue.)

As coded, it assumes that the checkModifierKeys is located in /usr/local/bin/. Modify as needed.

See the comments in the if theFrontmostProcessWhenRun is "Google Chrome" then block for its logic flow.

    on run {input, parameters}

        --  # Get the name of frontmost process at the time the services was run.
        --  #
        --  # This is used later in an if statement block for when if Google Chrome was frontmost process when run
        --  # to check that the value returned from checkModifierKeys was for the command key being pressed.

        tell application "System Events"
            set theFrontmostProcessWhenRun to get name of process 1 where frontmost is true
        end tell

        (*
    When triggering this Service in applications other than Google Chrome, such as TextEdit, the Chrome window opens in the background. This command brings the Chrome window to the foreground:
*)
        activate application "Google Chrome"

        (*
    Converting the selected text to plain text to remove any formatting:
        From: http://lifehacker.com/127683/clear-text-formatting-on-os-x
*)
        set selectedText to input
        set selectedText to (selectedText as text)

        (*
    Removing any line breaks and indentations in the selected text:
        From: http://stackoverflow.com/a/12546965 
*)

        set AppleScript's text item delimiters to {return & linefeed, return, linefeed, character id 8233, character id 8232}
        set plainTextSelectedText to text items of (selectedText as text)
        set AppleScript's text item delimiters to {" "}
        set plainTextSelectedText to plainTextSelectedText as text

        (* Assigning variables: *)
        set baseURL to "https://www.google.com/search?q="
        set finalLink to baseURL & plainTextSelectedText

        (* Opening webpage in Chrome: *)
        (*
    The following tell block creates a new tab, located immediately after the currently open tab, which is what I want to occur.
        From: http://apple.stackexchange.com/questions/271702/applescript-how-to-open-a-link-in-google-chrome-in-a-new-adjacent-tab/271709#271709
*)


        if theFrontmostProcessWhenRun is "Google Chrome" then
            --  # Google Chrome was the frontmost process when the service was run.
            if ((do shell script "/usr/local/bin/checkModifierKeys") as integer) is equal to 256 then
                --  # The command key was pressed when the service was run.
                tell application "Google Chrome"
                    --  # See Note: below.
                    set activeTab to active tab index of front window
                    tell front window to make new tab at after (get active tab) with properties {URL:finalLink} -- open a new tab after the current tab
                    set active tab index of front window to activeTab
                end tell
            else
                tell application "Google Chrome"
                    tell front window to make new tab at after (get active tab) with properties {URL:finalLink} -- open a new tab after the current tab
                end tell
            end if
        else
            --  # Google Chrome was not the frontmost process when the service was run.
            tell application "Google Chrome"
                tell front window to make new tab at after (get active tab) with properties {URL:finalLink} -- open a new tab after the current tab
            end tell
        end if

    end run

Note: When Google Chrome is frontmost at the time the service is run and the ⌘ command key is pressed, this gets the current active tab index and sets it back to it after making the new tab. This is intended as a workaround as it's a bit kludgy, but better then nothing until a more elegant solution to a problem can be found.