Wednesday, December 8, 2021

#1 2020-08-31 11:52:06 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1147

Quartz Filter Script

This script applies Quartz filters to PDF files. The reason one might do this is described by Apple as:

You can use Quartz filters to modify a PDF document by adding effects, changing the color space, and even reducing the size of the file by recompressing graphics.


https://support.apple.com/lt-lt/guide/c … /mac/10.15

To test this script:

1) Open the script in Script Editor.
2) Select a PDF file in a Finder window.
3) Run the script.
4) Select a filter to apply to the PDF file.

The script applies the selected filter to a copy of the PDF and does not alter the original.

Quartz filters are created and modified with the ColorSync app, and the operation of this app is fairly straightforward. However, it is important to note that the default filters in Catalina are contained in a folder within the System folder, and user-created filters are contained in a folder within the User folder. So, for regular use, it's probably best to edit the script to point filterFolder to the filters in the User folder. The default filters are easily copied over if that's desired.

Shane wrote the code in the applyFilter handler and his help is appreciated.

Applescript:

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

on main()
   
   set filterFolder to ((path to system folder as text) & "Library:Filters:") as alias
   -- set filterFolder to ((path to home folder as text) & "Library:Filters:") as alias
   
   tell application "Finder"
       set sourceFile to selection as alias list
       set filterFiles to the name of every file in filterFolder
   end tell
   
   if (count sourceFile) ≠ 1 then errorDialog("Multiple or no files selected.")    
   set sourceFile to POSIX path of sourceFile
   if sourceFile does not end with ".pdf" then errorDialog("The selected file does not have a PDF extension.")
   
   repeat with aFile in filterFiles
       set contents of aFile to text 1 thru -9 of aFile
   end repeat
   
   choose from list filterFiles with prompt "Select a filter:" with title "Quartz Filter" default items (item 1 of filterFiles)
   set selectedFilter to result
   
   if selectedFilter = false then
       error number -128
   else
       set filterFile to (filterFolder as text) & (item 1 of selectedFilter) & ".qfilter" as alias
       set filterFile to POSIX path of filterFile
   end if
   
   set text item delimiters to {".pdf"}
   set destinationFile to (text 1 thru text item -2 of sourceFile) & " (" & selectedFilter & " Filter).pdf"
   set text item delimiters to {""}
   
   applyFilter(sourceFile, destinationFile, filterFile)
   
end main

on applyFilter(sourceFile, destinationFile, filterFile) -- written by Shane Stanley
   set filterPath to current application's NSString's stringWithString:filterFile
   set filterURL to current application's NSURL's fileURLWithPath:filterPath
   set filterFile to current application's QuartzFilter's quartzFilterWithURL:filterURL
   set pdfURL to current application's NSURL's fileURLWithPath:sourceFile
   set thePDFDoc to current application's PDFDocument's alloc()'s initWithURL:pdfURL
   set theOptions to {QuartzFilter:filterFile}
   thePDFDoc's writeToFile:destinationFile withOptions:theOptions
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()

Last edited by peavine (2020-09-02 09:14:10 am)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#2 2020-09-01 10:06:32 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1147

Re: Quartz Filter Script

This post contains a few miscellaneous thoughts on this topic.

My script is intended for regular use in applying Quartz filters to PDF files. For occasional use, or just to view the impact of a Quartz filter on a particular PDF, the viewer included with the ColorSync app should probably be used.

The impact of a Quartz filter on a PDF is often unpredictable and that's one reason my script does not work on the original. However, should the user wish to always overwrite the original, the handler call in my script can be modified as follows:

Applescript:

applyFilter(sourceFile, sourceFile, filterFile)

Last edited by peavine (2021-05-31 08:55:38 am)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#3 2021-05-31 08:31:19 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1147

Re: Quartz Filter Script

This is a revised version of my earlier script--I just cleaned it up a bit. The user should set the path to the filter folder they will be using.

BTW, macOS Catalina includes in the system folder a filter named "Reduce File Size" and it works by constraining image size to a minimum and maximum of 128 and 512 pixels. Everyone's needs differ but I've found that this filter often does not work well, and the user may want to test alternative settings.

Applescript:

-- Revised 2021.05.31

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

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 sourceFile to selection 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.")
   set sourceFile to POSIX path of item 1 of sourceFile
   if sourceFile does not end with ".pdf" then errorDialog("The selected file does not have a PDF extension.")
   
   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 sourceFile) & " (" & selectedFilter & " Filter).pdf"
   
   applyFilter(sourceFile, 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()


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#4 2021-08-05 02:09:14 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1147

Re: Quartz Filter Script

The following script is similar to those above except that it:

* prompts the user for the file to process and allows the user to select multiple files;
* places the processed files in what is intended to be a temporary folder on the desktop for user review; and
* moves any existing PDF files in the temporary desktop folder to the trash.

Applescript:

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

on main()
   set filterFolder to (path to system folder as text) & "Library:Filters:"
   -- set filterFolder to (path to home folder as text) & "Library:Filters:"
   set targetPath to (path to desktop as text)
   set targetFolderName to "PDF Filtered Files"
   set sourceFiles to (choose file of type {"com.adobe.pdf"} with multiple selections allowed)
   tell application "Finder" to set filterFiles to the name of every file in folder filterFolder
   
   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
   
   tell application "Finder"
       if not (exists folder (targetPath & targetFolderName)) then
           make new folder at folder targetPath with properties {name:targetFolderName}
       else
           delete ((every file in folder (targetPath & targetFolderName)) whose name ends with ".pdf")
       end if
       
       repeat with aFile in sourceFiles
           set aFileName to name of aFile
           set targetFile to targetPath & targetFolderName & ":" & (text 1 thru -5 of aFileName) & " (" & selectedFilter & " Filter).pdf"
           my applyFilter(POSIX path of aFile, POSIX path of targetFile, filterFile)
       end repeat
   end tell
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
   try
       thePDFDoc's writeToFile:targetFile withOptions:{QuartzFilter:filterFile}
   on error
       errorDialog("An error occurred while processing " & quote & sourceFile & quote)
   end try
end applyFilter

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

main()

Last edited by peavine (2021-08-05 02:13:39 pm)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)