Ich habe auf meinem Mac einen benutzerdefinierten Systemdienst mit dem Titel Google Search , der den ausgewählten Text in eine definierte URL einfügt und die URL dann in einem neuen Tab (neben dem aktuellen Tab) in Google Chrome öffnet.
Mein Service erhält ausgewählt text
in any application
. Der Dienst wird ausschließlich über das Rechtsklick-Kontextmenü für ausgewählten Text systemweit und in allen Anwendungen ausgelöst. Es ist keine Drittanbieter-App oder Tastenkombination beteiligt.
Wenn Sie auf einen Link klicken, der eine neue Registerkarte in Chrome öffnet, während Sie gedrückt halten ⌘ command, ändert sich die aktuelle Registerkarte in Chrome standardmäßig nicht. Die neue Registerkarte wird rechts neben und unmittelbar neben der aktuellen Registerkarte geöffnet, aber die neue Registerkarte wird nicht zur aktiven Registerkarte gemacht.
Ich möchte, dass der ⌘ command Schlüssel denselben Effekt hat, wenn ich meinen Dienst ausführe. So dass:
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.
Mein Dienst besteht aus einer Aktion „AppleScript ausführen“. Hier ist der vollständige 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
Meine Beschwerde mit dem obigen Code ist, dass er die aktuelle Registerkarte auf die neue Registerkarte setzt, selbst wenn ⌘ commander beim Starten des Dienstes gedrückt gehalten wird.
Ist es möglich, dass die aktuelle Registerkarte nicht in die neue Registerkarte geändert wird, wenn und nur wenn der Benutzer gedrückt hält, ⌘ commandwährend der Dienst ausgeführt wird?
Ich erwarte nur, dass die ⌘ commandSchlüsselfunktionalität funktioniert, wenn in Chrome.app auf das Kontextmenü mit der rechten Maustaste geklickt wird. Wenn dieser Dienst beispielsweise innerhalb von Preview.app ausgelöst wird, wäre es zwar schön, wenn ich immer noch die Möglichkeit hätte, die ⌘ commandTaste zu verwenden, um die aktive Registerkarte des Chrome-Fensters nicht zu ändern, aber ich verstehe, dass dies wahrscheinlich verlangt wird zu viel.
Ich verstehe, dass AppleScript keinen Mechanismus hat, um zu überprüfen, ob eine Taste mitten im Skript gedrückt wird. Ich frage mich jedoch, ob es eine alternative Methode zum Erstellen einer neuen Registerkarte in AppleScript gibt, die Chrome dazu bringt, die gesamte Überwachung zu übernehmen, damit Chrome wie ⌘ commandgewohnt darauf reagieren kann.
Ich denke, das wird tun, was Sie fragen. Ich habe Ihren ursprünglichen Code geändert , um zu sehen, welcher Prozess zum Zeitpunkt der Ausführung am vordersten ist , um auf der Grundlage der in Ihrer Frage durch die Verwendung von * ausgedrückten Bedingungen entsprechend zu verzweigen und zu testen . um zu sehen, ob die Taste gedrückt wurde, als Google Chrome zum Zeitpunkt der Ausführung des Dienstes der vorderste Prozess war . * (Ich habe keine Verbindung zu Charles Poyntons Blog oder Stefan Kliemes checkModifierKeys, außer dass ich dieses Programm seit einigen Jahren ohne Probleme verwende.)checkModifierKeys
⌘ command
Wie codiert geht es davon aus, dass sich das checkModifierKeys
in befindet /usr/local/bin/
. Ändern Sie nach Bedarf.
Siehe die Kommentare im if theFrontmostProcessWhenRun is "Google Chrome" then
Block für seinen logischen Ablauf .
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
Hinweis: Wenn Google Chrome zum Zeitpunkt der Ausführung des Dienstes im Vordergrund steht und die Taste gedrückt wird, erhält dies den aktuellen und setzt ihn nach dem Erstellen des neuen Tabs wieder darauf zurück . Dies ist als Problemumgehung gedacht, da es etwas klobig ist, aber besser als nichts, bis eine elegantere Lösung für ein Problem gefunden werden kann.⌘ commandactive tab index
checkModifierKeys
oder Ihrer Lösung zu tun; Es hat damit zu tun, wie AppleScript einen Hintergrund-Tab in Chrome öffnet. Gibt es eine andere Methode, um einen Hintergrund-Chrome-Tab in AppleScript zu öffnen? Diese Frage verdient wahrscheinlich einen eigenen, separaten Beitrag ...active tab index
, um ihn zurückzusetzen, nachdem der Fokus seit seiner Erstellung
auf die neue Registerkarte gesetzt wurde.checkModifierKeys
.)
Rubiks Sphäre