I have an Automator Service that is comprised of a single "Run AppleScript" action. The AppleScript basically consists of a series of dialogs.
In the middle of the script, the script relies on keystroking ⌘ command + C in order to get the selected rich text into the script as the clipboard
. This copy command must fall in the middle of the script, as the user might want to paste the real clipboard into the initial dialog.
AppleScript dialogs in Services are by default pinned on top of all windows; you cannot change this.
AppleScript is only able to keystroke ⌘ command + C when the underlying app is in focus, which makes sense, considering that it is the underlying app -- not the script dialog -- that contains the selected text.
So, I'll put my question inelegantly: Is there a way to make the top part of the window immediately behind the Service dialog gray and thus make the top bar of the Service dialog white, while still not moving the script dialog out of view?
I cannot go by the title of the window or the app of the window, because these variables will vary. The constant is that the window that I want to bring into focus will always be immediately behind the Service dialog.
The following is an example of what I'm talking about:
-- Back up original clipboard contents:
set savedClipboard to the clipboard
set optionList to {"Uppercase case"}
set dialogBoxChoice to choose from list optionList with title "Change case of selection" with prompt "Choose a letter case:"
-- Copy selected text to clipboard:
tell application "System Events" to keystroke "c" using {command down}
delay 1 -- Without this, the clipboard may have stale data.
set theSelectedText to the clipboard
-- Makes the selected text all uppercase:
-- From: http://apple.stackexchange.com/a/171196/184907
set theModifiedSelectedText to (do shell script ("echo " & theSelectedText & " | tr a-z A-Z;"))
display dialog theModifiedSelectedText
-- Overwrite the old selection with the desired text:
set the clipboard to theModifiedSelectedText
tell application "System Events" to keystroke "v" using {command down}
delay 1 -- Without this delay, may restore clipboard before pasting.
-- Restore clipboard:
set the clipboard to savedClipboard
If you put the above code in a keyboard-shortcut-triggered Service, you will see that the script will fail to tell application "System Events" to keystroke "c" using {command down}
; thus, theSelectedText
will be identical to the savedClipboard
every time.
tell me
choose from list
of only one item, which is obviously nonsensical. Anyway, your solution completely solves my issue. Thanks!