PDF compression using qfilter

Hi all,

I have found the script in the forum to execute the pdf compression for selected files and I tried to modify the code for batch or multiple pdf files and facing some issues could anyone help me how to fix this?

code below:

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

(*global PathToDesktop, destinationFoldeHFS, destinationFolderPosixPath
set PathToDesktop to path to the desktop as text
set destinationFoldeHFS to PathToDesktop & "Pdf_Compress"
set destinationFolderPosixPath to POSIX path of destinationFoldeHFS

try
	alias destinationFoldeHFS
on error
	tell application "System Events" to ¬
		make new folder at folder PathToDesktop with properties {name:"Pdf_Compress"}
end try*)

on main()
	set filterFolder to (path to system folder as text) & "Library:Filters:"
	-- set filterFolder to (path to home folder as text) & "Library:Filters:"
	
	tell application "Finder"
		(*set desktopFolder to (path to desktop)
		set fldnm to "Pdf_Compress"
		set targetFolder to desktopFolder & "Pdf_Compress:" as text
		set rslt to my FldrExists(targetFolder)
		if rslt is false then
			set targetFolder to make new folder at folder desktopFolder with properties {name:fldnm}
		else
			delete (every item of folder (targetFolder) whose name ends with ".pdf")
		end if*)
		
		set sourceFile to (choose file with prompt "Please choose one or more files:" with multiple selections allowed) as alias list
		set filterFiles to the name of every file in folder filterFolder
	end tell
	
	--if (count sourceFile) ≠ 1 then errorDialog("Multiple or no files selected.")
	repeat with i from 1 to count of sourceFile
		set finalsourceFile to POSIX file (item i of sourceFile) as text
		if finalsourceFile does not end with ".pdf" then errorDialog("The selected file does not have a PDF extension.")
	end repeat
	
	repeat with aFile in filterFiles
		set contents of aFile to text 1 thru -9 of aFile
	end repeat
	
	set selectedFilter to choose from list filterFiles with prompt "Select a filter:" with title "Quartz Filter" default items (item 1 of filterFiles)
	if selectedFilter = false then
		error number -128
	else
		set filterFile to POSIX path of filterFolder & (item 1 of selectedFilter) & ".qfilter"
	end if
	set targetFile to (text 1 thru -5 of finalsourceFile) & " (" & selectedFilter & " Filter).pdf"
	applyFilter(finalsourceFile, targetFile, filterFile)
end main

on applyFilter(sourceFile, targetFile, filterFile) -- credit to Shane Stanley
	set filterFile to current application's |NSURL|'s fileURLWithPath:filterFile
	set filterFile to current application's QuartzFilter's quartzFilterWithURL:filterFile
	set sourceFile to current application's |NSURL|'s fileURLWithPath:sourceFile
	set thePDFDoc to current application's PDFDocument's alloc()'s initWithURL:sourceFile
	thePDFDoc's writeToFile:targetFile withOptions:{QuartzFilter:filterFile}
end applyFilter

on errorDialog(dialogText)
	display dialog dialogText buttons "OK" cancel button 1 default button 1 with title "Quartz Filter" with icon stop
end errorDialog

main()

-- Folder checking
on FldrExists(theFldr) -- (String) as Boolean
	tell application "System Events"
		if exists folder theFldr then
			return true
		else
			return false
		end if
	end tell
end FldrExists

-- File checking
on FileExists(theFle) -- (String) as Boolean
	tell application "System Events"
		if exists file theFle then
			return true
		else
			return false
		end if
	end tell
end FileExists

Thanks Fredrick for Info.

I agreed with your point. And my point should be is it possible to get the pdf document size and apply the respective filter?

For example, In the batch folder, the pdf document having width of 814 mm and greater to apply one filter and another set of pdf document having with width of less than 814 mm to apply another filter is it possible?

Thanks for the suggestions!

My requirement is using applescript to run the two filters (high-res & med-res) for two different size of pdf files in the selected folders which has to be run as batch process. I’m new to applescript could you help me how to resolve this?

Yes I understood, I want the things to automate in applescript. For Example, if the user drag and drop the folder containing PDF files and the final output compressed PDF files will generate in the desktop without manual clicking options in the preview.

Just in general, doing what you want is not especially difficult. You first have to create the desired low- and high-resolution Quartz filters, and that is easily done with the ColorSync utility. Before proceeding further, you need to confirm that the newly-created filters will yield the desired result.

Next, the script contained in post 4 of the thread linked below needs to be edited to automatically apply one of the Quartz filters, based on a file or PDF characteristic of your choice. If that characteristic is file size, then the process (not the actual code) would be something like the following:

tell application "Finder"
	repeat with aPDF in thePDFs
		if (size of aPDF) < 1000000 then
			-- apply low-resolution filter
		else
			-- apply high-resolution filter
		end if
	end repeat
end tell

https://macscripter.net/viewtopic.php?id=47872

Thanks peavine! Your help is really appreciated and I have tried if anything need I will get back to you!

Is it possible to get the PDF document width and height in mm using shell script?