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