I was not satisfied with my last macOS Shortcut to convert image format and paste. The simple version forced me to choose between PNG and JPEG, and the complex version was too slow - specifically, the Find All Windows action. So here we go again...
Requirements
So I’m re-visiting my previous Shortcut to address these issues:
- Get the image from the clipboard (if any).
- Convert the image to both PNG and JPEG (remember that JPEG is lossy, I left the quality slider as the default).
- Compare the file sizes, in order to find the smaller.
- Copy the smaller of the formats to the clipboard.
- And then automate the Paste Special steps by sending keystrokes to the previously active (topmost) application.
A few key assumptions:
- the paste “target” is ALWAYS a Microsoft Office app like Word, Excel or Powerpoint, which has a Paste Special shortcut keystroke,
- and which was the last active (topmost) application before this Shortcut was run,
- plus, this Shortcut is run via the Shortcuts Menu Bar only.
Shortcut
When creating the Shortcut, make sure to select the correct options in Step 2 and Step 3.
- In Step 2, after adding the Convert Image action:
- right click the
Type
and selectClipboard
, - then, click on the
Clipboard
input again, and make sure to change the Type toImage
instead ofText
to get the image from the clipboard.
- right click the
- In Step 3 (“if PNG is smaller then JPEG“), after adding the If action:
- for the first variable, right click it and use Select Magic Variable to point to the PNG image, i.e. the
Converted Image
output. - then click on the
Converted Image
input and change the Get field fromImage
toFile Size
. - change the condition to
is smaller than
. - for the second variable, right click the number placeholder and, just like before, use Select Magic Variable to choose to the converted JPEG.
- then, change to Get
File Size
as well. - finally, I change the
MB
unit tobytes
.
- for the first variable, right click it and use Select Magic Variable to point to the PNG image, i.e. the
Step 5 is repeated twice, once for the JPEG and again for the PNG. Run JavaScript for Automation runs a Mac Automation Script that can do much more - including being able to send keystrokes to applications.
The Paste Special dialog box for Word/Excel/Powerpoint requires different keystrokes for JPEG or PNG. So...
When the clipboard has a PNG formatted image, as in the screenshot above:
- first, press Escape to close the Shortcut dropdown menu (remember the third assumption),
- which then sets the focus back to the previously active window (second assumption)
- then, press Control ^ + Command + V (first assumption),
- and finally, press Return which is equivalent to pressing the OK button.
var sys = Application('System Events')
sys.keyCode(53)
sys.keystroke('v', {using: ['command down', 'control down']})
sys.keyCode(36)
But when the clipboard has a JPEG, notice from the screenshot above that the format I want to paste as is the second item in the list:
- first, press Escape,
- then, press Control ^ + Command + V,
- next, press the down arrow key to select the JPEG format,
- and finally, press Return.
var sys = Application('System Events')
sys.keyCode(53)
sys.keystroke('v', {using: ['command down', 'control down']})
sys.keyCode(125)
sys.keyCode(36)
If you are wondering what keycodes correspond to the down arrow and Return keys, check out this Complete list of AppleScript key codes
Aside: More JXA scripts
Here are some other scripts I was playing with, but ultimately did not use to keep the Shortcut simple with the aforementioned assumptions.
If you want to always send the keystroke to a given app, try something like this:
Application('Word').activate()
Application('System Events').keystroke('v', {using: ['command down', 'control down']})
If you want to check the name of the top most window before doing anything:
var sys = Application('System Events')
var top = sys.processes.whose({frontmost: true})
var app = Application.currentApplication()
app.includeStandardAdditions = true
app.displayDialog(top.displayedName())
I did find ideas via questions like this “MacOSX: get foremost window title” on Stack Overflow. You may also find this JavaScript for Automation Cookbook useful.