Tuesday, June 27, 2017

#1 2017-06-12 03:49:48 pm

Prepress@BIP
Member
Registered: 2010-02-24
Posts: 10

Help in cleaning up script

Hello everyone,

I am new to AppleScript and I have a project I have been working on off and on now for a few weeks. It is cobbled together from things I have seen here and through some Google-fu.

I work at a printing company and we send out PDF proofs. We want to automate that process. What I have works and works well, but I am sure it can be cleaned up to be more efficient and possibly quicker (though I don't find it to be slow).

What it does is when I export a PDF it gets the Metadata for the Group Name and stores it as a variable, then it strips the file name down to just the job/quote number. It then watermarks, each file and saves to the groups folder. Then finds all items for that same job number and combines them into one as saves to a different folder where it will get a sign off sheet and security applied.

Hopefully it is fairly clear how I am doing all of this when you see the code....it's a lot! Well to me anyway. There are a bunch of situations that could be in play so there are a ton of if statements. I just finished adding a bunch to cover a situation where there might be some items that are under a job number and others start as a quote but get exported at the same time. We are using a script in InDesign that will export a PDF of all open windows at once.

Thanks for your time!

Applescript:

-- strip _ or - from filename
on findAndReplaceInText(theText, theSearchString, theReplacementString)
   set AppleScript's text item delimiters to theSearchString
   set theTextItems to every text item of theText
   set AppleScript's text item delimiters to theReplacementString
   set theText to theTextItems as string
   set AppleScript's text item delimiters to ""
   return theText
end findAndReplaceInText


-- split filename into pieces
on splitText(theText, theDelimiter)
   set AppleScript's text item delimiters to theDelimiter
   set theTextItems to every text item of theText
   set AppleScript's text item delimiters to ""
   return theTextItems
end splitText

--folders to empty after script is finished
set deleteWM to ":Users:Prepress:Desktop:Proofing:ToWatermark"
set deleteWMNSO to ":Users:Prepress:Desktop:Proofing:ToWatermark_NSO"
set deleteASF to ":Users:Prepress:Desktop:Proofing:AddSignOff"


-- find group name
tell application "Finder" to set pdfList to (files of folder "ToWatermark" of folder "Proofing" of desktop) as alias list
tell application "Finder" to set metadataFile to (item 1 of my pdfList) as alias
set groupName to do shell script "mdls -name kMDItemTitle -raw " & space & quoted form of POSIX path of metadataFile

-- find job numbers
set filesFound to {}
set jobNumbers to {}
set job1Files to {}
set job2Files to {}
set nextItem to 1

tell application "Finder" to set myFiles to name of every file of (folder "ToWatermark" of folder "Proofing" of desktop)

--loop used for populating list filesFound with all filenames found
repeat with i in myFiles
   set end of filesFound to (item nextItem of myFiles)
   set nextItem to (nextItem + 1)
end repeat

set nextItem to 1 --reset counter to 1

--loop used for pulling each filename from list filesFound and return all job numbers
repeat with i in filesFound
   if text 6 through 7 of i contains "_" then
       set end of jobNumbers to item 1 of (splitText(item nextItem of filesFound, "_"))
       set nextItem to (nextItem + 1)
   else if text 6 through 7 of i contains "-" then
       set end of jobNumbers to item 1 of (splitText(item nextItem of filesFound, "-"))
       set nextItem to (nextItem + 1)
   else
       set end of jobNumbers to item 1 of (splitText(item nextItem of filesFound, space))
       set nextItem to (nextItem + 1)
   end if
end repeat

--remove duplicate listings
set jobList to items of jobNumbers
set uniquelist to {}
repeat with i from 1 to count of jobList
   set thisJob to item i of jobList
   if thisJob is not in uniquelist then set end of uniquelist to thisJob
end repeat

--set variables for unique job numbers found
try
   set jobNumber1 to item 1 of uniquelist
   set jobNumber2 to item 2 of uniquelist
end try

-- find how many items each job number has
on count_matches(this_list, this_item)
   set the match_counter to 0
   repeat with i from 1 to the count of this_list
       if item i of this_list is this_item then ¬
           set the match_counter to the match_counter + 1
   end repeat
   return the match_counter
end count_matches
try
   set job1Items to count_matches(jobList, jobNumber1)
   set job2Items to count_matches(jobList, jobNumber2)
end try


-- settings to be used or combining PDFs later
set outputFolder to "/Users/Prepress/Desktop/Proofing/AddSignOff/"
set pdfFiles to ""

-- find other items in job
if (count of uniquelist) is 1 then --only 1 job number
   set matchingPDFs to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber1 & "*.pdf\"" --search for pdfs with same job number in Printers Plan PDF folder for current group
   set outputFile to (outputFolder as text) & jobNumber1 & ".pdf" --combining variable
else if (count of uniquelist) is 2 then --2 job numbers
   set matchingPDFs1 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber1 & "*.pdf\"" --search for pdfs with first job number in Printers Plan PDF folder for current group
   set matchingPDFs2 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber2 & "*.pdf\"" --search for pdfs with second job number in Printers Plan PDF folder for current group
   set outputFile1 to (outputFolder as text) & jobNumber1 & ".pdf" --combining variable
   set outputFile2 to (outputFolder as text) & jobNumber2 & ".pdf" --combining variable
end if

-- Acrobat scripts
set WaterMark to "this.addWatermarkFromFile({
               cDIPath: '/Volumes/bipserver/BIP Library Stuff/proof stamp.pdf',
               nHorizAlign: app.constants.align.center,
               nVertAlign: app.constants.align.center,
               nRotation: 45,
               nScale: -1,
               bOnScreen: false
               });"


set destFolder to "/Volumes/bipserver/PrintersPlan_PDF_Files/ /"
set destFolder to findAndReplaceInText(destFolder, " ", groupName) as string
set destFolder2 to ("this.saveAs(" & (destFolder as string) & " + this.documentFileName);")
set destFolder2 to findAndReplaceInText(destFolder2, "(/", "('/") as string
set destFolder2 to findAndReplaceInText(destFolder2, "/ +", "/' +") as string
set SaveGroupFolder to destFolder2 as string

set SignOffSheet to "this.insertPages ({
               nPage: -1,
               cPath: '/Volumes/bipserver/BIP Library Stuff/PDF Proof box 1-16.pdf',
               nStart: 0
               });"


set PDF_Security to "var policyArray = security.getSecurityPolicies(); for (var i = 0; i < policyArray.length; i++) {if (policyArray[i].name == 'BIPPDF') { var myPolicy = policyArray[i]}} this.encryptUsingPolicy( myPolicy );"

set SaveAs to "this.saveAs('/Volumes/bipserver/PDFs_for_Email/' + this.documentFileName);"

try
   set SaveAsJobNumber1 to "this.saveAs('/Volumes/bipserver/PDFs_for_Email/' + this.documentFileName);"
   set SaveAsJobNumber1 to findAndReplaceInText(SaveAsJobNumber1, "this.documentFileName", quoted form of jobNumber1) as string
   set SaveAsJobNumber1 to findAndReplaceInText(SaveAsJobNumber1, "');", ".pdf');") as string
   
   set SaveAsJobNumber2 to "this.saveAs('/Volumes/bipserver/PDFs_for_Email/' + this.documentFileName);"
   set SaveAsJobNumber2 to findAndReplaceInText(SaveAsJobNumber2, "this.documentFileName", quoted form of jobNumber2) as string
   set SaveAsJobNumber2 to findAndReplaceInText(SaveAsJobNumber2, "');", ".pdf');") as string
end try

set ClosePDF to "this.closeDoc();"


-- processing
if (count of uniquelist) is 1 then -- only one job number
   if (count of paragraphs in my matchingPDFs) is 0 and (count of items in my pdfList) is 1 then -- first proof, single item, do whole process in one pass
       tell application "Adobe Acrobat"
           open my pdfList
           do script my WaterMark
           do script my SaveGroupFolder
           do script my SignOffSheet
           do script my PDF_Security
           do script my SaveAsJobNumber1
           do script my ClosePDF
       end tell
       
   else if (count of paragraphs in my matchingPDFs) is 0 and (count of items in my pdfList) is greater than 1 then --first proof, multiple items, watermark all then do rest
       tell application "Adobe Acrobat"
           repeat with listItem in my pdfList
               open listItem
               do script my WaterMark
               do script my SaveGroupFolder
               do script my ClosePDF
           end repeat
       end tell
       
       set matchingPDFs to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber1 & "*.pdf\""
       
       tell application "Finder" to set pdfsToCombine to paragraphs of my matchingPDFs as alias list
       
       repeat with p in my pdfsToCombine
           set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
       end repeat
       do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile & my pdfFiles
       
       tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
       
       tell application "Adobe Acrobat"
           open my proofPDF
           do script my SignOffSheet
           do script my PDF_Security
           do script my SaveAs
           do script my ClosePDF
       end tell
       
   else if (count of paragraphs in my matchingPDFs) is greater than 0 then --not first proof, watermark all then do rest
       tell application "Adobe Acrobat"
           repeat with listItem in my pdfList
               open listItem
               do script my WaterMark
               do script my SaveGroupFolder
               do script my ClosePDF
           end repeat
       end tell
       
       set matchingPDFs to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber1 & "*.pdf\""
       
       tell application "Finder" to set pdfsToCombine to paragraphs of my matchingPDFs as alias list
       
       if (count of items in pdfsToCombine) is 1 then
           tell application "Adobe Acrobat"
               open my pdfsToCombine
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAsJobNumber1
               do script my ClosePDF
           end tell
       else
           repeat with p in my pdfsToCombine
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile & my pdfFiles
           
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
       end if
   end if
else if (count of uniquelist) is 2 then -- two job numbers
   if (count of paragraphs in my matchingPDFs1) is 0 and (count of paragraphs in my matchingPDFs2) is 0 then --no pdfs for either in PrintersPlanPDFs
       if job1Items is 1 and job2Items is 1 then --one item for each number, first proof, do whole thing in one pass for each file
           tell application "Adobe Acrobat"
               open item 1 of my pdfList
               do script my WaterMark
               do script my SaveGroupFolder
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAsJobNumber1
               do script my ClosePDF
               open item 2 of my pdfList
               do script my WaterMark
               do script my SaveGroupFolder
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAsJobNumber2
               do script my ClosePDF
           end tell
       else if job1Items is greater than 1 and job2Items is 1 then --multiple items for first number, one item for second. watermark all for first then do rest. do whole process in one pass for second job number
           set nextItem to 1
           try
               repeat with i in my pdfList
                   if nextItem is less than or equal to job1Items then
                       set end of job1Files to (item nextItem of my pdfList)
                       set nextItem to (nextItem + 1)
                   end if
               end repeat
           end try
           tell application "Adobe Acrobat"
               open item (job1Items + 1) of my pdfList
               do script my WaterMark
               do script my SaveGroupFolder
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAsJobNumber2
               do script my ClosePDF
               repeat with listItem in my job1Files
                   open listItem
                   do script my WaterMark
                   do script my SaveGroupFolder
                   do script my ClosePDF
               end repeat
           end tell
           
           set matchingPDFs1 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber1 & "*.pdf\""
           
           tell application "Finder" to set pdfsToCombine1 to paragraphs of my matchingPDFs1 as alias list
           
           repeat with p in my pdfsToCombine1
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile1 & my pdfFiles
           
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
       else if job1Items is 1 and job2Items is greater than 1 then --one item for first number, multiple for second, do whole process in one pass for first job number, watermark all in second then do rest
           set nextItem to (job1Items + 1)
           try
               repeat with i in my pdfList
                   set end of job2Files to (item nextItem of my pdfList)
                   set nextItem to (nextItem + 1)
               end repeat
           end try
           tell application "Adobe Acrobat"
               open item 1 of my pdfList
               do script my WaterMark
               do script my SaveGroupFolder
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAsJobNumber1
               do script my ClosePDF
               repeat with listItem in my job2Files
                   open listItem
                   do script my WaterMark
                   do script my SaveGroupFolder
                   do script my ClosePDF
               end repeat
           end tell
           
           set matchingPDFs2 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber2 & "*.pdf\""
           
           tell application "Finder" to set pdfsToCombine2 to paragraphs of my matchingPDFs2 as alias list
           
           repeat with p in my pdfsToCombine2
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile2 & my pdfFiles
           
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
       else if job1Items is greater than 1 and job2Items is greater than 1 then --multiple items for both numbers, watermark all items then combine jobs separately and complete
           tell application "Adobe Acrobat"
               repeat with listItem in my pdfList
                   open listItem
                   do script my WaterMark
                   do script my SaveGroupFolder
                   do script my ClosePDF
               end repeat
           end tell
           
           set matchingPDFs1 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber1 & "*.pdf\""
           set matchingPDFs2 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber2 & "*.pdf\""
           
           tell application "Finder"
               set pdfsToCombine1 to paragraphs of my matchingPDFs1 as alias list
               set pdfsToCombine2 to paragraphs of my matchingPDFs2 as alias list
           end tell
           
           repeat with p in my pdfsToCombine1
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile1 & my pdfFiles
           
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
           
           tell application "System Events" to delete (files of folder deleteASF)
           
           set pdfFiles to ""
           repeat with p in my pdfsToCombine2
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile2 & my pdfFiles
           
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
       end if
   else if (count of paragraphs in my matchingPDFs1) is greater than 0 and (count of paragraphs in my matchingPDFs2) is 0 then --pdfs for first, no pdfs for second in PrintersPlanPDFs
       if job1Items is 1 and job2Items is 1 then
           tell application "Adobe Acrobat"
               open item 1 of my pdfList
               do script my WaterMark
               do script my SaveGroupFolder
               do script my ClosePDF
               open item 2 of my pdfList
               do script my WaterMark
               do script my SaveGroupFolder
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAsJobNumber2
               do script my ClosePDF
           end tell
           
           set matchingPDFs1 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber1 & "*.pdf\""
           
           tell application "Finder" to set pdfsToCombine1 to paragraphs of my matchingPDFs1 as alias list
           
           repeat with p in my pdfsToCombine1
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile1 & my pdfFiles
           
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
       else if job1Items is greater than 1 and job2Items is 1 then
           set nextItem to 1
           try
               repeat with i in my pdfList
                   if nextItem is less than or equal to job1Items then
                       set end of job1Files to (item nextItem of my pdfList)
                       set nextItem to (nextItem + 1)
                   end if
               end repeat
           end try
           tell application "Adobe Acrobat"
               open item (job1Items + 1) of my pdfList
               do script my WaterMark
               do script my SaveGroupFolder
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAsJobNumber2
               do script my ClosePDF
               repeat with listItem in my job1Files
                   open listItem
                   do script my WaterMark
                   do script my SaveGroupFolder
                   do script my ClosePDF
               end repeat
           end tell
           
           set matchingPDFs1 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber1 & "*.pdf\""
           
           tell application "Finder" to set pdfsToCombine1 to paragraphs of my matchingPDFs1 as alias list
           
           repeat with p in my pdfsToCombine1
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile1 & my pdfFiles
           
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
       else if job1Items is 1 and job2Items is greater than 1 then
           tell application "Adobe Acrobat"
               repeat with listItem in my pdfList
                   open listItem
                   do script my WaterMark
                   do script my SaveGroupFolder
                   do script my ClosePDF
               end repeat
           end tell
           
           set matchingPDFs1 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber1 & "*.pdf\""
           set matchingPDFs2 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber2 & "*.pdf\""
           
           tell application "Finder"
               set pdfsToCombine1 to paragraphs of my matchingPDFs1 as alias list
               set pdfsToCombine2 to paragraphs of my matchingPDFs2 as alias list
           end tell
           
           repeat with p in my pdfsToCombine1
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile1 & my pdfFiles
           
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
           
           tell application "System Events" to delete (files of folder deleteASF)
           
           set pdfFiles to ""
           repeat with p in my pdfsToCombine2
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile2 & my pdfFiles
           
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
       else if job1Items is greater than 1 and job2Items is greater than 1 then
           tell application "Adobe Acrobat"
               repeat with listItem in my pdfList
                   open listItem
                   do script my WaterMark
                   do script my SaveGroupFolder
                   do script my ClosePDF
               end repeat
           end tell
           
           set matchingPDFs1 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber1 & "*.pdf\""
           set matchingPDFs2 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber2 & "*.pdf\""
           
           tell application "Finder"
               set pdfsToCombine1 to paragraphs of my matchingPDFs1 as alias list
               set pdfsToCombine2 to paragraphs of my matchingPDFs2 as alias list
           end tell
           
           repeat with p in my pdfsToCombine1
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile1 & my pdfFiles
           
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
           
           tell application "System Events" to delete (files of folder deleteASF)
           
           set pdfFiles to ""
           repeat with p in my pdfsToCombine2
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile2 & my pdfFiles
           
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
       end if
   else if (count of paragraphs in my matchingPDFs1) is 0 and (count of paragraphs in my matchingPDFs2) is greater than 0 then --no pdfs for first, pdfs for second in PrintersPlanPDFs
       if job1Items is 1 and job2Items is 1 then
           tell application "Adobe Acrobat"
               open item 1 of my pdfList
               do script my WaterMark
               do script my SaveGroupFolder
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAsJobNumber1
               do script my ClosePDF
               open item 2 of my pdfList
               do script my WaterMark
               do script my SaveGroupFolder
               do script my ClosePDF
           end tell
           
           set matchingPDFs2 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber2 & "*.pdf\""
           
           tell application "Finder" to set pdfsToCombine2 to paragraphs of my matchingPDFs2 as alias list
           
           repeat with p in my pdfsToCombine2
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile2 & my pdfFiles
           
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
       else if job1Items is greater than 1 and job2Items is 1 then
           tell application "Adobe Acrobat"
               repeat with listItem in my pdfList
                   open listItem
                   do script my WaterMark
                   do script my SaveGroupFolder
                   do script my ClosePDF
               end repeat
           end tell
           
           set matchingPDFs1 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber1 & "*.pdf\""
           set matchingPDFs2 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber2 & "*.pdf\""
           
           tell application "Finder"
               set pdfsToCombine1 to paragraphs of my matchingPDFs1 as alias list
               set pdfsToCombine2 to paragraphs of my matchingPDFs2 as alias list
           end tell
           
           repeat with p in my pdfsToCombine1
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile1 & my pdfFiles
           
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
           
           tell application "System Events" to delete (files of folder deleteASF)
           
           set pdfFiles to ""
           repeat with p in my pdfsToCombine2
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile2 & my pdfFiles
           
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
       else if job1Items is 1 and job2Items is greater than 1 then
           set nextItem to (job1Items + 1)
           try
               repeat with i in my pdfList
                   if nextItem is less than or equal to job2Items then
                       set end of job2Files to (item nextItem of my pdfList)
                       set nextItem to (nextItem + 1)
                   end if
               end repeat
           end try
           tell application "Adobe Acrobat"
               open item 1 of my pdfList
               do script my WaterMark
               do script my SaveGroupFolder
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAsJobNumber1
               do script my ClosePDF
               repeat with listItem in my job2Files
                   open listItem
                   do script my WaterMark
                   do script my SaveGroupFolder
                   do script my ClosePDF
               end repeat
           end tell
           
           set matchingPDFs2 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber2 & "*.pdf\""
           
           tell application "Finder" to set pdfsToCombine2 to paragraphs of my matchingPDFs2 as alias list
           
           repeat with p in my pdfsToCombine2
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile2 & my pdfFiles
           
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
       else if job1Items is greater than 1 and job2Items is greater than 1 then
           tell application "Adobe Acrobat"
               repeat with listItem in my pdfList
                   open listItem
                   do script my WaterMark
                   do script my SaveGroupFolder
                   do script my ClosePDF
               end repeat
           end tell
           
           set matchingPDFs1 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber1 & "*.pdf\""
           set matchingPDFs2 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber2 & "*.pdf\""
           
           tell application "Finder"
               set pdfsToCombine1 to paragraphs of my matchingPDFs1 as alias list
               set pdfsToCombine2 to paragraphs of my matchingPDFs2 as alias list
           end tell
           
           repeat with p in my pdfsToCombine1
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile1 & my pdfFiles
           
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
           
           tell application "System Events" to delete (files of folder deleteASF)
           
           set pdfFiles to ""
           repeat with p in my pdfsToCombine2
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile2 & my pdfFiles
           
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
       end if
   else if (count of paragraphs in my matchingPDFs1) is greater than 0 and (count of paragraphs in my matchingPDFs2) is greater than 0 then --pdfs for both in PrintersPlanPDFs
       tell application "Adobe Acrobat"
           repeat with listItem in my pdfList
               open listItem
               do script my WaterMark
               do script my SaveGroupFolder
               do script my ClosePDF
           end repeat
       end tell
       
       set matchingPDFs1 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber1 & "*.pdf\""
       set matchingPDFs2 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber2 & "*.pdf\""
       
       tell application "Finder"
           set pdfsToCombine1 to paragraphs of my matchingPDFs1 as alias list
           set pdfsToCombine2 to paragraphs of my matchingPDFs2 as alias list
       end tell
       
       repeat with p in my pdfsToCombine1
           set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
       end repeat
       do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile1 & my pdfFiles
       
       tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
       
       tell application "Adobe Acrobat"
           open my proofPDF
           do script my SignOffSheet
           do script my PDF_Security
           do script my SaveAs
           do script my ClosePDF
       end tell
       
       tell application "System Events" to delete (files of folder deleteASF)
       
       set pdfFiles to ""
       repeat with p in my pdfsToCombine2
           set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
       end repeat
       do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile2 & my pdfFiles
       
       tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
       
       tell application "Adobe Acrobat"
           open my proofPDF
           do script my SignOffSheet
           do script my PDF_Security
           do script my SaveAs
           do script my ClosePDF
       end tell
   end if
end if

tell application "System Events"
   delete (files of folder deleteWM)
   delete (files of folder deleteASF)
   delete (files of folder deleteWMNSO)
end tell

Offline

 

#2 2017-06-13 05:07:49 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3059

Re: Help in cleaning up script

I tried to clean your script but as I don't own ADOBE products I can't test.

Applescript:

-- strip _ or - from filename
on findAndReplaceInText(theText, theSearchString, theReplacementString)
   set AppleScript's text item delimiters to theSearchString
   set theTextItems to text items of theText
   set AppleScript's text item delimiters to theReplacementString
   set theText to theTextItems as string
   set AppleScript's text item delimiters to ""
   return theText # returns a string. It will be useless to coerce as string later
end findAndReplaceInText


-- split filename into pieces
on |splittext|(theText, theDelimiter)
   set AppleScript's text item delimiters to theDelimiter
   set theTextItems to text items of theText
   set AppleScript's text item delimiters to ""
   return theTextItems
end |splittext|


-- find how many items each job number has
on count_matches(this_list, this_item)
   set the match_counter to 0
   (*
   repeat with i from 1 to count this_list
       if item i of this_list is this_item then ¬
           set match_counter to match_counter + 1
   end repeat
   *)

   repeat with anItem in this_list
       if contents of anItem is this_item then ¬
           set match_counter to match_counter + 1
   end repeat
   return the match_counter
end count_matches

--folders to empty after script is finished
(*
# These paths are incomplete HFS paths.
set deleteWM to ":Users:Prepress:Desktop:Proofing:ToWatermark"
set deleteWMNSO to ":Users:Prepress:Desktop:Proofing:ToWatermark_NSO"
set deleteASF to ":Users:Prepress:Desktop:Proofing:AddSignOff"
*)

# Must be
set deleteWM to "/Users/Prepress/Desktop/Proofing/ToWatermark"
set deleteWMNSO to "/Users/Prepress/Desktop/Proofing/ToWatermark_NSO"
set deleteASF to "/Users/Prepress/Desktop/Proofing/AddSignOff"

-- find group name
tell application "Finder" to set pdfList to (files of folder "ToWatermark" of folder "Proofing" of desktop) as alias list
set metadataFile to (item 1 of my pdfList) as alias
set groupName to do shell script "mdls -name kMDItemTitle -raw " & space & quoted form of POSIX path of metadataFile

-- find job numbers
set filesFound to {}
set jobNumbers to {}
set job1Files to {}
set job2Files to {}

tell application "Finder" to set myFiles to name of every file of (folder "ToWatermark" of folder "Proofing" of desktop)

(*
--loop used for populating list filesFound with all filenames found
set nextItem to 1
repeat with i in myFiles
   set end of filesFound to (item nextItem of myFiles)
   set nextItem to (nextItem + 1)
end repeat
*)

# If I understand well this single instruction replace the loop
copy myFiles to filesFound

set nextItem to 1 --reset counter to 1
--loop used for pulling each filename from list filesFound and return all job numbers
repeat with i in filesFound
   if text 6 through 7 of i contains "_" then
       set end of jobNumbers to item 1 of (|splittext|(item nextItem of filesFound, "_"))
   else if text 6 through 7 of i contains "-" then
       set end of jobNumbers to item 1 of (|splittext|(item nextItem of filesFound, "-"))
   else
       set end of jobNumbers to item 1 of (|splittext|(item nextItem of filesFound, space))
   end if
   set nextItem to (nextItem + 1)
end repeat


--remove duplicate listings
set jobList to items of jobNumbers
set uniquelist to {}
(*
repeat with i from 1 to count of jobList
   set thisJob to item i of jobList
   if thisJob is not in uniquelist then set end of uniquelist to thisJob
end repeat
*)

# If I understand well this loop may replace the "old" loop
repeat with thisJob in jobList
   if thisJob is not in uniquelist then set end of uniquelist to contents of thisJob
end repeat --set variables for unique job numbers found

(*
# Moved below
try
set jobNumber1 to item 1 of uniquelist
   set jobNumber2 to item 2 of uniquelist
end try
*)

(*
#Moved to the top, out of the main code
-- find how many items each job number has
on count_matches(this_list, this_item)
   set the match_counter to 0
   (*
   repeat with i from 1 to count this_list
       if item i of this_list is this_item then ¬
           set match_counter to match_counter + 1
   end repeat
   *)

   repeat with anItem in this_list
       if contents of anItem is this_item then ¬
           set match_counter to match_counter + 1
   end repeat
   return the match_counter
end count_matches
*)
(*
# Moved below
try
   set job1Items to count_matches(jobList, jobNumber1)
   set job2Items to count_matches(jobList, jobNumber2)
end try
*)


-- settings to be used or combining PDFs later
set outputFolder to "/Users/Prepress/Desktop/Proofing/AddSignOff/" # It's a string, no need to coerce it later
set pdfFiles to ""

-- find other items in job
if (count of uniquelist) is 1 then --only 1 job number
   set jobNumber1 to item 1 of uniquelist
   set job1Items to my count_matches(jobList, jobNumber1)
   set matchingPDFs to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber1 & "*.pdf\"" --search for pdfs with same job number in Printers Plan PDF folder for current group
   set outputFile to outputFolder & jobNumber1 & ".pdf" --combining variable
   set jobNumber2 to false
   set job2Items to 0
else if (count of uniquelist) is 2 then --2 job numbers
   set jobNumber1 to item 1 of uniquelist
   set job1Items to my count_matches(jobList, jobNumber1)
   set jobNumber2 to item 2 of uniquelist
   set job2Items to my count_matches(jobList, jobNumber2)
   set matchingPDFs1 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber1 & "*.pdf\"" --search for pdfs with first job number in Printers Plan PDF folder for current group
   set matchingPDFs2 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber2 & "*.pdf\"" --search for pdfs with second job number in Printers Plan PDF folder for current group
   set outputFile1 to outputFolder & jobNumber1 & ".pdf" --combining variable
   set outputFile2 to outputFolder & jobNumber2 & ".pdf" --combining variable
end if

-- Acrobat scripts
set WaterMark to "this.addWatermarkFromFile({
               cDIPath: '/Volumes/bipserver/BIP Library Stuff/proof stamp.pdf',
               nHorizAlign: app.constants.align.center,
               nVertAlign: app.constants.align.center,
               nRotation: 45,
               nScale: -1,
               bOnScreen: false
               });"


set SignOffSheet to "this.insertPages ({
               nPage: -1,
               cPath: '/Volumes/bipserver/BIP Library Stuff/PDF Proof box 1-16.pdf',
               nStart: 0
               });"


set PDF_Security to "var policyArray = security.getSecurityPolicies(); for (var i = 0; i < policyArray.length; i++) {if (policyArray[i].name == 'BIPPDF') { var myPolicy = policyArray[i]}} this.encryptUsingPolicy( myPolicy );"

set SaveAs to "this.saveAs('/Volumes/bipserver/PDFs_for_Email/' + this.documentFileName);"

set destFolder to "/Volumes/bipserver/PrintersPlan_PDF_Files/ /"
set destFolder to findAndReplaceInText(destFolder, " ", groupName) # it's a text object
set destFolder2 to ("this.saveAs(" & (destFolder) & " + this.documentFileName);") # it's a text object
set destFolder2 to findAndReplaceInText(destFolder2, "(/", "('/") # it's a text object
set destFolder2 to findAndReplaceInText(destFolder2, "/ +", "/' +") # it's a text object
set SaveGroupFolder to destFolder2 # it's a text object



try
   set SaveAsJobNumber1 to "this.saveAs('/Volumes/bipserver/PDFs_for_Email/' + this.documentFileName);"
   set SaveAsJobNumber1 to findAndReplaceInText(SaveAsJobNumber1, "this.documentFileName", quoted form of jobNumber1) --as string
   set SaveAsJobNumber1 to findAndReplaceInText(SaveAsJobNumber1, "');", ".pdf');") --as string
   
   if jobNumber2 is not false then
       set SaveAsJobNumber2 to "this.saveAs('/Volumes/bipserver/PDFs_for_Email/' + this.documentFileName);"
       set SaveAsJobNumber2 to findAndReplaceInText(SaveAsJobNumber2, "this.documentFileName", quoted form of jobNumber2) --as string
       set SaveAsJobNumber2 to findAndReplaceInText(SaveAsJobNumber2, "');", ".pdf');") --as string
   end if
end try

set ClosePDF to "this.closeDoc();"


-- processing
if (count uniquelist) is 1 then -- only one job number
   if (count paragraphs in my matchingPDFs) is 0 and (count my pdfList) is 1 then -- first proof, single item, do whole process in one pass
       tell application "Adobe Acrobat"
           open my pdfList
           do script my WaterMark
           do script my SaveGroupFolder
           do script my SignOffSheet
           do script my PDF_Security
           do script my SaveAsJobNumber1
           do script my ClosePDF
       end tell
       
   else if (count paragraphs in my matchingPDFs) is 0 and (count my pdfList) is greater than 1 then --first proof, multiple items, watermark all then do rest
       tell application "Adobe Acrobat"
           repeat with listItem in my pdfList
               open listItem
               do script my WaterMark
               do script my SaveGroupFolder
               do script my ClosePDF
           end repeat
       end tell
       
       set matchingPDFs to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber1 & "*.pdf\""
       # The instruction below doesn't return a list of aliases but a list of posix paths
       --tell application "Finder" to set pdfsToCombine to paragraphs of my matchingPDFs as alias list
       # This shorter one does the same
       set pdfsToCombine to paragraphs of my matchingPDFs
       
       repeat with p in my pdfsToCombine
           # My understanding is that p is already a posix path !
           set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
       end repeat
       do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile & my pdfFiles
       
       # This syntax is curious. It works ONLY if the folder contains a single file
       tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
       
       tell application "Adobe Acrobat"
           open my proofPDF
           do script my SignOffSheet
           do script my PDF_Security
           do script my SaveAs
           do script my ClosePDF
       end tell
       
   else if (count paragraphs in my matchingPDFs) is greater than 0 then --not first proof, watermark all then do rest
       tell application "Adobe Acrobat"
           repeat with listItem in my pdfList
               open listItem
               do script my WaterMark
               do script my SaveGroupFolder
               do script my ClosePDF
           end repeat
       end tell
       
       set matchingPDFs to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber1 & "*.pdf\""
       # The instruction below doesn't return a list of aliases but a list of posix paths
       --tell application "Finder" to set pdfsToCombine to paragraphs of my matchingPDFs as alias list
       # This short one does the same
       set pdfsToCombine to paragraphs of my matchingPDFs
       
       if (count pdfsToCombine) is 1 then
           tell application "Adobe Acrobat"
               open my pdfsToCombine
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAsJobNumber1
               do script my ClosePDF
           end tell
       else
           repeat with p in my pdfsToCombine
               # My understanding is that p is already a posix path !
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile & my pdfFiles
           
           # This syntax is curious. It works ONLY if the folder contains a single file
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
       end if
   end if
else if (count uniquelist) is 2 then -- two job numbers
   if (count paragraphs in my matchingPDFs1) is 0 and (count paragraphs in my matchingPDFs2) is 0 then --no pdfs for either in PrintersPlanPDFs
       if job1Items is 1 and job2Items is 1 then --one item for each number, first proof, do whole thing in one pass for each file
           tell application "Adobe Acrobat"
               open item 1 of my pdfList
               do script my WaterMark
               do script my SaveGroupFolder
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAsJobNumber1
               do script my ClosePDF
               open item 2 of my pdfList
               do script my WaterMark
               do script my SaveGroupFolder
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAsJobNumber2
               do script my ClosePDF
           end tell
       else if job1Items is greater than 1 and job2Items is 1 then --multiple items for first number, one item for second. watermark all for first then do rest. do whole process in one pass for second job number
           set nextItem to 1
           try
               
               # My understanding is that you may replace
               #    repeat with i in my pdfList
               # by
               repeat count my pdfList times
                   if nextItem is less than or equal to job1Items then
                       set end of job1Files to (item nextItem of my pdfList)
                       set nextItem to (nextItem + 1)
                   end if
               end repeat
           end try
           tell application "Adobe Acrobat"
               open item (job1Items + 1) of my pdfList
               do script my WaterMark
               do script my SaveGroupFolder
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAsJobNumber2
               do script my ClosePDF
               repeat with listItem in my job1Files
                   open listItem
                   do script my WaterMark
                   do script my SaveGroupFolder
                   do script my ClosePDF
               end repeat
           end tell
           
           set matchingPDFs1 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber1 & "*.pdf\""
           
           # The instruction below doesn't return a list of aliases but a list of posix paths
           # tell application "Finder" to set pdfsToCombine1 to paragraphs of my matchingPDFs1 as alias list
           # This short one does the same
           set pdfsToCombine1 to paragraphs of my matchingPDFs1
           
           repeat with p in my pdfsToCombine1
               # My understanding is that p is already a posix path !
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile1 & my pdfFiles
           
           # This syntax is curious. It works ONLY if the folder contains a single file
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
       else if job1Items is 1 and job2Items is greater than 1 then --one item for first number, multiple for second, do whole process in one pass for first job number, watermark all in second then do rest
           
           set nextItem to (job1Items + 1)
           try
               # My understanding is that you may replace
               # repeat with i in my pdfList
               # by
               repeat count my pdfList times
                   set end of job2Files to (item nextItem of my pdfList)
                   set nextItem to (nextItem + 1)
               end repeat
           end try
           
           tell application "Adobe Acrobat"
               open item 1 of my pdfList
               do script my WaterMark
               do script my SaveGroupFolder
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAsJobNumber1
               do script my ClosePDF
               repeat with listItem in my job2Files
                   open listItem
                   do script my WaterMark
                   do script my SaveGroupFolder
                   do script my ClosePDF
               end repeat
           end tell
           
           set matchingPDFs2 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber2 & "*.pdf\""
           
           # The instruction below doesn't return a list of aliases but a list of posix paths
           # tell application "Finder" to set pdfsToCombine2 to paragraphs of my matchingPDFs2 as alias list
           # This short one does the same
           set pdfsToCombine2 to paragraphs of my matchingPDFs2
           
           repeat with p in my pdfsToCombine2
               # My understanding is that p is already a posix path !
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile2 & my pdfFiles
           
           # This syntax is curious. It works ONLY if the folder contains a single file
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
       else if job1Items is greater than 1 and job2Items is greater than 1 then --multiple items for both numbers, watermark all items then combine jobs separately and complete
           tell application "Adobe Acrobat"
               repeat with listItem in my pdfList
                   open listItem
                   do script my WaterMark
                   do script my SaveGroupFolder
                   do script my ClosePDF
               end repeat
           end tell
           
           set matchingPDFs1 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber1 & "*.pdf\""
           set matchingPDFs2 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber2 & "*.pdf\""
           # The instructions below don't return list of aliases but list of posix paths
           (*
           tell application "Finder"
               set pdfsToCombine1 to paragraphs of my matchingPDFs1 as alias list
               set pdfsToCombine2 to paragraphs of my matchingPDFs2 as alias list
           end tell
           *)

           # These short ones do the same
           set pdfsToCombine1 to paragraphs of my matchingPDFs1
           set pdfsToCombine2 to paragraphs of my matchingPDFs2
           
           repeat with p in my pdfsToCombine1
               # My understanding is that p is already a posix path !
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile1 & my pdfFiles
           
           # This syntax is curious. It works ONLY if the folder contains a single file
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
           
           tell application "System Events" to delete (files of folder deleteASF)
           
           set pdfFiles to ""
           repeat with p in my pdfsToCombine2
               # My understanding is that p is already a posix path !
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile2 & my pdfFiles
           
           # This syntax is curious. It works ONLY if the folder contains a single file
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
       end if
   else if (count of paragraphs in my matchingPDFs1) is greater than 0 and (count of paragraphs in my matchingPDFs2) is 0 then --pdfs for first, no pdfs for second in PrintersPlanPDFs
       if job1Items is 1 and job2Items is 1 then
           tell application "Adobe Acrobat"
               open item 1 of my pdfList
               do script my WaterMark
               do script my SaveGroupFolder
               do script my ClosePDF
               open item 2 of my pdfList
               do script my WaterMark
               do script my SaveGroupFolder
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAsJobNumber2
               do script my ClosePDF
           end tell
           
           set matchingPDFs1 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber1 & "*.pdf\""
           
           # The instruction below doesn't return a list of aliases but a list of posix paths
           --    tell application "Finder" to set pdfsToCombine1 to paragraphs of my matchingPDFs1 as alias list
           # This short one does the same
           set pdfsToCombine1 to paragraphs of my matchingPDFs1
           
           repeat with p in my pdfsToCombine1
               # My understanding is that p is already a posix path !
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile1 & my pdfFiles
           
           # This syntax is curious. It works ONLY if the folder contains a single file
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
       else if job1Items is greater than 1 and job2Items is 1 then
           set nextItem to 1
           try
               # My understanding is that you may replace
               # repeat with i in my pdfList
               # by
               repeat count my pdfList times
                   if nextItem is less than or equal to job1Items then
                       set end of job1Files to (item nextItem of my pdfList)
                       set nextItem to (nextItem + 1)
                   end if
               end repeat
           end try
           tell application "Adobe Acrobat"
               open item (job1Items + 1) of my pdfList
               do script my WaterMark
               do script my SaveGroupFolder
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAsJobNumber2
               do script my ClosePDF
               repeat with listItem in my job1Files
                   open listItem
                   do script my WaterMark
                   do script my SaveGroupFolder
                   do script my ClosePDF
               end repeat
           end tell
           
           set matchingPDFs1 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber1 & "*.pdf\""
           
           # The instruction below doesn't return a list of aliases but a list of posix paths
           # tell application "Finder" to set pdfsToCombine1 to paragraphs of my matchingPDFs1 as alias list
           # This short one does the same
           set pdfsToCombine1 to paragraphs of my matchingPDFs1
           
           repeat with p in my pdfsToCombine1
               # My understanding is that p is already a posix path !
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile1 & my pdfFiles
           
           # This syntax is curious. It works ONLY if the folder contains a single file
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
       else if job1Items is 1 and job2Items is greater than 1 then
           tell application "Adobe Acrobat"
               repeat with listItem in my pdfList
                   open listItem
                   do script my WaterMark
                   do script my SaveGroupFolder
                   do script my ClosePDF
               end repeat
           end tell
           
           set matchingPDFs1 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber1 & "*.pdf\""
           set matchingPDFs2 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber2 & "*.pdf\""
           (*
           # The instructions below don't return a list of aliases but a list of posix paths
           tell application "Finder"
               set pdfsToCombine1 to paragraphs of my matchingPDFs1 as alias list
               set pdfsToCombine2 to paragraphs of my matchingPDFs2 as alias list
           end tell
           *)

           # These short ones do the same
           set pdfsToCombine1 to paragraphs of my matchingPDFs1
           set pdfsToCombine2 to paragraphs of my matchingPDFs2
           
           repeat with p in my pdfsToCombine1
               # My understanding is that p is already a posix path !
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile1 & my pdfFiles
           
           # This syntax is curious. It works ONLY if the folder contains a single file
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
           
           tell application "System Events" to delete (files of folder deleteASF)
           
           set pdfFiles to ""
           repeat with p in my pdfsToCombine2
               # My understanding is that p is already a posix path !
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile2 & my pdfFiles
           
           # This syntax is curious. It works ONLY if the folder contains a single file
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
       else if job1Items is greater than 1 and job2Items is greater than 1 then
           tell application "Adobe Acrobat"
               repeat with listItem in my pdfList
                   open listItem
                   do script my WaterMark
                   do script my SaveGroupFolder
                   do script my ClosePDF
               end repeat
           end tell
           
           set matchingPDFs1 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber1 & "*.pdf\""
           set matchingPDFs2 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber2 & "*.pdf\""
           (*
           # The instructions below don't return a list of aliases but a list of posix paths
           tell application "Finder"
               set pdfsToCombine1 to paragraphs of my matchingPDFs1 as alias list
               set pdfsToCombine2 to paragraphs of my matchingPDFs2 as alias list
           end tell
           *)

           # These short ones do the same
           set pdfsToCombine1 to paragraphs of my matchingPDFs1
           set pdfsToCombine2 to paragraphs of my matchingPDFs2
           
           repeat with p in my pdfsToCombine1
               # My understanding is that p is already a posix path !
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile1 & my pdfFiles
           
           # This syntax is curious. It works ONLY if the folder contains a single file
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
           
           tell application "System Events" to delete (files of folder deleteASF)
           
           set pdfFiles to ""
           repeat with p in my pdfsToCombine2
               # My understanding is that p is already a posix path !
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile2 & my pdfFiles
           
           # This syntax is curious. It works ONLY if the folder contains a single file
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
       end if
   else if (count paragraphs in my matchingPDFs1) is 0 and (count paragraphs in my matchingPDFs2) is greater than 0 then --no pdfs for first, pdfs for second in PrintersPlanPDFs
       if job1Items is 1 and job2Items is 1 then
           tell application "Adobe Acrobat"
               open item 1 of my pdfList
               do script my WaterMark
               do script my SaveGroupFolder
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAsJobNumber1
               do script my ClosePDF
               open item 2 of my pdfList
               do script my WaterMark
               do script my SaveGroupFolder
               do script my ClosePDF
           end tell
           
           set matchingPDFs2 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber2 & "*.pdf\""
           
           # The instruction below doesn't return a list of aliases but a list of posix paths
           tell application "Finder" to set pdfsToCombine2 to paragraphs of my matchingPDFs2 as alias list
           # This short one does the same
           set pdfsToCombine2 to paragraphs of my matchingPDFs2
           
           repeat with p in my pdfsToCombine2
               # My understanding is theat p is already a posix path !
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile2 & my pdfFiles
           
           # This syntax is curious. It works ONLY if the folder contains a single file
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
       else if job1Items is greater than 1 and job2Items is 1 then
           tell application "Adobe Acrobat"
               repeat with listItem in my pdfList
                   open listItem
                   do script my WaterMark
                   do script my SaveGroupFolder
                   do script my ClosePDF
               end repeat
           end tell
           
           set matchingPDFs1 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber1 & "*.pdf\""
           set matchingPDFs2 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber2 & "*.pdf\""
           (*
           # The instructions below don't return a list of aliases but a list of posix paths
           tell application "Finder"
               set pdfsToCombine1 to paragraphs of my matchingPDFs1 as alias list
               set pdfsToCombine2 to paragraphs of my matchingPDFs2 as alias list
           end tell
           *)

           # These short ones do the same
           set pdfsToCombine1 to paragraphs of my matchingPDFs1
           set pdfsToCombine2 to paragraphs of my matchingPDFs2
           repeat with p in my pdfsToCombine1
               # My understanding is theat p is already a posix path !
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile1 & my pdfFiles
           
           # This syntax is curious. It works ONLY if the folder contains a single file
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
           
           tell application "System Events" to delete (files of folder deleteASF)
           
           set pdfFiles to ""
           repeat with p in my pdfsToCombine2
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile2 & my pdfFiles
           
           # This syntax is curious. It works ONLY if the folder contains a single file
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
       else if job1Items is 1 and job2Items is greater than 1 then
           set nextItem to (job1Items + 1)
           try
               # My understanding is that you may replace
               # repeat with i in my pdfList
               # by
               repeat count my pdfList times
                   if nextItem is less than or equal to job2Items then
                       set end of job2Files to (item nextItem of my pdfList)
                       set nextItem to (nextItem + 1)
                   end if
               end repeat
           end try
           tell application "Adobe Acrobat"
               open item 1 of my pdfList
               do script my WaterMark
               do script my SaveGroupFolder
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAsJobNumber1
               do script my ClosePDF
               repeat with listItem in my job2Files
                   open listItem
                   do script my WaterMark
                   do script my SaveGroupFolder
                   do script my ClosePDF
               end repeat
           end tell
           
           set matchingPDFs2 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber2 & "*.pdf\""
           
           # The instruction below doesn't return a list of aliases but a list of posix paths
           # tell application "Finder" to set pdfsToCombine2 to paragraphs of my matchingPDFs2 as alias list
           # This short one does the same
           set pdfsToCombine2 to paragraphs of my matchingPDFs2
           
           repeat with p in my pdfsToCombine2
               # My understanding is that p is already a posix path !
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile2 & my pdfFiles
           
           # This syntax is curious. It works ONLY if the folder contains a single file
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
       else if job1Items is greater than 1 and job2Items is greater than 1 then
           tell application "Adobe Acrobat"
               repeat with listItem in my pdfList
                   open listItem
                   do script my WaterMark
                   do script my SaveGroupFolder
                   do script my ClosePDF
               end repeat
           end tell
           
           set matchingPDFs1 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber1 & "*.pdf\""
           set matchingPDFs2 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber2 & "*.pdf\""
           (*            
# The instruction below doesn't return a list of aliases but a list of posix paths
           tell application "Finder"
               set pdfsToCombine1 to paragraphs of my matchingPDFs1 as alias list
               set pdfsToCombine2 to paragraphs of my matchingPDFs2 as alias list
           end tell
           *)

           # These short ones do the same
           set pdfsToCombine1 to paragraphs of my matchingPDFs1
           set pdfsToCombine2 to paragraphs of my matchingPDFs2
           
           repeat with p in my pdfsToCombine1
               # My understanding is that p is already a posix path !
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile1 & my pdfFiles
           
           # This syntax is curious. It works ONLY if the folder contains a single file
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
           
           tell application "System Events" to delete (files of folder deleteASF)
           
           set pdfFiles to ""
           repeat with p in my pdfsToCombine2
               # My understanding is that p is already a posix path !
               set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
           end repeat
           do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile2 & my pdfFiles
           
           # This syntax is curious. It works ONLY if the folder contains a single file
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           
           tell application "Adobe Acrobat"
               open my proofPDF
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end tell
       end if
   else if (count of paragraphs in my matchingPDFs1) is greater than 0 and (count of paragraphs in my matchingPDFs2) is greater than 0 then --pdfs for both in PrintersPlanPDFs
       tell application "Adobe Acrobat"
           repeat with listItem in my pdfList
               open listItem
               do script my WaterMark
               do script my SaveGroupFolder
               do script my ClosePDF
           end repeat
       end tell
       
       set matchingPDFs1 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber1 & "*.pdf\""
       set matchingPDFs2 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber2 & "*.pdf\""
       (*
# The instructions below don't return a list of aliases but a list of posix paths
       tell application "Finder"
           set pdfsToCombine1 to paragraphs of my matchingPDFs1 as alias list
           set pdfsToCombine2 to paragraphs of my matchingPDFs2 as alias list
       end tell
       *)

       # These short one do the same
       set pdfsToCombine1 to paragraphs of my matchingPDFs1
       set pdfsToCombine2 to paragraphs of my matchingPDFs2
       
       repeat with p in my pdfsToCombine1
           # My understanding is that p is already a posix path !
           set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
       end repeat
       do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile1 & my pdfFiles
       
       # This syntax is curious. It works ONLY if the folder contains a single file
       tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
       
       tell application "Adobe Acrobat"
           open my proofPDF
           do script my SignOffSheet
           do script my PDF_Security
           do script my SaveAs
           do script my ClosePDF
       end tell
       
       tell application "System Events" to delete (files of folder deleteASF)
       
       set pdfFiles to ""
       repeat with p in my pdfsToCombine2
           # My understanding is that p is already a posix path !
           set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
       end repeat
       do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile2 & my pdfFiles
       
       # This syntax is curious. It works ONLY if the folder contains a single file
       tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
       
       tell application "Adobe Acrobat"
           open my proofPDF
           do script my SignOffSheet
           do script my PDF_Security
           do script my SaveAs
           do script my ClosePDF
       end tell
   end if
end if

tell application "System Events"
   delete (files of folder deleteWM)
   delete (files of folder deleteASF)
   delete (files of folder deleteWMNSO)
end tell

Edited to take care of the jobNumber2 problem

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) mardi 13 juin 2017 12:06:40

Last edited by Yvan Koenig (2017-06-15 05:45:02 am)

Offline

 

#3 2017-06-13 08:22:49 am

DJ Bazzie Wazzie
Member
From: the Netherlands
Registered: 2004-10-20
Posts: 2654

Re: Help in cleaning up script

Prepress@BIP wrote:

Hopefully it is fairly clear how I am doing all of this when you see the code....it's a lot! Well to me anyway. There are a bunch of situations that could be in play so there are a ton of if statements.

Every programmer has it's own style of writing code. Some prefer snake_case variables while others prefer UpperCamelCase or lowerCamelCase or some even mix them to make difference between types of variables. But there are also preferences who has been proven to be working better, especially when the code gets longer.

Instead of rewriting you're code in my style I could give some advise and leave it up to you. The best advise I can give based on your code is to use more functions/handlers and here is why:
1. There is nothing wrong with an function containing a single line of code. Sometimes complex commands with one of more variables is easier to understand when the function name describes better what to do than the command itself. Also use understandable and self declaring routine names to make the code better to understand. For example:

Applescript:

set matchingPDFs to getPDFsByGroupNameAndJobNumber(groupName, jobNumber)

The example above is more clear and cleaner than the do shell script find. It doesn't improve you're script in terms of shrinking the number of lines of code but I believe understandable code is always better than compressed almost unreadable code.

2The exact number is hard to tell but it is fair to say that when you have 50 lines of consecutive code, your code is too long. The function running the code is too broad en general and not doing a specific task and should be split. It's important that each routine does it's own task. For example:

Applescript:

set matchingPDFs to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber1 & "*.pdf\""
# The instruction below doesn't return a list of aliases but a list of posix paths
--tell application "Finder" to set pdfsToCombine to paragraphs of my matchingPDFs as alias list
# This shorter one does the same
set pdfsToCombine to paragraphs of my matchingPDFs

repeat with p in my pdfsToCombine
# My understanding is that p is already a posix path !
set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
end repeat
do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile & my pdfFiles

can be written down as:

Applescript:

on PDFCombineFiles(pFileList, pOutputFile)
   do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of pOutputFile & space & quotedParameterStringFromPosixPaths(pFileList)
end PDFCombineFiles

on getPDFsByGroupNameAndJobNumber(groupName, jobNumber)
   return paragraphs of (do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber & "*.pdf\"")
end getPDFsByGroupNameAndJobNumber

on quotedParameterStringFromPosixPaths(lst)
   repeat with i from 1 to count lst
       set item i of lst to quoted form of item i of lst
   end repeat
   return join(lst, space)
end quotedParameterStringFromPosixPaths

on join(textItems, separator)
   tell AppleScript
       set oldTIDs to text item delimiters
       set text item delimiters to separator
       set theList to textItems as string
       set text item delimiters to oldTIDs
   end tell
   return theList
end join

Again at first this doesn't look like it improves in lines of code. But when you look at the code calling the routine it's much smaller and can be used many times again in your code:

Applescript:

set matchingPDFs to getPDFsByGroupNameAndJobNumber(groupName, jobNumber1)
PDFCombineFiles(matchingPDFs, my outputFile)

It's not only a lot smaller but the code is split in groups with its own tasks. quoting, joining components, finding a file, combining pdf files has its own routines. They make your code better to read and when used multiple times more compact but the routine can also be updated or replaced from a single space in your code. This change will save already 200 lines of code.

3 Last but most important reason to use functions is to avoid repetitive code. I see a lot of repetitive code and even when you have function calls you can wrap those in an new function. When looking at the code for five minutes can see that the code can be shrunk from 700lines to an easily 300 lines of code solution.

other hint: Don't be afraid of concatenating strings. I see you create static strings and after that you use an search and replace to insert addition characters. With the ampersand you can bind strings together.

Applescript:

set SaveAsJobNumber1 to "this.saveAs('/Volumes/bipserver/PDFs_for_Email/" & jobNumber2 & ".pdf');"

Last edited by DJ Bazzie Wazzie (2017-06-13 08:24:32 am)

Offline

 

#4 2017-06-13 11:41:56 am

Prepress@BIP
Member
Registered: 2010-02-24
Posts: 10

Re: Help in cleaning up script

Thank you guys SO MUCH! I knew It could be cleaned up, just didn't really know/understand how. Like I said, I found little snippets here and there and piecemealed it together to fit. Everything I have in place does exactly what we need it to, but I wanted to make it more efficient and learn from your guidance. I most definitely have learned. I am going to go back through and apply these suggestions to help reinforce things and hopefully remember to apply these techniques in the future.

Again, I cannot thank you enough.

Might be back with more specific questions about some of the changes suggested.

Offline

 

#5 2017-06-14 11:17:53 am

Prepress@BIP
Member
Registered: 2010-02-24
Posts: 10

Re: Help in cleaning up script

Just wanted to say thanks again! Back with some more thoughts

@Yvan Koenig
This:

Applescript:

# If I understand well this single instruction replace the loop
copy myFiles to filesFound

made me realize I don't need filesFound at all. Can run this directly on myFiles:

Applescript:

repeat with i in filesFound
if text 6 through 7 of i contains "_" then
set end of jobNumbers to item 1 of (splittext {item nextItem of filesFound, "_"})
else if text 6 through 7 of i contains "-" then
set end of jobNumbers to item 1 of (splittext {item nextItem of filesFound, "-"})
else
set end of jobNumbers to item 1 of (splittext {item nextItem of filesFound, space})
end if
set nextItem to (nextItem + 1)
end repeat

Kept getting jobNumber1 not defined error with this

Applescript:

try
set {jobNumber1, jobNumber2} to items 1 thru 2 of uniquelist
end try

Applescript:

# This syntax is curious. It works ONLY if the folder contains a single file
tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias

This is because the AddSignOff folder is just a placeholder and will only contain one file at a time the way I was doing it, but it made me rethink how I handled some instances. Went from this

Applescript:

set matchingPDFs1 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber1 & "*.pdf\""
       set matchingPDFs2 to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber2 & "*.pdf\""
       
       tell application "Finder"
           set pdfsToCombine1 to paragraphs of my matchingPDFs1 as alias list
           set pdfsToCombine2 to paragraphs of my matchingPDFs2 as alias list
       end tell
       
       repeat with p in my pdfsToCombine1
           set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
       end repeat
       do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile1 & my pdfFiles
       
       tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
       
       tell application "Adobe Acrobat"
           open my proofPDF
           do script my SignOffSheet
           do script my PDF_Security
           do script my SaveAs
           do script my ClosePDF
       end tell
       
       tell application "System Events" to delete (files of folder deleteASF)
       
       set pdfFiles to ""
       repeat with p in my pdfsToCombine2
           set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
       end repeat
       do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile2 & my pdfFiles
       
       tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
       
       tell application "Adobe Acrobat"
           open my proofPDF
           do script my SignOffSheet
           do script my PDF_Security
           do script my SaveAs
           do script my ClosePDF
       end tell

to this

Applescript:

set matchingPDFs1 to getPDFsByGroupNameAndJobNumber(groupName, jobNumber1)
       PDFCombineFiles(matchingPDFs1, my outputFile1)
       
       set matchingPDFs2 to getPDFsByGroupNameAndJobNumber(groupName, jobNumber2)
       PDFCombineFiles(matchingPDFs2, my outputFile2)
       
       tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias list
       
       tell application "Adobe Acrobat"
           repeat with listItem in my proofPDF
               open listItem
               do script my SignOffSheet
               do script my PDF_Security
               do script my SaveAs
               do script my ClosePDF
           end repeat
       end tell

@DJ Bazzie Wazzie
Thanks for all the functions/handlers! Are these also called subroutines? I kept telling myself I needed to create subroutines to simplify the main body of the code, but could not figure out how to go about it. These are exactly what I was thinking!

3 Last but most important reason to use functions is to avoid repetitive code. I see a lot of repetitive code and even when you have function calls you can wrap those in an new function. When looking at the code for five minutes can see that the code can be shrunk from 700lines to an easily 300 lines of code solution.

Most of the repetitive stuff contains

Applescript:

do script

which references a javascript for Acrobat. I tried making a function to run this

Applescript:

tell application "Adobe Acrobat"
open my pdfList
do script my WaterMark
do script my SaveGroupFolder
do script my SignOffSheet
do script my PDF_Security
do script my SaveAsJobNumber1
do script my ClosePDF
end tell

but I can't have the do script inside a function. Could you give me an example of something repetitive that could be done in a function?

Once again, thank you guys so much! And man, it's easy for posts to get long on here haha

Offline

 

#6 2017-06-14 12:46:07 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3059

Re: Help in cleaning up script

Prepress@BIP wrote:

Just wanted to say thanks again! Back with some more thoughts

@Yvan Koenig
This:

Applescript:


Kept getting jobNumber1 not defined error with this

Applescript:

try
set {jobNumber1, jobNumber2} to items 1 thru 2 of uniquelist
end try

I don't understand what is the problem.
My code is an alternate way to do exactly what yours was doing.

Applescript:

set uniquelist to {}
# your code
--set variables for unique job numbers found
try
   set jobNumber1 to item 1 of uniquelist
   set jobNumber2 to item 2 of uniquelist
end try
log "point 1"
try
   log jobNumber1
on error errmsg
   log errmsg (*La variable jobNumber1 n’est pas définie.*)
end try
log "point 2"

try
   log jobNumber2
on error errmsg
   log errmsg (*La variable jobNumber2 n’est pas définie.*)
end try
log "point 3"

# my version
try
   set {jobNumber1, jobNumber2} to items 1 thru 2 of uniquelist
end try
log "point 4"
try
   log jobNumber1
on error errmsg
   log errmsg (*La variable jobNumber1 n’est pas définie.*)
end try
log "point 5"

try
   log jobNumber2
on error errmsg
   log errmsg (*La variable jobNumber2 n’est pas définie.*)
end try
log "point 6"

set uniquelist to {"aa"}
# your code
--set variables for unique job numbers found
try
   set jobNumber1 to item 1 of uniquelist
   set jobNumber2 to item 2 of uniquelist
end try
log "point 7"
try
   log jobNumber1 (*aa*)
on error errmsg
   log errmsg
end try
log "point 8"

try
   log jobNumber2
on error errmsg
   log errmsg (*La variable jobNumbers2 n’est pas définie.*)
end try
log "point 9"

# my version
try
   set {jobNumber1, jobNumber2} to items 1 thru 2 of uniquelist
end try
log "point 10"
try
   log jobNumber1 (*aa*)
on error errmsg
   log errmsg
end try
log "point 11"

try
   log jobNumber2
on error errmsg
   log errmsg (*La variable jobNumbers2 n’est pas définie.*)
end try
log "point 12"

set uniquelist to {"aa", "bb"}
# your code
--set variables for unique job numbers found
try
   set jobNumber1 to item 1 of uniquelist
   set jobNumber2 to item 2 of uniquelist
end try
log "point 13"
try
   log jobNumber1 (*aa*)
on error errmsg
   log errmsg
end try
log "point 14"

try
   log jobNumber2 (*bb*)
on error errmsg
   log errmsg
end try
log "point 15"

# my version
try
   set {jobNumber1, jobNumber2} to items 1 thru 2 of uniquelist
end try
log "point 16"
try
   log jobNumber1 (*aa*)
on error errmsg
   log errmsg
end try
log "point 17"

try
   log jobNumber2 (*bb*)
on error errmsg
   log errmsg
end try
log "point 18"

Trying to clean a code doesn't mean that we change its behavior.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) mercredi 14 juin 2017 19:45:25

Offline

 

#7 2017-06-14 01:56:04 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3059

Re: Help in cleaning up script

Here are the basis required to use handlers/functions :

Applescript:

# Some values are constants defined once, so I would store them in properties
property WaterMark : "this.addWatermarkFromFile…" # defined once
property SignOffSheet : "this.insertPages…" # defined once
property PDF_Security : "var policyArray = …"
property ClosePDF : "this.closeDoc();" # defined once


set pdfList to {"a", "b"}
set SaveGroupFolder to destFolder2 --as string
set SaveAsJobNumber1 to "123"

# Some values are calculated in the script so we must pass them in the instruction calling the function
my sampleFunction(pdfList, SaveGroupFolder, SaveAsJobNumber1)

on sampleFunction(pdfList, SaveGroupFolder, SaveAsJobNumber1)
   local fakeLocalVariable # not used here
   
   tell application "Adobe Acrobat"
       open pdfList # don't prefix by my
       do script my WaterMark # prefix by my because it's a property
       do script SaveGroupFolder # don't prefix by my
       do script my SignOffSheet # prefix by my because it's a property
       do script my PDF_Security # prefix by my because it's a property
       do script SaveAsJobNumber1 # don't prefix by my
       do script my ClosePDF # prefix by my because it's a property
   end tell
end sampleFunction

In a handler/function, I never prefix with my the variables passed in the caller instruction and/or the variables defined as local.
I use the prefix my only for properties.
Honestly, I didn't understood why you  coded : do script my something everywhere in your long script.
Maybe it"s because you borrowed the code from a script in which the different values were defined as properties.

When it's not too complicated to achieve, I use properties for values defined as constants which must be used in handler/function.
As I dislike properties taking different values during execution, I define them as standard variables and I pass them in the instructions calling the handlers/functions.
I assume that the script where pieces of codes were borrowed defined the 7 variables used in the handler/function as properties, which would explain the use of my.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) mercredi 14 juin 2017 20:48:29

Offline

 

#8 2017-06-14 02:10:07 pm

Prepress@BIP
Member
Registered: 2010-02-24
Posts: 10

Re: Help in cleaning up script

Thanks Yvan. I should clarify. I get the error if only 1 job number is present. If there are 2 it works fine. Not sure why that would be but that is what is happening.

I will read through the function stuff some more and will be back with more questions/explanations.

Offline

 

#9 2017-06-15 04:59:39 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3059

Re: Help in cleaning up script

Are you sure that it's this part of the script which is the culprit?
Isn't it the instructions trying to use the value later which issue the error?

I will read the code again to try to find the answer but as it's really long I am not sure that I will not miss something.


I found the wrongdoer. It's what I assumed. several instructions use jobNumber2 without checking that it's available.

CAUTION: You name a handler splittext which is the name of a function belonging to a widely used scripting addition named Satimage. It would be a good idea to rename it. Here I renamed it |splittext|.

I edited message #2 trying to solve the jobNumber2 problem.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) jeudi 15 juin 2017 11:59:34

Last edited by Yvan Koenig (2017-06-15 05:46:16 am)

Offline

 

#10 2017-06-15 05:38:10 am

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 4312

Re: Help in cleaning up script

Prepress@BIP wrote:

Thanks Yvan. I should clarify. I get the error if only 1 job number is present. If there are 2 it works fine. Not sure why that would be but that is what is happening.

Hi.

I haven't been following this thread, but if you're referring to the "jobNumber1 not defined" error you said you were getting, there is a slight difference between …

Applescript:

try
   set jobNumber1 to item 1 of uniquelist
   set jobNumber2 to item 2 of uniquelist
end try

… and …

Applescript:

try
   set {jobNumber1, jobNumber2} to items 1 thru 2 of uniquelist
end try

If there's only one item in uniquelist, the first excerpt will successfully set jobNumber1 but error when it tries to access item 2 to set jobNumber2. But the second excerpt tries to access both items before setting either variable, so neither variable gets set if the list contains less than two items.

I haven't looked to see what you're doing with those variables, but, as Yvan's implied, if there's a possibility that either won't be set, that needs to be allowed for in the code.


NG

Offline

 

#11 2017-06-15 10:33:26 am

Prepress@BIP
Member
Registered: 2010-02-24
Posts: 10

Re: Help in cleaning up script

Nigel, that is exactly what I figured was happening and is why I did it the way I did. If there is only 1 job number (which is most of the time) then jobNumber2 never gets asked for later in the script.

Yvan, took your advice on splittext and changed that. Also went ahead and moved the set jobNumber1 and jobNumber2 into that if (count of uniquelist) block like you did.

Are you sure that it's this part of the script which is the culprit?
Isn't it the instructions trying to use the value later which issue the error?

No, like Nigel said, trying to do both at once when there may only be one was causing neither to be set and when jobNumber1 was called was throwing the error. The way I had it would allow jobNumber1 to be set and then skip 2 if it didn't exist.

In regards to the use of my I first saw that here http://macscripter.net/viewtopic.php?pid=96062#p96062
actually posted by Nigel. I then saw it somewhere else while reading up on how to speed up AppleScript and it is very likely I went overboard with it and most likely am misusing it altogether.

For the do script items, those are javascript for Adobe Acrobat and is how it has to be called from AppleScript.

Offline

 

#12 2017-06-15 10:46:11 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3059

Re: Help in cleaning up script

Prepress@BIP wrote:

In regards to the use of my I first saw that here http://macscripter.net/viewtopic.php?pid=96062#p96062
actually posted by Nigel. I then saw it somewhere else while reading up on how to speed up AppleScript and it is very likely I went overboard with it and most likely am misusing it altogether.

In the main part of a script, my is used to fasten the treatment of lists defined as properties.


For the do script items, those are javascript for Adobe Acrobat and is how it has to be called from AppleScript.

What puzzle me is not the do script command by itself, it's the generalized use of [ my ]. None of the documents which I found about the way to call javaScripts from AppleScript use this possessive adjective

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) jeudi 15 juin 2017 17:46:06

Last edited by Yvan Koenig (2017-06-15 11:02:00 am)

Offline

 

#13 2017-06-15 10:59:51 am

Prepress@BIP
Member
Registered: 2010-02-24
Posts: 10

Re: Help in cleaning up script

Ok, so it is not doing me any good then as those are not lists. Thanks yet again. Still looking at the functions stuff trying to wrap my head around it and apply it my my script.

Offline

 

#14 2017-06-15 11:50:49 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3059

Re: Help in cleaning up script

I wanted to edit the script using the handlers proposed by DJ Bazzie Wazzie but I'm not sure that they are right.

DJ Bazzie Wazzie wrote:

For example:

Applescript:

set matchingPDFs to do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber1 & "*.pdf\""
# The instruction below doesn't return a list of aliases but a list of posix paths
--tell application "Finder" to set pdfsToCombine to paragraphs of my matchingPDFs as alias list
# This shorter one does the same
set pdfsToCombine to paragraphs of my matchingPDFs

repeat with p in my pdfsToCombine
# My understanding is that p is already a posix path !
set pdfFiles to pdfFiles & " " & quoted form of POSIX path of p
end repeat
do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of my outputFile & my pdfFiles

can be written down as:

Applescript:

on PDFCombineFiles(pFileList, pOutputFile)
   do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of pOutputFile & space & quotedParameterStringFromPosixPaths(pFileList)
end PDFCombineFiles

on getPDFsByGroupNameAndJobNumber(groupName, jobNumber)
   return paragraphs of (do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber & "*.pdf\"")
end getPDFsByGroupNameAndJobNumber

on quotedParameterStringFromPosixPaths(lst)
   repeat with i from 1 to count lst
       set item i of lst to quoted form of item i of lst
   end repeat
   return join(lst, space)
end quotedParameterStringFromPosixPaths

on join(textItems, separator)
   tell AppleScript
       set oldTIDs to text item delimiters
       set text item delimiters to separator
       set theList to textItems as string
       set text item delimiters to oldTIDs
   end tell
   return theList
end join

I may be wrong but my understanding is thatDJ missed that pdfFiles is a string which is repeatedly concatenated to one or several paths so that the handler must return the string to the main script.

I'm waiting for DJ's feedback before continuing to work upon the script.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) jeudi 15 juin 2017 18:49:05

Offline

 

#15 2017-06-15 03:22:02 pm

Prepress@BIP
Member
Registered: 2010-02-24
Posts: 10

Re: Help in cleaning up script

Thanks to all of your help, this is where I have it now! The main body is 182 lines. Lots of functions at the bottom. Works great and looks cleaner.

Applescript:

--Some values are constants defined once, so store them in properties
property outputFolder : "/Users/Prepress/Desktop/Proofing/AddSignOff/" --output folder for combining PDFs later
property deleteWM : "/Users/Prepress/Desktop/Proofing/ToWatermark"
property deleteWMNSO : "/Users/Prepress/Desktop/Proofing/ToWatermark_NSO" --folders to empty after script is finished
property deleteASF : "/Users/Prepress/Desktop/Proofing/AddSignOff"
--Acrobat scripts that are constant
property WaterMark : "this.addWatermarkFromFile({
               cDIPath: '/Volumes/bipserver/BIP Library Stuff/proof stamp.pdf',
               nHorizAlign: app.constants.align.center,
               nVertAlign: app.constants.align.center,
               nRotation: 45,
               nScale: -1,
               bOnScreen: false
               });"

property SignOffSheet : "this.insertPages ({
               nPage: -1,
               cPath: '/Volumes/bipserver/BIP Library Stuff/PDF Proof box 1-16.pdf',
               nStart: 0
               });"

property PDF_Security : "var policyArray = security.getSecurityPolicies(); for (var i = 0; i < policyArray.length; i++) {if (policyArray[i].name == 'BIPPDF') { var myPolicy = policyArray[i]}} this.encryptUsingPolicy( myPolicy );"
property SaveAs : "this.saveAs('/Volumes/bipserver/PDFs_for_Email/' + this.documentFileName);"
property ClosePDF : "this.closeDoc();"

--variables used later
set filesFound to {}
set jobNumbers to {}
set uniqueJobs to {}
set job1Files to {}
set job2Files to {}
set nextItem to 1

-- find group name
tell application "Finder" to set pdfList to (files of folder "ToWatermark" of folder "Proofing" of desktop) as alias list
set groupName to do shell script "mdls -name kMDItemTitle -raw " & space & quoted form of POSIX path of (item 1 of pdfList)

--loop used for pulling each filename and return all job numbers
tell application "Finder" to set myFiles to name of every file of (folder "ToWatermark" of folder "Proofing" of desktop)
repeat with i in myFiles
   if text 6 through 7 of i contains "_" then
       set end of jobNumbers to item 1 of (breaktext(item nextItem of myFiles, "_"))
   else if text 6 through 7 of i contains "-" then
       set end of jobNumbers to item 1 of (breaktext(item nextItem of myFiles, "-"))
   else
       set end of jobNumbers to item 1 of (breaktext(item nextItem of myFiles, space))
   end if
   set nextItem to (nextItem + 1)
end repeat

--remove duplicate jobNumbers
repeat with thisJob in jobNumbers
   if thisJob is not in uniqueJobs then set end of uniqueJobs to contents of thisJob
end repeat

-- find other items in job
if (count uniqueJobs) is 1 then --only 1 job number
   set jobNumber1 to item 1 of uniqueJobs
   set job1Items to count_matches(jobNumbers, jobNumber1)
   set matchingPDFs to getPDFsByGroupNameAndJobNumber(groupName, jobNumber1) --search for pdfs with same job number in Printers Plan PDF folder for current group
   set outputFile to (outputFolder as text) & jobNumber1 & ".pdf" --combining variable
else if (count uniqueJobs) is 2 then --2 job numbers
   set jobNumber1 to item 1 of uniqueJobs
   set job1Items to count_matches(jobNumbers, jobNumber1)
   set jobNumber2 to item 2 of uniqueJobs
   set job2Items to count_matches(jobNumbers, jobNumber2)
   set matchingPDFs1 to getPDFsByGroupNameAndJobNumber(groupName, jobNumber1) --search for pdfs with first job number in PrintersPlanPDF folder
   set matchingPDFs2 to getPDFsByGroupNameAndJobNumber(groupName, jobNumber2) --search for pdfs with second job number in PrintersPlanPDF folder
   set outputFile1 to (outputFolder as text) & jobNumber1 & ".pdf" --combining variable
   set outputFile2 to (outputFolder as text) & jobNumber2 & ".pdf" --combining variable
end if

-- Acrobat variable scripts
set SaveGroupFolder to "this.saveAs('/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "/' + this.documentFileName);"
try
   set SaveAsJobNumber1 to "this.saveAs('/Volumes/bipserver/PDFs_for_Email/" & jobNumber1 & ".pdf');"
   set SaveAsJobNumber2 to "this.saveAs('/Volumes/bipserver/PDFs_for_Email/" & jobNumber2 & ".pdf');"
end try

-- processing
if (count uniqueJobs) is 1 then -- only one job number
   if (count matchingPDFs) is 0 and (count pdfList) is 1 then -- first proof, single item, do whole process in one pass
       WatermarkGroupFolderSignOffSecuritySaveAsClosePDF(pdfList, SaveGroupFolder, SaveAsJobNumber1)
       
   else if (count matchingPDFs) is 0 and (count pdfList) is greater than 1 then --first proof, multiple items, watermark all then do rest
       WatermarkGroupFolderClose(pdfList, SaveGroupFolder)
       
       set matchingPDFs to getPDFsByGroupNameAndJobNumber(groupName, jobNumber1)
       PDFCombineFiles(matchingPDFs, my outputFile)
       
       tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
       SignOffSecuritySaveCloseSingleItem(proofPDF)
       
   else if (count matchingPDFs) is greater than 0 then --not first proof, watermark all then do rest
       WatermarkGroupFolderClose(pdfList, SaveGroupFolder)
       
       set matchingPDFs to getPDFsByGroupNameAndJobNumber(groupName, jobNumber1)
       if (count matchingPDFs) is 1 then
           SignOffSecuritySaveAsJobNumberClose(matchingPDFs, SaveAsJobNumber1)
           
       else
           PDFCombineFiles(matchingPDFs, my outputFile)
           
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           SignOffSecuritySaveCloseSingleItem(proofPDF)
           
       end if
   end if
else if (count uniqueJobs) is 2 then -- two job numbers
   if (count matchingPDFs1) is 0 and (count matchingPDFs2) is 0 then --no pdfs for either in PrintersPlanPDFs
       if job1Items is 1 and job2Items is 1 then --one item for each number, first proof, do whole thing in one pass for each file
           WatermarkGroupFolderSignOffSecuritySaveAsClosePDF(item 1 of pdfList, SaveGroupFolder, SaveAsJobNumber1)
           WatermarkGroupFolderSignOffSecuritySaveAsClosePDF(item 2 of pdfList, SaveGroupFolder, SaveAsJobNumber2)
           
       else if job1Items is greater than 1 and job2Items is 1 then --multiple items for first number, one item for second. watermark all for first then do rest. do whole process in one pass for second job number
           set job1Files to jobFilesList(1, pdfList, job1Items)
           
           WatermarkGroupFolderSignOffSecuritySaveAsClosePDF(item (job1Items + 1) of pdfList, SaveGroupFolder, SaveAsJobNumber2)
           WatermarkGroupFolderClose(job1Files, SaveGroupFolder)
           
           set matchingPDFs1 to getPDFsByGroupNameAndJobNumber(groupName, jobNumber1)
           PDFCombineFiles(matchingPDFs1, my outputFile1)
           
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           SignOffSecuritySaveCloseSingleItem(proofPDF)
           
       else if job1Items is 1 and job2Items is greater than 1 then --one item for first number, multiple for second, do whole process in one pass for first job number, watermark all in second then do rest
           
           set job2Files to jobFilesList((job1Items + 1), pdfList, job2Items)
           
           WatermarkGroupFolderSignOffSecuritySaveAsClosePDF(item 1 of pdfList, SaveGroupFolder, SaveAsJobNumber1)
           WatermarkGroupFolderClose(job2Files, SaveGroupFolder)
           
           set matchingPDFs2 to getPDFsByGroupNameAndJobNumber(groupName, jobNumber2)
           PDFCombineFiles(matchingPDFs2, my outputFile2)
           
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           SignOffSecuritySaveCloseSingleItem(proofPDF)
           
       else if job1Items is greater than 1 and job2Items is greater than 1 then --multiple items for both numbers, watermark all items then combine jobs separately and complete
           WatermarkGroupFolderClose(pdfList, SaveGroupFolder)
           
           set matchingPDFs1 to getPDFsByGroupNameAndJobNumber(groupName, jobNumber1)
           PDFCombineFiles(matchingPDFs1, my outputFile1)
           
           set matchingPDFs2 to getPDFsByGroupNameAndJobNumber(groupName, jobNumber2)
           PDFCombineFiles(matchingPDFs2, my outputFile2)
           
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias list
           SignOffSecuritySaveCloseMultiItem(proofPDF)
           
       end if
   else if (count matchingPDFs1) is greater than 0 and (count matchingPDFs2) is 0 then --pdfs for first, no pdfs for second in PrintersPlanPDFs
       if job1Items is 1 and job2Items is 1 then
           WatermarkGroupFolderCloseSingleItem(item 1 of pdfList, SaveGroupFolder)
           WatermarkGroupFolderSignOffSecuritySaveAsClosePDF(item 2 of pdfList, SaveGroupFolder, SaveAsJobNumber2)
           
           set matchingPDFs1 to getPDFsByGroupNameAndJobNumber(groupName, jobNumber1)
           
           if (count matchingPDFs1) is 1 then
               SignOffSecuritySaveAsJobNumberClose(matchingPDFs1, SaveAsJobNumber1)
               
           else
               PDFCombineFiles(matchingPDFs1, my outputFile1)
               
               tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
               SignOffSecuritySaveCloseSingleItem(proofPDF)
               
           end if
       else if job1Items is greater than 1 and job2Items is 1 then
           set job1Files to jobFilesList(1, pdfList, job1Items)
           
           WatermarkGroupFolderSignOffSecuritySaveAsClosePDF(item (job1Items + 1) of pdfList, SaveGroupFolder, SaveAsJobNumber2)
           WatermarkGroupFolderClose(job1Files, SaveGroupFolder)
           
           set matchingPDFs1 to getPDFsByGroupNameAndJobNumber(groupName, jobNumber1)
           PDFCombineFiles(matchingPDFs1, my outputFile1)
           
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           SignOffSecuritySaveCloseSingleItem(proofPDF)
           
       else if (job1Items is greater than or equal to 1) and job2Items is greater than 1 then
           WatermarkGroupFolderClose(pdfList, SaveGroupFolder)
           
           set matchingPDFs1 to getPDFsByGroupNameAndJobNumber(groupName, jobNumber1)
           PDFCombineFiles(matchingPDFs1, my outputFile1)
           
           set matchingPDFs2 to getPDFsByGroupNameAndJobNumber(groupName, jobNumber2)
           PDFCombineFiles(matchingPDFs2, my outputFile2)
           
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias list
           SignOffSecuritySaveCloseMultiItem(proofPDF)
       end if
       
   else if (count matchingPDFs1) is 0 and (count matchingPDFs2) is greater than 0 then --no pdfs for first, pdfs for second in PrintersPlanPDFs
       if job1Items is 1 and job2Items is 1 then
           WatermarkGroupFolderSignOffSecuritySaveAsClosePDF(item 1 of pdfList, SaveGroupFolder, SaveAsJobNumber1)
           WatermarkGroupFolderCloseSingleItem(item 2 of pdfList, SaveGroupFolder)
           
           set matchingPDFs2 to getPDFsByGroupNameAndJobNumber(groupName, jobNumber2)
           PDFCombineFiles(matchingPDFs2, my outputFile2)
           
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           SignOffSecuritySaveCloseSingleItem(proofPDF)
           
       else if job1Items is greater than 1 and job2Items is 1 then
           WatermarkGroupFolderClose(pdfList, SaveGroupFolder)
           
           set matchingPDFs1 to getPDFsByGroupNameAndJobNumber(groupName, jobNumber1)
           PDFCombineFiles(matchingPDFs1, my outputFile1)
           
           set matchingPDFs2 to getPDFsByGroupNameAndJobNumber(groupName, jobNumber2)
           PDFCombineFiles(matchingPDFs2, my outputFile2)
           
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias list
           SignOffSecuritySaveCloseMultiItem(proofPDF)
           
       else if job1Items is 1 and job2Items is greater than 1 then
           set job2Files to jobFilesList((job1Items + 1), pdfList, job2Items)
           
           WatermarkGroupFolderSignOffSecuritySaveAsClosePDF(item 1 of pdfList, SaveGroupFolder, SaveAsJobNumber1)
           WatermarkGroupFolderClose(job2Files, SaveGroupFolder)
           
           set matchingPDFs2 to getPDFsByGroupNameAndJobNumber(groupName, jobNumber2)
           PDFCombineFiles(matchingPDFs2, my outputFile2)
           
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias
           SignOffSecuritySaveCloseSingleItem(proofPDF)
           
       else if job1Items is greater than 1 and job2Items is greater than 1 then
           WatermarkGroupFolderClose(pdfList, SaveGroupFolder)
           
           set matchingPDFs1 to getPDFsByGroupNameAndJobNumber(groupName, jobNumber1)
           PDFCombineFiles(matchingPDFs1, my outputFile1)
           
           set matchingPDFs2 to getPDFsByGroupNameAndJobNumber(groupName, jobNumber2)
           PDFCombineFiles(matchingPDFs2, my outputFile2)
           
           tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias list
           SignOffSecuritySaveCloseMultiItem(proofPDF)
           
       end if
   else if (count matchingPDFs1) is greater than 0 and (count matchingPDFs2) is greater than 0 then --pdfs for both in PrintersPlanPDFs
       WatermarkGroupFolderClose(pdfList, SaveGroupFolder)
       
       set matchingPDFs1 to getPDFsByGroupNameAndJobNumber(groupName, jobNumber1)
       PDFCombineFiles(matchingPDFs1, my outputFile1)
       
       set matchingPDFs2 to getPDFsByGroupNameAndJobNumber(groupName, jobNumber2)
       PDFCombineFiles(matchingPDFs2, my outputFile2)
       
       tell application "Finder" to set proofPDF to (files of folder "AddSignOff" of folder "Proofing" of desktop) as alias list
       SignOffSecuritySaveCloseMultiItem(proofPDF)
   end if
end if

tell application "System Events"
   delete (files of folder deleteWM)
   delete (files of folder deleteASF)
   delete (files of folder deleteWMNSO)
end tell


--functions/handlers
on findAndReplaceInText(theText, theSearchString, theReplacementString)
   set AppleScript's text item delimiters to theSearchString
   set theTextItems to text items of theText
   set AppleScript's text item delimiters to theReplacementString
   set theText to theTextItems as string
   set AppleScript's text item delimiters to ""
   return theText # returns a string. It will be useless to coerce as string later
end findAndReplaceInText

on breaktext(theText, theDelimiter)
   set AppleScript's text item delimiters to theDelimiter
   set theTextItems to text items of theText
   set AppleScript's text item delimiters to ""
   return theTextItems
end breaktext

on PDFCombineFiles(pFileList, pOutputFile)
   do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/join.py " & "-o " & quoted form of POSIX path of pOutputFile & space & quotedParameterStringFromPosixPaths(pFileList)
end PDFCombineFiles

on getPDFsByGroupNameAndJobNumber(groupName, jobNumber)
   return paragraphs of (do shell script "find \"/Volumes/bipserver/PrintersPlan_PDF_Files/" & groupName & "\" -name \"" & jobNumber & "*.pdf\"")
end getPDFsByGroupNameAndJobNumber

on quotedParameterStringFromPosixPaths(lst)
   repeat with i from 1 to count lst
       set item i of lst to quoted form of item i of lst
   end repeat
   return join(lst, space)
end quotedParameterStringFromPosixPaths

on join(textItems, separator)
   tell AppleScript
       set oldTIDs to text item delimiters
       set text item delimiters to separator
       set theList to textItems as string
       set text item delimiters to oldTIDs
   end tell
   return theList
end join

-- find how many items each job number has
on count_matches(this_list, this_item)
   set the match_counter to 0
   repeat with anItem in this_list
       if contents of anItem is this_item then ¬
           set match_counter to match_counter + 1
   end repeat
   return the match_counter
end count_matches

on WatermarkGroupFolderClose(pdfList, SaveGroupFolder)
   tell application "Adobe Acrobat"
       repeat with listItem in pdfList
           open listItem
           do script my WaterMark
           do script SaveGroupFolder
           do script my ClosePDF
       end repeat
   end tell
end WatermarkGroupFolderClose

on SignOffSecuritySaveCloseSingleItem(proofPDF)
   tell application "Adobe Acrobat"
       open proofPDF
       do script my SignOffSheet
       do script my PDF_Security
       do script my SaveAs
       do script my ClosePDF
   end tell
end SignOffSecuritySaveCloseSingleItem

on SignOffSecuritySaveCloseMultiItem(proofPDF)
   tell application "Adobe Acrobat"
       repeat with listItem in proofPDF
           open listItem
           do script my SignOffSheet
           do script my PDF_Security
           do script my SaveAs
           do script my ClosePDF
       end repeat
   end tell
end SignOffSecuritySaveCloseMultiItem

on SignOffSecuritySaveAsJobNumberClose(matchingPDFs, SaveAsJobNumber1)
   tell application "Adobe Acrobat"
       open matchingPDFs
       do script my SignOffSheet
       do script my PDF_Security
       do script SaveAsJobNumber1
       do script my ClosePDF
   end tell
end SignOffSecuritySaveAsJobNumberClose

on WatermarkGroupFolderSignOffSecuritySaveAsClosePDF(pdfList, SaveGroupFolder, SaveAsJobNumber1)
   tell application "Adobe Acrobat"
       open pdfList
       do script my WaterMark
       do script SaveGroupFolder
       do script my SignOffSheet
       do script my PDF_Security
       do script SaveAsJobNumber1
       do script my ClosePDF
   end tell
end WatermarkGroupFolderSignOffSecuritySaveAsClosePDF

on WatermarkGroupFolderCloseSingleItem(pdfList, SaveGroupFolder)
   tell application "Adobe Acrobat"
       open pdfList
       do script my WaterMark
       do script SaveGroupFolder
       do script my ClosePDF
   end tell
end WatermarkGroupFolderCloseSingleItem

on jobFilesList(nextItem, pdfList, jobItems)
   set jobFiles to {}
   try
       repeat count pdfList times
           if nextItem is less than or equal to jobItems then
               set end of jobFiles to (item nextItem of pdfList)
               set nextItem to (nextItem + 1)
           end if
       end repeat
   end try
   return jobFiles
end jobFilesList

Last edited by Prepress@BIP (2017-06-15 03:22:57 pm)

Offline

 

#16 2017-06-16 05:38:17 am

DJ Bazzie Wazzie
Member
From: the Netherlands
Registered: 2004-10-20
Posts: 2654

Re: Help in cleaning up script

cool looks much better!

Offline

 

Board footer

Powered by FluxBB

[ Generated in 3.791 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)