Saturday, March 28, 2020

#1 2020-03-25 06:25:21 am

GOAviator
Member
Registered: 2019-08-29
Posts: 4

Append same pdf to multiple pdfs

Hi I write also here because same post is in Automator but this section I think is better.
I found on this forum this script but I need a modification. This append a single-page pdf to a list of pdfs dragged in the script. I need the same but appending a multi-page pdf to each of the pdf. Can you help me? I tried alone but I am beginner.
Thanks

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "Quartz"

on open fileList -- drag list of PDFs to be updated
   set masterFile to POSIX path of (choose file with prompt "Choose the master file:" of type {"pdf"})
   set listOfPosixPaths to {}
   repeat with aFile in fileList
       set end of listOfPosixPaths to POSIX path of aFile
   end repeat
   my insertPageOneFrom:masterFile intoDocuments:listOfPosixPaths
   display dialog "Finished" buttons {"OK"}
end open

on insertPageOneFrom:posixPath intoDocuments:listOfPosixPaths
   set theURL to current application's class "NSURL"'s fileURLWithPath:posixPath
   set thePDFDoc to current application's PDFDocument's alloc()'s initWithURL:theURL
   set thePage to thePDFDoc's pageAtIndex:0 -- zero-based
   repeat with aPath in listOfPosixPaths
       set thisURL to (current application's class "NSURL"'s fileURLWithPath:aPath)
       set oldName to (thisURL's lastPathComponent()'s stringByDeletingPathExtension()'s stringByAppendingString:"+")
       set newURL to ((thisURL's URLByDeletingLastPathComponent()'s URLByAppendingPathComponent:oldName)'s URLByAppendingPathExtension:"pdf")
       set thisPDFDoc to (current application's PDFDocument's alloc()'s initWithURL:thisURL)
       (thisPDFDoc's insertPage:thePage atIndex:0)
       (thisPDFDoc's writeToURL:newURL)
   end repeat
end insertPageOneFrom:intoDocuments:

Offline

 

#2 2020-03-25 07:27:43 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1015

Re: Append same pdf to multiple pdfs

I think, this should perform your task:

Applescript:


use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "Quartz"

on run
   set fileList to (choose file of type "pdf" with prompt "Choose PDF files to append the master PDF to them:" with multiple selections allowed)
   open fileList
end run

on open fileList -- drag list of PDFs to be updated
   set masterFile to POSIX path of (choose file with prompt "Choose the master PDF:" of type {"pdf"})
   set listOfPosixPaths to {}
   repeat with aFile in fileList
       set end of listOfPosixPaths to POSIX path of aFile
   end repeat
   my insertPDFFrom:masterFile intoDocuments:listOfPosixPaths
   display dialog "Finished" buttons {"OK"}
end open

on insertPDFFrom:posixPath intoDocuments:listOfPosixPaths
   
   set masterURL to current application's class "NSURL"'s fileURLWithPath:posixPath
   set masterPDFDoc to current application's PDFDocument's alloc()'s initWithURL:masterURL
   set masterDocCount to masterPDFDoc's pageCount() -- zero-based
   
   repeat with aPath in listOfPosixPaths
       set thisURL to (current application's class "NSURL"'s fileURLWithPath:aPath)
       set thisPDFDoc to (current application's PDFDocument's alloc()'s initWithURL:thisURL)
       set thisDocCount to thisPDFDoc's pageCount()
       set oldName to (thisURL's lastPathComponent()'s stringByDeletingPathExtension()'s stringByAppendingString:"+")
       set newURL to ((thisURL's URLByDeletingLastPathComponent()'s URLByAppendingPathComponent:oldName)'s URLByAppendingPathExtension:"pdf")
       
       repeat with i from 1 to masterDocCount
           set thePDFPage to (masterPDFDoc's pageAtIndex:(i - 1)) -- zero-based indexes
           (thisPDFDoc's insertPage:thePDFPage atIndex:thisDocCount)
           set thisDocCount to thisDocCount + 1
       end repeat
       
       (thisPDFDoc's writeToURL:newURL)
   end repeat
   
end insertPDFFrom:intoDocuments:

Last edited by KniazidisR (2020-03-25 07:36:37 am)


Model: MacBook Pro
macOS Mojave -- version 10.14.4, Ram 4 GB
Safari -- version 12.1
Firefox -- version 70.0

Online

 

#3 2020-03-25 09:04:43 am

GOAviator
Member
Registered: 2019-08-29
Posts: 4

Re: Append same pdf to multiple pdfs

OMG is perfect Thank you!!!
Maybe you can tell how to modify that new generated pdf files are saved in a new folder called "new"?

thanks anyway

Offline

 

#4 2020-03-25 01:48:55 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 1015

Re: Append same pdf to multiple pdfs

GOAviator wrote:

Maybe you can tell how to modify that new generated pdf files are saved in a new folder called "new"?


Other version - with New folder creating:

Applescript:


use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "Quartz"

on run
   set fileList to (choose file of type "pdf" with prompt "Choose PDF files to append the master PDF to them:" with multiple selections allowed)
   open fileList
end run

on open fileList -- drag list of PDFs to be updated
   
   tell application "Finder"
       set aContainerPath to (container of (item 1 of fileList)) as text
       set newFolderPath to aContainerPath & "New"
       if not (folder newFolderPath exists) then make new folder at folder aContainerPath with properties {name:"New"}
   end tell
   
   set masterFile to POSIX path of (choose file with prompt "Choose the master PDF:" of type {"pdf"})
   set listOfPosixPaths to {}
   repeat with aFile in fileList
       set end of listOfPosixPaths to POSIX path of aFile
   end repeat
   
   my insertPDFFrom:masterFile intoDocuments:listOfPosixPaths
   display dialog "Finished" buttons {"OK"}
end open

on insertPDFFrom:posixPath intoDocuments:listOfPosixPaths
   
   set masterURL to current application's class "NSURL"'s fileURLWithPath:posixPath
   set masterPDFDoc to current application's PDFDocument's alloc()'s initWithURL:masterURL
   set masterDocCount to masterPDFDoc's pageCount() -- zero-based
   
   repeat with aPath in listOfPosixPaths
       set thisURL to (current application's class "NSURL"'s fileURLWithPath:aPath)
       set thisPDFDoc to (current application's PDFDocument's alloc()'s initWithURL:thisURL)
       set thisDocCount to thisPDFDoc's pageCount()
       set oldName to thisURL's lastPathComponent()'s stringByDeletingPathExtension()
       set newURL to (((thisURL's URLByDeletingLastPathComponent()'s URLByAppendingPathComponent:"/New")'s URLByAppendingPathComponent:oldName)'s URLByAppendingPathExtension:"pdf")
       
       repeat with i from 1 to masterDocCount
           set thePDFPage to (masterPDFDoc's pageAtIndex:(i - 1)) -- zero-based indexes
           (thisPDFDoc's insertPage:thePDFPage atIndex:thisDocCount)
           set thisDocCount to thisDocCount + 1
       end repeat
       
       (thisPDFDoc's writeToURL:newURL)
   end repeat
   
end insertPDFFrom:intoDocuments:

Last edited by KniazidisR (2020-03-25 01:54:19 pm)


Model: MacBook Pro
macOS Mojave -- version 10.14.4, Ram 4 GB
Safari -- version 12.1
Firefox -- version 70.0

Online

 

#5 2020-03-26 03:05:14 am

GOAviator
Member
Registered: 2019-08-29
Posts: 4

Re: Append same pdf to multiple pdfs

works great! than you! Really appreciated! wink

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)