If it’s really what you want to achieve, as KniazidisR wrote, there are several examples of scripts spitting PDFs or concatenating pages on the net and particularily in this forum.
Here are three of them based upon Shane Stanley’s codes.
(* Split a PDF in pages *)
use AppleScript version "2.3.1"
use scripting additions
use framework "Foundation"
use framework "Quartz" -- required for PDF stuff
#===== Handlers
-- Supposed to create a new PDF file for every page from the passed PDF fine.
on splitPDF:thePath
set inNSURL to current application's |NSURL|'s fileURLWithPath:thePath
set thePDFDocument to current application's PDFDocument's alloc()'s initWithURL:inNSURL
# CAUTION. theList contain indexes of pages numbered starting from 1, but ASObjC number them starting from 0
set theCount to thePDFDocument's pageCount() as integer
repeat with i from 1 to theCount
set newPath to (its addString:("-page " & text -2 thru -1 of ((100 + i) as text)) beforeExtensionIn:thePath)
set outNSURL to (current application's |NSURL|'s fileURLWithPath:newPath)
set thePDFPage to (thePDFDocument's pageAtIndex:(i - 1)) # ?????
set newPDFDoc to current application's PDFDocument's alloc()'s init()
(newPDFDoc's insertPage:thePDFPage atIndex:0)
(newPDFDoc's writeToURL:outNSURL)
end repeat
end splitPDF:
-- inserts a string in a path before the extension
on addString:extraString beforeExtensionIn:aPath
set pathNSString to current application's NSString's stringWithString:aPath
set newNSString to current application's NSString's stringWithFormat_("%@%@.%@", pathNSString's stringByDeletingPathExtension(), extraString, pathNSString's pathExtension())
return newNSString as text
end addString:beforeExtensionIn:
#===== Caller
set thePath to POSIX path of (choose file with prompt "Choose a PDF file." of type {"PDF"})
its splitPDF:thePath
(* Combine PDFs *)
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "Quartz"
on combineFiles:inFiles savingTo:destPosixPath
-- make URL of the first PDF
set inNSURL to current application's class "NSURL"'s fileURLWithPath:(POSIX path of item 1 of inFiles)
-- make PDF document from the URL
set theDoc to current application's PDFDocument's alloc()'s initWithURL:inNSURL
-- loop through the rest
set oldDocCount to theDoc's pageCount()
set inFiles to rest of inFiles
repeat with aFile in inFiles
-- make URL of the next PDF
set inNSURL to (current application's class "NSURL"'s fileURLWithPath:(POSIX path of aFile))
-- make PDF document from the URL
set newDoc to (current application's PDFDocument's alloc()'s initWithURL:inNSURL)
-- loop through, moving pages
set newDocCount to newDoc's pageCount()
repeat with i from 1 to newDocCount
-- get page of old PDF
set thePDFPage to (newDoc's pageAtIndex:(i - 1)) -- zero-based indexes
-- insert the page
(theDoc's insertPage:thePDFPage atIndex:oldDocCount)
set oldDocCount to oldDocCount + 1
end repeat
end repeat
set outNSURL to current application's class "NSURL"'s fileURLWithPath:destPosixPath
-- save the new PDF
(theDoc's writeToURL:outNSURL)
end combineFiles:savingTo:
set inFiles to (choose file of type {"pdf"} with prompt "Choose your PDF files:" with multiple selections allowed)
set destPosixPath to POSIX path of (choose file name default name "Combined.pdf" with prompt "Save new PDF to:")
its combineFiles:inFiles savingTo:destPosixPath
(* Remove pages from PDF *)
use AppleScript version "2.3.1"
use scripting additions
use framework "Foundation"
use framework "Quartz" -- required for PDF stuff
#===== Handlers
-- Remove from a PDF file the pages whose index is in theList, saving as a new version
on removePagesInPDF:thePath outputTo:newPath pagesList:theList dropThem:flag
set inNSURL to current application's |NSURL|'s fileURLWithPath:thePath
set outNSURL to current application's |NSURL|'s fileURLWithPath:newPath
set thePDFDocument to current application's PDFDocument's alloc()'s initWithURL:inNSURL
if (thePDFDocument's isEncrypted()) as boolean then
thePDFDocument's writeToURL:outNSURL
set thePDFDocument to current application's PDFDocument's alloc()'s initWithURL:outNSURL
end if
# CAUTION. theList contain indexes of pages numbered starting from 1, but ASObjC number them starting from 0
set theCount to thePDFDocument's pageCount() as integer
# Remove pages starting from the PDF's end
if flag is true then
# drop the pages listed
repeat with i from theCount to 1 by -1
if i is in theList then (thePDFDocument's removePageAtIndex:(i - 1))
end repeat
else
# keep the pages listed
repeat with i from theCount to 1 by -1
if i is not in theList then (thePDFDocument's removePageAtIndex:(i - 1))
end repeat
end if
thePDFDocument's writeToURL:outNSURL
end removePagesInPDF:outputTo:pagesList:dropThem:
-- inserts a string in a path before the extension
on addString:extraString beforeExtensionIn:aPath
set pathNSString to current application's NSString's stringWithString:aPath
set newNSString to current application's NSString's stringWithFormat_("%@%@.%@", pathNSString's stringByDeletingPathExtension(), extraString, pathNSString's pathExtension())
return newNSString as text
end addString:beforeExtensionIn:
#===== Caller
--set thePath to POSIX path of ((path to desktop as text)&"IT01-RECUEIL-BAC.pdf")
set thePath to POSIX path of (choose file with prompt "Choose a PDF file." of type {"PDF"})
# If the number of pages to keep is small use this code
set newPath to its addString:"-stripped" beforeExtensionIn:thePath
its removePagesInPDF:thePath outputTo:newPath pagesList:{8, 9, 10, 11, 16, 17, 19, 20, 21, 25} dropThem:false
(*
# If the count of pages to drop is small, use this code
set newPath to its addString:"-strippedAlt" beforeExtensionIn:thePath
its removePagesInPDF:thePath outputTo:newPath pagesList:{1, 2} dropThem:true
*)
About GUI scripting, I added code to insert a blank page in the original PDF before pasting but at this time, the paste process paste a huge version of the PDF icon.
As I get the same behavior when I paste by hand, I assume that it’s linked to the fact that I run the demo version.
Here is the code:
(*
CAUTION, uses the free CLI named cliclick
*)
tell application "Finder" to activate
tell application "System Events"
tell process "Finder"
--The File in this test is already highlighted
keystroke "c" using command down
delay 3
end tell
end tell
tell application "PDFpenPro" to activate
tell application "System Events"
tell process "PDFpenPro"
set frontmost to true
keystroke "b" using {command down, option down}
tell window 1
class of UI elements --> {splitter group, button, button, button, list, list, button, menu button, menu button, radio group, toolbar, image, static text, sheet}
tell sheet 1
class of UI elements --> {static text, checkbox, checkbox, scroll area, button, button}
position of checkboxes --> {{287, 168}, {345, 168}}
-- checkbox 1 = Vertical, checkbox 2 = Horizontal
click checkbox 1 -- select Vertical
tell scroll area 1
class of UI elements --> {list, scroll bar}
tell list 1
class of UI elements --> {list}
tell list 1
class of UI elements --> {group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group, group}
set gr to 4 -- why not ?
tell group gr
--class of UI elements --> {image}
set {{x, y}, {w, h}} to {position, size}
end tell
tell me to do shell script "/usr/local/bin/cliclick c:" & x + w div 2 & "," & y + h div 2
end tell -- list 1
end tell -- list 1
end tell -- scroll area 1
name of buttons --> {"Choisir", "Annuler"}
click button 1 -- insert a new page at cursor location
end tell -- sheet 1
keystroke "v" using command down
end tell -- window 1
end tell
end tell
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 4 mai 2020 23:21:39