You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

AppleScript error opening pdf in preview

macOS Sierra Version 10.12.6.


Hello, I'm trying to reduce the size of multiple pdfs, however the script below prompts error: line ending expected, but class name found. The script also highlights line 19's word document.

-- Compress PDFs using Preview's Quartz Filters
set quartzFilter to "Reduce File Size" -- Replace with your Quartz filter name
set outputFolder to (choose folder with prompt "Select the folder to save compressed PDFs:")


tell application "Finder"
    set pdfFiles to (choose file of type "PDF" with prompt "Select PDF files to compress:" with multiple selections allowed)
end tell


repeat with pdfFile in pdfFiles
    set inputFilePath to (pdfFile as text)
    set outputFilePath to ((outputFolder as text) & (name of (info for pdfFile)))
    
    tell application "Preview"
        open pdfFile
        delay 1 -- Allow time for Preview to load the file
        try
            export document 1 to file outputFilePath as PDF using Quartz filter quartzFilter
            close document 1
        on error errMsg
            display dialog "Error processing file: " & (name of (info for pdfFile)) & ". Error: " & errMsg
        end try
    end tell
end repeat


display dialog "Compression complete!" buttons {"OK"} default button "OK"

Mac mini, macOS 10.13

Posted on Nov 17, 2024 4:10 AM

Reply
2 replies

Nov 17, 2024 8:31 AM in response to eiger3970

The AppleScript scripting dictionary for Apple's Preview has no export verb.


You can still reduce a PDF's file size by creating a custom Quartz filter that you can then use in AppleScript/Objective-C.


To create the Quartz Filter see Preview - Quartz Filter Reduce File Size … - Apple Community


Once you have saved the Quartz filter (it will be in ~/Library/Filters), you can then use it in an AppleScript and apply that filter to a PDF.


AppleScript:

(*
	compress_pdf.applescript
	
	Uses predefined ColorSync Quartz filter
	Reference: https://discussions.apple.com/thread/253247239?sortBy=oldest_first
	Tested: macOS Sequoia v15.1
	VikingOSX, 2024-11-17, Apple Support Communities, No warranties at all.
*)

use framework "Foundation"
use framework "Quartz"
use scripting additions

property ca : current application

-- change the filter name to the one you created in ColorSync app.
set filterFile to "~/Library/Filters/Reduce File Size 200dpi.qfilter"
set outPDF to "~/Desktop/out.pdf"
set NSFilterFile to (ca's NSString's stringWithString:filterFile)'s stringByExpandingTildeInPath()
set NSOutPDF to (ca's NSString's stringWithString:outPDF)'s stringByExpandingTildeInPath()
set ffilter to ca's QuartzFilter's quartzFilterWithURL:(ca's NSURL's fileURLWithPath:NSFilterFile)

set thisPDF to POSIX path of (choose file of type "PDF")

set pdf to ca's PDFDocument's alloc()'s initWithURL:(ca's NSURL's fileURLWithPath:thisPDF)
try
	pdf's writeToFile:NSOutPDF withOptions:{QuartzFilter:ffilter}
end try
delay 1
set {orig, reduced} to paragraphs of (do shell script "/usr/bin/stat -f '%z' " & quoted form of thisPDF & space & quoted form of (NSOutPDF as text))
set pct to ((orig - reduced) / orig) * 100
set percentage to my roundIt(pct, 1)

display dialog "Original PDF size: " & orig & return & "Reduced PDF size: " & reduced ¬
	& return & "Reduction (pct): " & percentage with title "PDF File Reduction"

return

on roundIt(n, numDecs)
	set x to 10 ^ numDecs
	tell n * x to return (it div 0.5 - it div 1) / x
end roundIt



You can adapt the meaningful parts of this script to work on more than one PDF file or establish an output folder for the resultant PDF.


Nov 17, 2024 6:41 AM in response to eiger3970

So I moved on to Terminal code however, testing on 1 file and then multiple files also failed as per code here:

Multiple files failed with loads of weird characters in the terminal output:

$ for file in /Users/Guest/Desktop/202410271045\ separated/*.pdf; do output="${file%.pdf}_compressed.pdf"; cupsfilter -i application/pdf -o "$output" -p /System/Library/Filters/Reduce\ File\ Size.qfilter "$file"; done


1 file

$ cupsfilter -i application/pdf -o /Users/Guest/Downloads/compressed.pdf -p /System/Library/Filters/Reduce\ File\ Size.qfilter /Users/Guest/Downloads/input.pdf
DEBUG: argv[0]="cupsfilter"
DEBUG: argv[1]="1"
DEBUG: argv[2]="Guest"
DEBUG: argv[3]="input.pdf"
DEBUG: argv[4]="1"
DEBUG: argv[5]="/Users/Guest/Downloads/compressed.pdf=true"
DEBUG: argv[6]="/Users/Guest/Downloads/input.pdf"
DEBUG: envp[0]="<CFProcessPath>"
DEBUG: envp[1]="CONTENT_TYPE=application/pdf"
DEBUG: envp[2]="CUPS_DATADIR=/usr/share/cups"
DEBUG: envp[3]="CUPS_FONTPATH=/usr/share/cups/fonts"
DEBUG: envp[4]="CUPS_SERVERBIN=/usr/libexec/cups"
DEBUG: envp[5]="CUPS_SERVERROOT=/private/etc/cups"
DEBUG: envp[6]="LANG=de_CH.UTF8"
DEBUG: envp[7]="PATH=/usr/libexec/cups/filter:/usr/bin:/usr/sbin:/bin:/usr/bin"
DEBUG: envp[8]="PPD=/System/Library/Filters/Reduce File Size.qfilter"
DEBUG: envp[9]="PRINTER_INFO=cupsfilter"
DEBUG: envp[10]="PRINTER_LOCATION=Unknown"
DEBUG: envp[11]="PRINTER=cupsfilter"
DEBUG: envp[12]="RIP_MAX_CACHE=128m"
DEBUG: envp[13]="USER=Guest"
DEBUG: envp[14]="CHARSET=utf-8"
DEBUG: envp[15]="FINAL_CONTENT_TYPE=application/pdf"
INFO: gziptoany (PID 5149) started.
DEBUG: Unable to open "/Users/Guest/Downloads/input.pdf".
ERROR:Zu druckende Datei kann nicht geöffnet werden: No such file or directory
ERROR: gziptoany (PID 5149) stopped with status 1

AppleScript error opening pdf in preview

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.