AppleScript to change clipboard image format

Posted

For the longest time, I’ve struggled to get the macOS version of Microsoft Office (Word, PowerPoint, Excel) to paste images as PNG or JPEG, instead of the default TIFF. TIFFs make the files far larger than they need to be! I’ve resorted to macros, shell scripts, Quick Actions... all paste PNGs (lossless) into MS Office! But I just found another method with AppleScript.

The problem

The issue is that images copied to the macOS PasteBoard (clipboard) are formatted as TIFF by default, and while there are other formats available, it is up to the application to request the other formats. Microsoft Office does not do this, even if you Paste As....

To test,

  1. Open a JPEG or a PNG in Preview and copy it to the clipboard.
  2. In the Terminal, run:
    osascript -e 'clipboard info'

This will show something like TIFF picture, 1744742, «class 8BPS», 529780, GIF picture, 45499, «class jp2 », 89434, JPEG picture, 111841, «class PNGf», 119352, «class BMP », 1742454, «class TPIC», 245354. Even if you previewed a JPEG or PNG, TIFF is the first format, followed by other available formats.

Now open a MS Office app and try to Paste As..., you will see only one format available:

MS Office Paste As... TIFF only

What we really want is for the clipboard to have JPEG or PNG first, like in the second or third examples:

Terminal running OSAScript to get "clipboard info"

(Firefox screenshots are also in TIFF, but the macOS Screenshot.app uses PNG by default, which you can change with defaults write com.apple.screencapture type jpg and replace jpg with png, tiff or even pdf.

Alternatives...

TIFFs make the files far larger than they need to be - modern formats have far higher compression resulting in smaller MS Office files. For screenshots PNGs are lossless and retain sharpless, while JPEGs great for photos, where artifacts due to lossy compression may not be perceivable.

This has annoyed me for a while, and I’ve resorted a few methods to “fix” TIFFs, e.g.

I also have tried compiling Objective-C code to save screenshots or images from the clipboard as PNG. For example:

Today’s Workaround

I just discovered this method of using AppleScript to set the clipboard to the format of choice!

While I knew it was possible to Change Format of Clipboard Image to JPEG, the major insight was that changing to PNG is also possible! by using «class PNGf» - I never realized the double angle brackets mean something!

Automator running AppleScript to convert clipboard formats

So, putting it all together:

  1. First, create an Automator script as an Application.
  2. Add the Run AppleScriptAction and copy the code as below:
    if ((clipboard info) as string) contains "TIFF picture" then
    display dialog "Convert TIFF in clipboard" buttons {"JPEG", "PNG"} default button 2 with icon note
    if button returned of result is "PNG" then
    set the clipboard to (the clipboard as «class PNGf»)
    else
    set the clipboard to (the clipboard as JPEG picture)
    end if
    end if
  3. Save. I called my script convert_clipboard_tiff.app
  4. Now, whenever you have a TIFF in the clipboard, double click the app to run it. If the clipboard contains a TIFF, select either to convert it to PNG or JPEG as required:

AppleScript dialog, prompting for JPEG or PNG format

If the clipboard TIFF is converted to PNG, MS Office will paste PNGs by default (no need to Paste As...):

Microsoft Office Paste As PNG

If the clipboard TIFF is converted to JPEG, MS Office still prefers TIFFs over JPEGs, so you will have to resort to Paste As... to choose JPEG manually:

Microsoft Office Paste As TIFF or JPEG

One could assign the Automator script to a Task Bar Quick Action too.

Or

Responding to the dialog is troublesome after a while - you may prefer to create two separate scripts, e.g.:

if ((clipboard info) as string) contains "TIFF picture" then
  set the clipboard to (the clipboard as JPEG picture)
end if

and:

if ((clipboard info) as string) contains "TIFF picture" then
  set the clipboard to (the clipboard as «class PNGf»)
end if

I know it’s still manual effort, but I hope this helps someone!