First point, you may use the script posted to split with your “wrongly” ordered files.
Rename the “main file” inserting at its very beginning.
I edited the script so that it will do the job in original case as well as with this new one.
Second point, probably the most important.
Which operating system are you running ?
use AppleScript version “2.4” means that the script requires 10.10 or later OS.
I guess that you are running an old operating system.
If it’s older than 10.9, no hope to use the script.
If it’s 10.9.x you may take benefit of ASObjC features but the dedicated code can’t be in a standard script, it must be in a library package.
This requires some changes to the script.
Part 1
use AppleScript version "2.3.1"
use scripting additions
use framework "Foundation"
use framework "Quartz" -- required for PDF stuff
#=====
on findPattern:thePattern inString:theString
set theNSString to current application's NSString's stringWithString:theString
set theOptions to ((current application's NSRegularExpressionDotMatchesLineSeparators) as integer) + ((current application's NSRegularExpressionAnchorsMatchLines) as integer)
set theRegEx to current application's NSRegularExpression's regularExpressionWithPattern:thePattern options:theOptions |error|:(missing value)
set theFinds to theRegEx's matchesInString:theNSString options:0 range:{location:0, |length|:theNSString's |length|()}
set theResult to {} -- we will add to this
repeat with i from 1 to count of theFinds
set theRange to (item i of theFinds)'s range()
set end of theResult to (theNSString's substringWithRange:theRange) as string
end repeat
return theResult
end findPattern:inString:
#=====
on splitPDF:thePath numberingFrom:theNum basePath:strippedPath
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:("_" & text -2 thru -1 of ((100 + theNum) as text)) beforeExtensionIn:strippedPath)
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)
set theNum to theNum + 1
end repeat
end splitPDF:numberingFrom:basePath:
-- 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:
#=====
on replace:sourceString existingString:d1 newString:d2
set sourceString to current application's NSString's stringWithString:sourceString
return (sourceString's stringByReplacingOccurrencesOfString:d1 withString:d2) as text
end replace:existingString:newString:
#=====
on getName:POSIXPath
local PosixName
set theURL to current application's |NSURL|'s fileURLWithPath:POSIXPath
return theURL's lastPathComponent() as text
end getName:
#=====
on split:sourceString usingString:d1
set sourceString to current application's NSString's stringWithString:sourceString
return (sourceString's componentsSeparatedByString:d1) as list
end split:usingString:
As you may see, I dropped the calling instructions and added two handlers.
Save this code as a script bundle (.scptd file) which you will store in ~/Library/Script Libraries/
I named it : “4macScripter.scptd”
You will probably have to create the subdirectory Script Libraries by yourself.
Open the bundle in the Script Editor.
At the right-top of its window you will get a button entitled “Bundle Contents”.
Click it to reveal the dedicated drawer.
In it you will see a checkbox entitled “AppleScript/Objective-C Library”
Check it, and re-save the file.
Now you will have an usable library.
Part 2
Contains quite only the calling instructions.
use theLib : script "4macScripter"
use scripting additions
set thePath to POSIX path of (choose file with prompt "Choose a PDF file." of type {"PDF"})
set itsName to theLib's getName:thePath
set itsFirstNumStr to item 1 of (theLib's findPattern:"_[0-9][0-9]_" inString:itsName)
set itsFirstNum to (theLib's replace:itsFirstNumStr existingString:"_" newString:"") as integer
set strippedPath to (item 1 of (theLib's split:thePath usingString:itsFirstNumStr)) & ".pdf"
theLib's splitPDF:thePath numberingFrom:itsFirstNum basePath:strippedPath
The very first instruction will inform the script that it’s urged to use features defined in the named bundle.
The second one is required because calling libraries disable scripting additions so we must re-enable them as we are using a feature of StandardAdditions : choose file.
As you may see, all the prefixes (my or its) which were used to call handlers are replaced by the prefix “theLib’s” which tell the script that the called handler belongs to theLib (aka 4macScripter)
Run the caller script.
It will ask you to choose a PDF to split then it will call the handlers : getName, findPattern, replace, split, splitPDF of our library to do the job.
Of course we weren’t forced to use ASObjC for everything here.
It would be easy to replace the handlers getName, replace and split by old fashioned pieces of code but the ObjC version is faster than them.
If you use 10.7 or 10.8, you may perhaps do the job with Shane STANLEY’s ASObjC Runner which was available from https://www.macosxautomation.com/applescript/apps/runner.html.
Alas, the important word in my late sentence is “was” because, as ASObjC Runner can’t be used with Sierra (10.12), it’s deprecated and I don’t know if there is a living link allowing to get it.
Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) mercredi 7 décembre 2016 16:03:00