MacOS – How to the default save format be changed for OS X Preview app

automationmacospreview

Is there a resource showing all the possible syntax for changing the preference file for the preview app in os x? I'm using Catalina if that matters. I frequently copy an image to the clipboard and then command N in preview and then want it to save in PDF format instead of the default PNG format.

defaults write com.apple.Preview

Best Answer

Unless you need to edit the image in Preview, I'd suggest leaving it out of the equation altogether.

I use the following python script which saves any image on the clipboard to a PDF file, using a save dialog. (If the file exists, it adds the image as a new page.) You can embed it in an Automator workflow with a Run Shell Script action, or add it to the /Library/Scripts folder, and you can access it using the Scripts menulet. (Script Editor.app Preferences enable the menulet.)

#!/usr/bin/python

from AppKit import NSPasteboard, NSPasteboardTypePDF, NSPasteboardTypeTIFF, NSPasteboardTypePNG, NSTIFFPboardType, NSPICTPboardType, NSImage, NSSavePanel, NSApp
from Foundation import NSURL
import Quartz as Quartz
import os, syslog

def save_dialog(directory, filename):
    panel = NSSavePanel.savePanel()
    print (panel)
    panel.setTitle_("Save clipboard")
    myUrl = NSURL.fileURLWithPath_isDirectory_(directory, True)
    panel.setDirectoryURL_(myUrl)
    panel.setNameFieldStringValue_(filename)
    NSApp.activateIgnoringOtherApps_(True)
    ret_value = panel.runModal()
    if ret_value:
        return panel.filename()
    else:
        return ''

def main():
    destination = os.path.expanduser("~/Desktop/")
    outfile = save_dialog(destination, "Clipboard.pdf")

    myFavoriteTypes = [NSPasteboardTypePDF, NSPasteboardTypeTIFF, NSPasteboardTypePNG, NSTIFFPboardType, NSPICTPboardType, 'com.adobe.encapsulated-postscript']
    pb = NSPasteboard.generalPasteboard()
    best_type = pb.availableTypeFromArray_(myFavoriteTypes)
    if best_type:
        clipData = pb.dataForType_(best_type)
        if clipData:
            image = NSImage.alloc().initWithPasteboard_(pb)
            if image:
                page = Quartz.PDFPage.alloc().initWithImage_(image)
            if os.path.exists(outfile):
                pdfURL = NSURL.fileURLWithPath_(outfile)
                myFile = Quartz.PDFDocument.alloc().initWithURL_(pdfURL)
                if myFile:
                    pagenum = myFile.pageCount()
                    myFile.insertPage_atIndex_(page, pagenum)
                    print ("Image added to Clipboard file.")

            else:
                pageData = page.dataRepresentation()
                myFile = Quartz.PDFDocument.alloc().initWithData_(pageData)
            myFile.writeToFile_(outfile)
            print ("Clipboard file created.")

    else:
        print ("No clipboard image data was retrieved.")
        print ("These types were available:")
        print (pb.types())

if __name__ == "__main__":
    main()