Acrobat Sort and Save

Hello! This is my first post and I’m very new to Applescripting.

I’m trying to create a script that, when a folder is dropped on it, will open the enclosed PDF’s in Acrobat, check the size and do a save as to the appropriate folder. For example: if the PDF is 0, 468, 684, 0 it should save to the 0_Flat folder created earlier in the script. When I run my script it acts like it will work until it gets to the save, then it errors saying that the document doesn’t understand the save message.

on open aList
    with timeout of (30 * minutes) seconds
        set CustFolder to item 1 of aList
        
        tell application "Finder"
            set AppleScript's text item delimiters to {":"}
            set PathList to text items of (aList as text)
            set PathCount to count of items in PathList
            set AppleScript's text item delimiters to {""}
            set OriginalfileList to name of every file of CustFolder
            set fileCount to count of items in OriginalfileList
            set FlatFolder to (CustFolder & "0_Flat:") as string
            set FoldFolder to (CustFolder & "0_Folded:") as string
            set FlatFile to (FlatFolder)
            set FoldFile to (FoldFolder)
            
            try
                set FlatFolder to make new folder at alias CustFolder with properties {name:"0_Flat"}
            end try
            try
                set FoldFolder to make new folder at alias CustFolder with properties {name:"0_Folded"}
            end try
            try
                set OriginalFolder to make new folder at alias CustFolder with properties {name:"0_Original"}
            end try
            
            repeat with t from 1 to fileCount
                set saveNo to "Yes"
                tell application "Adobe Acrobat Pro"
                    set theDocA to item t of OriginalfileList
                    open ((CustFolder & theDocA) as string)
                    
                    
                    tell document theDocA
                        tell page 1
                            set {0, theHeight, theWidth, 0} to bleed box
                            set the_number_to_round to theHeight
                            set the_decimal_precision to 1
                            my round_to_decimal_places(the_number_to_round, the_decimal_precision)
                            set theHeight to result
                            set the_number_to_round to theWidth
                            set the_decimal_precision to 1
                            my round_to_decimal_places(the_number_to_round, the_decimal_precision)
                            set theWidth to result
                        end tell
                    end tell
                    
                    
                    
                    if theHeight is 468 and theWidth is 684 then
                        set FlatFile to (FlatFolder)
                    else
                        set FoldFile to (FoldFolder)
                    end if
                    
                    save document theDocA with linearize
                    close document theDocA saving no
                end tell
            end repeat
            
            tell application "Finder"
                repeat with i in OriginalfileList
                    move i to folder "0_Originals" of CustFolder
                end repeat
                
                display dialog "UHG script has completed successfully."
            end tell
            
            
        end tell
    end timeout
    
end open


on round_to_decimal_places(the_number_to_round, the_decimal_precision)
    set multiplier to 10 ^ the_decimal_precision
    the_number_to_round * multiplier
    round result
    return result / multiplier
end round_to_decimal_places

Any pointers would be greatly appreciated. Thank you!!

Hi tmd0409 and welcome to the forum

This should get you going, i’m sure it can be cleaned up a bit, but runs fine as it is, ive set the code to not open the pdf’s but rather have them set to invisible so as to make things a bit quicker and less visually annoying.
no error checking at the moment.

on open aList
	with timeout of (30 * minutes) seconds
		set CustFolder to item 1 of aList
		tell application "Finder"
			set PathCount to count of items in aList
			set OriginalfileList to name of every file of CustFolder
			set fileCount to count of items in OriginalfileList
			set FlatFolder to make new folder at alias CustFolder with properties {name:"0_Flat"}
			set FoldFolder to make new folder at alias CustFolder with properties {name:"0_Folded"}
			set OriginalFolder to make new folder at alias CustFolder with properties {name:"0_Original"}
			--set current view of container window of CustFolder to list view
			set current view of container window of every folder of CustFolder to list view --or column view etc
			(open folder CustFolder) activate
			duplicate files of folder CustFolder to OriginalFolder
			---------------------------------
			repeat with t from 1 to fileCount
				tell application "Adobe Acrobat Pro"
					activate
					set theDocA to item t of OriginalfileList
					open ((CustFolder & theDocA) as string) with invisible
					--open ((CustFolder & theDocA) as string)
					---------------------------------
					tell document theDocA
						tell page 1
							set _1point to "0.3527777778" as number --1 point is this as mm
							set {0, theHeight, theWidth, 0} to bleed box
							set theHeight to ((round (theHeight * _1point) rounding up))
							set theWidth to ((round (theWidth * _1point) rounding down))
						end tell
					end tell
					---------------------------------
					if theHeight is 468 and theWidth is 684 then
						--set mydoc to active doc
						set mydoc to document 1
						set myCustFolder to POSIX path of (CustFolder)
						set NewPdf to myCustFolder & theDocA
						tell mydoc
							save to POSIX file NewPdf with linearize
						end tell
						close front document saving no
						tell application "Finder"
							set AllFiles to (files of folder CustFolder whose name is theDocA)
							duplicate AllFiles to FlatFolder
							delete AllFiles
						end tell
						---------------------------------
					else
						--set mydoc to active doc
						set mydoc to document 1
						set myCustFolder to POSIX path of (CustFolder)
						set NewPdf to myCustFolder & theDocA
						tell mydoc
							save to POSIX file NewPdf with linearize
						end tell
						close front document saving no
						tell application "Finder"
							set AllFiles to (files of folder CustFolder whose name is theDocA)
							duplicate AllFiles to FoldFolder
							delete AllFiles
						end tell
					end if
				end tell
			end repeat
			activate
			display dialog "UHG script has completed successfully." giving up after 2
		end tell
	end timeout
end open


So much better than what I had! Only thing is, it’s putting all files in the 0_Folded folder. The files that are 468pt x 684pt should be going to the 0_Flat folder. Any thoughts?

Thank you!
Trisha

It works! Ok, here’s what I ended up with:

on open aList
	with timeout of (30 * minutes) seconds
		set CustFolder to item 1 of aList
		tell application "Finder"
			set PathCount to count of items in aList
			set OriginalfileList to name of every file of CustFolder
			set fileCount to count of items in OriginalfileList
			set FlatFolder to make new folder at alias CustFolder with properties {name:"0_Flat"}
			set FoldFolder to make new folder at alias CustFolder with properties {name:"0_Folded"}
			set OriginalFolder to make new folder at alias CustFolder with properties {name:"0_Original"}
			set RightFolder to make new folder at FoldFolder with properties {name:"0_Right"}
			set LeftFolder to make new folder at FoldFolder with properties {name:"0_Left"}
			--set current view of container window of CustFolder to list view
			set current view of container window of every folder of CustFolder to list view --or column view etc
			(open folder CustFolder) activate
			duplicate files of folder CustFolder to OriginalFolder
			---------------------------------
			repeat with t from 1 to fileCount
				tell application "Adobe Acrobat Pro"
					activate
					set theDocA to item t of OriginalfileList
					open ((CustFolder & theDocA) as string) with invisible
					--open ((CustFolder & theDocA) as string)
					---------------------------------
					tell document theDocA
						tell page 1
							set {0, theHeight, theWidth, 0} to bleed box
							set the_number_to_round to theHeight
							set the_decimal_precision to 1
							my round_to_decimal_places(the_number_to_round, the_decimal_precision)
							set theHeight to result
							set the_number_to_round to theWidth
							set the_decimal_precision to 1
							my round_to_decimal_places(the_number_to_round, the_decimal_precision)
							set theWidth to result
						end tell
					end tell
					---------------------------------
					if theHeight is 468 and theWidth is 684 then
						--set mydoc to active doc
						set mydoc to document 1
						set myCustFolder to POSIX path of (CustFolder)
						set NewPdf to myCustFolder & theDocA
						tell mydoc
							save to POSIX file NewPdf with linearize
						end tell
						close front document saving no
						tell application "Finder"
							set AllFiles to (files of folder CustFolder whose name is theDocA)
							duplicate AllFiles to FlatFolder
							delete AllFiles
						end tell
						---------------------------------
					else
						--set mydoc to active doc
						set mydoc to document 1
						set myCustFolder to POSIX path of (CustFolder)
						set NewPdf to myCustFolder & theDocA
						tell mydoc
							save to POSIX file NewPdf with linearize
						end tell
						close front document saving no
						tell application "Finder"
							set AllFiles to (files of folder CustFolder whose name is theDocA)
							duplicate AllFiles to FoldFolder
							delete AllFiles
						end tell
					end if
				end tell
			end repeat
			
			tell application "Finder"
				set fileList to every file of FoldFolder
				repeat with i in fileList
					if (name of i) does not end with "_p2.pdf" then
						move i to folder "0_Left" of FoldFolder
					end if
				end repeat
				try
					duplicate every file of folder "0_Left" of FoldFolder to folder "0_Right" of FoldFolder --    LeftFolder to folder RightFolder
				end try
			end tell
			
			
			tell application "Finder"
				set leftList to every file of LeftFolder
				repeat with leftfile in leftList
					set l to name of leftfile
					if l ends with ".pdf" then
						set l to (text 1 through -5 of l) & "_L" & ".pdf" as string
						set name of leftfile to l
					end if
				end repeat
				--	end tell
				
				--	tell application "Finder"
				set rightList to every file of RightFolder
				repeat with rightfile in rightList
					set r to name of rightfile
					if r ends with ".pdf" then
						set r to (text 1 through -5 of r) & "_R" & ".pdf" as string
						set name of rightfile to r
					end if
				end repeat
			end tell
			activate
			display dialog "UHG script has completed successfully." giving up after 2
		end tell
	end timeout
end open


on round_to_decimal_places(the_number_to_round, the_decimal_precision)
	set multiplier to 10 ^ the_decimal_precision
	the_number_to_round * multiplier
	round result
	return result / multiplier
end round_to_decimal_places

Thank you so much for your help Budgie, I really appreciate it!!

the possible reason for all the files going to the 0_Folded folder is that I thought the measurements were in mm
so I set up conversion of point to mm

set _1point to "0.3527777778" as number --1 point is this as mm

glad you got it going for you

merry xmas