Shortcut to convert image format and paste

Posted

I have a hangup about Microsoft Office on macOS - it often elects to paste images from the clipboard in TIFF format, instead of PNG or JPG, which results in massive files, be it PowerPoint .PPTX presentations, Word .DOCX documents or Excel .XLSX spreadsheets. Enter: A new macOS Shortcut to convert clipboard images to (lossless) PNG or (lossy) JPG.

For those interested, I first noticed this issue around August 2017, and used a shell script to convert images thereby Fixing PowerPoint TIFF images and fonts. In October 2019, I figured, why not use a Word Macro to paste MS Office Drawing / TIFF image as PNG instead? And earlier this year, in April 2021, I decided to use an AppleScript to change clipboard image format. And now? Shortcuts!

Simple

Most people will be able to create this script:

  1. Choose from Menu, to select to use either JPG or PNG,
  2. Convert Image from the Clipboard, of type Anything to the desired format,
  3. Copy to Clipboard to place the converted image on the clipboard.

Simple Shortcut to Convert Clipboard Image Format

I also did not use Get Clipboard because it’s redundant as there is always a Clipboard variable / object directly selectable as input. Instead, I select the input of Convert Image to Clipboard and then make sure to change the Type to Anything (instead of Text). This applies twice.

Shortcut Action Clipboard Input as Anything

This means that most items on the clipboard can be converted to an image - which is cool:

  • images
  • text
  • documents

Whatever Preview can view, Convert Image can probably handle. In my case, I could even convert Markdown because I had the Glance “All-in-one Quick Look plugin” installed.

Complex

But then I go all crazy and want the script to:

  • only work with images on the clipboard,
  • be able to automatically switch focus to the most recent (topmost / frontmost) window PowerPoint, Word or Excel window, and
  • send the Paste Special... keystroke, which is Control+Command+V
  • also, I prefer full automation rather than a menu, so I have two duplicate scripts - one hardcoded to convert to JPG and the other PNG.

As a demonstration of the power of Shortcuts with traditional AppleScripts (in this case JXA introduced in macOs 10.10 Yosemite):

Shortcut to Convert Clipboard Image Format and Paste Special into Microsoft Office Apps

Make sure Get Type is set to use the Clipboard as input, of type Anything. I also explicitly set Convert Image to work with the clipboard content of type Image only.

Find Window also deserves a bit of explanation: I want to find App Names based on any of the three keywords, and only limit the result to the first window, as sorted by window index. Sorting by smallest first is opposite of what I expect, but anyhow, this is the option works for me. This action is also excruciatingly slow!

Then to set the focus on the window, basically requires brining it to front, so either Resize Window or Move Window can be used. I used the latter, and this is important: to prevent the window for actually moving, I set its Coordinates to its current X Position and Y Position.

Shortcut Action Window Variable X or Y Position

A tip when trying to set the X Position and Y Position - don’t focus on the input by clicking on it first - if you see a flashing cursor, then Shortcuts is expecting you to type in a number! Press tab to get out of edit mode.

Instead, with immediately right click on the input, then Select Magic Variable to choose the output of Find All Windows, then (left) click the windows variable to change it to X (or Y) Position instead.

Finally, the Run JavaScript for Mac Automation code is quite self explanatory:

Application("System Events").keystroke("v", {using: ['command down', 'control down']})

For hardcore AppleScript lovers, the equivalent is tell application "System Events" to keystroke "v" using {command down, control down}

More Shortcuts coming soon!