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.