Hi All,
I have applied quartz filters to some of the PDFs which reduced file sizes correctly and some of the PDF file sizes increases from existing file sizes.
Could you please anyone tell me why this is happening?
Hi All,
I have applied quartz filters to some of the PDFs which reduced file sizes correctly and some of the PDF file sizes increases from existing file sizes.
Could you please anyone tell me why this is happening?
Quartz filters have numerous settings that could decrease/increase the size of one PDF but not the next (e.g. existing and filter-specified image compression, resolution, and minimum image size). To better understand this, you may want to look at the following:
Hi Peavine,
Thanks for the info,
I have applying filters to multiple PDF files via script. The problem is filter will increase the size of maximum reduced file size PDF in the batch. Is it possible to ignore the maximum reduced file size PDFs and applying filters to remaining PDFs in the batch.
Thanks
Vijay_Yukthi. As far as I know, it is not possible to do what you want. However, you can accomplish the same thing as follows:
Create a temporary PDF file by applying the Quartz filter to the original PDF file.
Have the script make a determination (probably based on file size) what action the script will take.
Based on the determination made in 2 above, have the script either delete the temporary PDF file and retain the original PDF file, or delete the original PDF file and replace it with the temporary PDF file.
Hi Peavine,
I’m not clear with above points as you mentioned. Could you please explain in detail. The script will apply the filters based on the document width. Please find the below code for your reference.
use framework "Foundation"
use framework "Quartz"
use scripting additions
on main()
tell application "System Events"
set theUser to name of current user
end tell
set filterFolder to (path to library folder from user domain as text) & "Filters:"
-- set filterFolder to (path to home folder as text) & "Library:Filters:"
set targetPath to (path to desktop as text)
set targetFolderName to "PDF_Compression"
set filterFile to POSIX path of filterFolder & "Med for Hi-RES Above A0.qfilter"
set filterFile1 to POSIX path of filterFolder & "Med for Small Prints Smaller A0.qfilter"
tell application "System Events"
if exists file filterFile then
else
display dialog "Med for Hi-RES Above A0.qfilter is missing in the below path
" & filterFile
tell me to quit
end if
if exists file filterFile1 then
else
display dialog "Med for Small Prints Smaller A0.qfilter is missing in the below path
" & filterFile1
tell me to quit
end if
end tell
--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
set theFolder to (choose folder)
tell application "Finder"
set theFiles to (every file of the entire contents of theFolder whose name extension is "pdf") as alias list
end tell
repeat with aFile in filterFiles
set contents of aFile to text 1 thru -9 of aFile
end repeat
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 theFiles
set aFileName to name of aFile
set targetFile to targetPath & targetFolderName & ":" & (text 1 thru -5 of aFileName) & ".pdf"
---------------------------------------------------------------------------------
set pdfDoc to (current application's PDFDocument's alloc()'s initWithURL:aFile)
set thePage to (pdfDoc's pageAtIndex:0) -- page 1
set pageSize to (thePage's boundsForBox:(current application's kPDFDisplayBoxMediaBox))
set pageWidth to (current application's NSWidth(pageSize)) / 72 * 25.4 -- returns width in mm
set pageHeight to (current application's NSHeight(pageSize)) / 72 * 25.4 -- returns height in mm
----------------------------------------------------------------------------------
if pageWidth ≥ 841 then
my applyFilter(POSIX path of aFile, POSIX path of targetFile, filterFile)
end if
if pageWidth < 841 then
my applyFilter(POSIX path of aFile, POSIX path of targetFile, filterFile1)
end if
end repeat
display alert "hey! " & theUser & return & return & "Process Completed..."
end tell
end main
--Apply Filter
on applyFilter(sourceFile, targetFile, filterFile)
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()
After your script creates the new PDF in the PDF_Compression folder, the script should compare the size of the original and the newly-created PDF and take whatever action you want. The new code would be inserted in your script at the following point:
if pageWidth < 841 then
my applyFilter(POSIX path of aFile, POSIX path of targetFile, filterFile1)
end if
## insert new code here ##
end repeat
display alert "hey! " & theUser & return & return & "Process Completed..."
If you want the newly-created PDF to be deleted if it is larger than the original, the code might be:
set sourceSize to size of aFile
set targetSize to size of file targetFile
if targetSize > sourceSize then delete file targetFile
This may seem a bit cumbersome but, as noted earlier, I don’t believe it’s possible to determine in advance if a PDF’s file size is going to be larger or smaller after applying a Quartz filter.
Thanks Peavine!
Shall we change the code instead of deleted the target files replace with source files?
set sourceSize to size of aFile
set targetSize to size of file targetFile
if targetSize > sourceSize then
--delete file targetFile
copy aFile to theFolder
move aFile to targetPath & targetFolderName with replacing
end if
The problem is copy will move the file instead of copying and tried with duplicate it move the original file to final folder and the duplicate file remains in the source folder.
Is it possible to copy the files without duplicating or moving from the source folder?
Vijay_Yukthi. The terminology can be confusing. You use the duplicate command if you want to create a copy of a file in a destination folder and to retain the original file. In general, you use the move command if you want to create a copy of a file in a destination folder and to delete the original file. The only exception is when you move a file from one drive to another in which instance the original file is not deleted. The copy command does something entirely different and should not be used in this code.
I’m not certain what you want to do, but the following duplicates the original file to the PDF_Compression folder and overwrites any existing files in the PDF_Compression folder. I tested this code with your original script and it worked as expected.
set sourceSize to size of aFile
set targetSize to size of file targetFile
if targetSize > sourceSize then
duplicate aFile to folder (targetPath & targetFolderName) with replacing
end if
Thanks Peavine! I wi ll check this.
Hi.
Just to be picky: when you “move” a file to another folder on the same disk, it actually stays exactly where it is on the disk. Only the entries in the disk catalogue are changed so that the route to the file is via one directory rather than the other.
Thanks Nigel–I didn’t know that. I assume that’s why moving large files from one location to another on the same disk is so fast.
Hi peavine. Sorry for the slow reply.
Yes indeed. It takes next to no time just to change a couple of catalogue entries.
But as you’ve noted above, when it comes to transferring data to another disk, the Finder’s move and duplicate commands both copy the data across to the other disk and leave the original file in situ. This seems the best idea from a safety angle and I imagine the various shell and Foundation alternatives do the same, although I haven’t tested them in this respect.
Nigel. I was curious and tested for this. The shell mv command, System Events move command, and fileManager’s moveItemAtPath method all delete the original after moving a file from one disk to another. Unfortunately, on macOS Monterey, the System Events move command continues not to work with aliases, but I guess that’s another topic.
Ah. I must remember that. Thanks, peavine!
It does work with aliases, but only members of the alias class it defines, which is different from the standard AppleScript alias. Why they used the same name, I’ll never understand.
Shane. The earlier thread on this topic is at:
https://macscripter.net/viewtopic.php?id=47321
KniazidisR had suggested the following as a test but it doesn’t work in Monterey. Perhaps I don’t understand how you create an alias in System Events.
set aFileHFSPath to (choose file) as text
set aFolderHFSPath to (choose folder) as text
tell application "System Events"
set anAliasFile to alias aFileHFSPath
set anAliasFolder to alias aFolderHFSPath
move anAliasFile to anAliasFolder --> System Events got an error: NSArgumentsWrongScriptError
end tell
My mistake. I was confusing it with the fact that you can get the properties of a System Events alias.