Sunday, January 23, 2022

#1 2021-12-20 02:39:54 am

Vijay_Yukthi
Member
Registered: 2021-04-16
Posts: 46

applying Quartz Filters increase sizes from existing file size?

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?

Offline

 

#2 2021-12-20 07:34:16 am

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

Re: applying Quartz Filters increase sizes from existing file size?

Vijay_Yukthi wrote:

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:

1) Open the ColorSync utility.
2) Click on the Filters tab.
3) Expand the Reduce File Size filter.
4) Expand Image Sampling.

Last edited by peavine (2021-12-20 03:39:33 pm)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#3 2022-01-04 02:26:23 am

Vijay_Yukthi
Member
Registered: 2021-04-16
Posts: 46

Re: applying Quartz Filters increase sizes from existing file size?

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

Offline

 

#4 2022-01-04 07:33:37 am

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

Re: applying Quartz Filters increase sizes from existing file size?

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:

1)  Create a temporary PDF file by applying the Quartz filter to the original PDF file.

2) Have the script make a determination (probably based on file size) what action the script will take.

3) 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.


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#5 2022-01-04 09:42:13 am

Vijay_Yukthi
Member
Registered: 2021-04-16
Posts: 46

Re: applying Quartz Filters increase sizes from existing file size?

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.

Applescript:

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()

Offline

 

#6 2022-01-04 05:26:13 pm

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

Re: applying Quartz Filters increase sizes from existing file size?

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:

Applescript:

   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:

Applescript:

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.

Last edited by peavine (2022-01-04 06:31:45 pm)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#7 2022-01-05 12:30:23 am

Vijay_Yukthi
Member
Registered: 2021-04-16
Posts: 46

Re: applying Quartz Filters increase sizes from existing file size?

Thanks Peavine!

Shall we change the code instead of deleted the target files replace with source files?

Applescript:

           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?

Last edited by Vijay_Yukthi (2022-01-05 02:15:59 am)

Offline

 

#8 2022-01-05 07:25:01 am

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

Re: applying Quartz Filters increase sizes from existing file size?

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.

Applescript:

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


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#9 2022-01-05 08:26:45 am

Vijay_Yukthi
Member
Registered: 2021-04-16
Posts: 46

Re: applying Quartz Filters increase sizes from existing file size?

Thanks Peavine! I wi ll check this.

Offline

 

#10 2022-01-05 08:50:33 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5474

Re: applying Quartz Filters increase sizes from existing file size?

peavine wrote:

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.


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.  smile


NG

Offline

 

#11 2022-01-05 09:15:00 am

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

Re: applying Quartz Filters increase sizes from existing file size?

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.


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#12 2022-01-06 04:04:57 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5474

Re: applying Quartz Filters increase sizes from existing file size?

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.


NG

Offline

 

#13 2022-01-06 08:08:37 am

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

Re: applying Quartz Filters increase sizes from existing file size?

Nigel Garvey wrote:

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. smile


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#14 2022-01-06 04:03:46 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5474

Re: applying Quartz Filters increase sizes from existing file size?

peavine wrote:

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.


Ah. I must remember that. Thanks, peavine!

Unfortunately, on macOS Monterey, the System Events move command continues not to work with aliases, but I guess that's another topic. smile


smile


NG

Offline

 

#15 2022-01-06 07:30:38 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6747

Re: applying Quartz Filters increase sizes from existing file size?

peavine wrote:

Unfortunately, on macOS Monterey, the System Events move command continues not to work with aliases



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 Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#16 2022-01-06 08:49:53 pm

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

Re: applying Quartz Filters increase sizes from existing file size?

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.

Applescript:

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


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#17 2022-01-06 09:15:18 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6747

Re: applying Quartz Filters increase sizes from existing file size?

My mistake. I was confusing it with the fact that you can get the properties of a System Events alias.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)