Set a coloured text and send it to the clipboard
This is probably very easy, but all the solutions I found were directed to specific applications.
At my work, we use an online system within the browser and I find myself every day setting the color of my messages using the mouse, which is very annoying. I just need to enter something like:
[green] The file was approved. [/green] <-- this is just for illustration purposes since this question field doesn't have colors. These tags have no effect on the system.
or
[red] The file was denied. [/red].
Since the system is online, I can't use direct commands to set the color the text like we would do using TextEdit or any other real editor. So I wonder if there is a way to set the color of the text via Applescript (Automator Service) and send it to the clipboard because I know the browser will paste the formatted text with the right color.
Any other workaround will be appreciated.
Solution 1:
As I do not have access to the web app you are using, I really cannot test if this will work for you.
The example AppleScript code, shown further below, with limited testing works for me as coded under macOS High Sierra.
Overview: Gets the selected text in Google Chrome and places it on the clipboard in the chosen color as HTML data, and pastes it back over the selected text.1
It uses an Automator Service, set to Service receives selected text
in Google Chrome
, with [] Output replaces selected text left unchecked, to which a keyboard shortcut was assigned in System Preferences > Keyboard > Shortcuts > Services as: ⌃⌘C
The Automator Service uses a Run AppleScript action with the default code replaced entirely with the example AppleScript code.
Usage: After selecting text in Google Chrome then pressing ⌃⌘C a choose from list
dialog box is shown where you can select which color to make the selected text on the clipboard. You do not need to use the mouse, just keystrokes, e.g. G-Enter for green, O-Enter for orange and R-Enter for red, or Esc to cancel the action.
At this point, if all works right, the selected text will have been replaced with the colored text from the clipboard.1 (You will have to give it a second to let the service complete.)
Yes, it's a few keystrokes, but once comfortable with it I believe it will become second nature and be faster then having to reach for the mouse, etc.
The usual caveats may apply for the use of this in: System Preferences > Security & Privacy > Privacy > Accessibility
1Also note that the text box containing the selected text must allow pasting back the same HTML class data type and structure that was placed on the clipboard as given in the comments to the OP as answers to the questions I asked, otherwise this will not work as coded.
Example AppleScript code:
-- # Green HTML Color: #27ae60 '23323761653630'
-- # Orange HTML Color: #e67e22; '23653637653232'
-- # Red HTML Color: #d35400 '23643335343030'
-- # sed command 'placeholder' '706C616365686F6C646572'
property myBrowser : "Google Chrome"
property greenHTML : «data HTML3C6D65746120636861727365743D277574662D38273E3C7370616E207374796C653D22636F6C6F723A233030666630303B223E706C616365686F6C6465723C2F7370616E3E0A»
property orangeHTML : «data HTML3C6D65746120636861727365743D277574662D38273E3C7370616E207374796C653D22636F6C6F723A236536376532323B223E706C616365686F6C6465723C2F7370616E3E0A»
property redHTML : «data HTML3C6D65746120636861727365743D277574662D38273E3C7370616E207374796C653D22636F6C6F723A236433353430303B223E706C616365686F6C6465723C2F7370616E3E0A»
property tmpHTMLdata : "/private/tmp/tmpHTMLdata"
on run {input, parameters}
activate me
set selectedText to item 1 of input as text
set chosenColor to (choose from list ¬
{"Green", "Orange", "Red"} with title ¬
"Color Chooser" with prompt ¬
"Which color to make the selected text?") as text
if (chosenColor as text) is "false" then
return
else if chosenColor is "Green" then
my writeToFile(greenHTML, tmpHTMLdata, true)
else if chosenColor is "Orange" then
my writeToFile(orangeHTML, tmpHTMLdata, true)
else if chosenColor is "Red" then
my writeToFile(redHTML, tmpHTMLdata, true)
end if
do shell script "sed -i '' -e 's|placeholder|" & selectedText & "|' " & quoted form of tmpHTMLdata
set the clipboard to (read tmpHTMLdata as «class HTML»)
delay 0.25
tell application myBrowser to activate
delay 0.25
tell application "System Events" to keystroke "v" using command down
end run
on writeToFile(theData, theFile, overwriteExistingContent)
try
set theOpenedFile to open for access POSIX file theFile with write permission
if overwriteExistingContent is true then set eof of theOpenedFile to 0
write theData to theOpenedFile starting at eof
close access theOpenedFile
on error eStr number eNum
try
close access file theFile
end try
display dialog eStr & " number " & eNum ¬
buttons {"OK"} default button 1 ¬
with title "File I/O Error..." with icon stop
return
end try
end writeToFile
Note: The example AppleScript code is just that and other than 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.