• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Industry Colleague is Looking for a Script
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Industry Colleague is Looking for a Script


  • Subject: Re: Industry Colleague is Looking for a Script
  • From: Yvan KOENIG <email@hidden>
  • Date: Wed, 16 Nov 2016 16:28:20 +0100

If the PDF is a paginated one you may use :

(* splitInSeveralPDFs.scpt *)
# Most of this code was borrowed from Shane Stanley.


use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "Quartz" -- required for PDF stuff

#=====#=====#=====#=====#=====#=====

on splitInSeveralPDFs:thePath nbFiles:cnt
set inNSURL to current application's |NSURL|'s fileURLWithPath:thePath
set theDoc to current application's PDFDocument's alloc()'s initWithURL:inNSURL
set theCount to theDoc's pageCount() as integer
set nbPages to theCount div cnt (*theMiddle = 0*)
set beg to 0
set thePaths to {}
repeat with knt from 1 to cnt
set newPath1 to (its addString:("-part" & knt) beforeExtensionIn:thePath)
set end of thePaths to newPath1 as text
set outNSURL1 to (current application's |NSURL|'s fileURLWithPath:newPath1)
set newPDFDoc1 to current application's PDFDocument's alloc()'s init()
if knt = cnt then
set lastPage to theCount - 1
else
set lastPage to beg + nbPages - 1
end if
set newPage to 0
repeat with i from beg to lastPage
set thePDFPage to (theDoc's pageAtIndex:i) -- zero-based indexes
(newPDFDoc1's insertPage:thePDFPage atIndex:newPage)
set newPage to newPage + 1
end repeat
(newPDFDoc1's writeToURL:outNSURL1)
set beg to beg + nbPages
end repeat
(*
# Efface le PDF initial
set theFileManager to current application's NSFileManager's |defaultManager|()
theFileManager's removeItemAtURL:inNSURL |error|:(missing value)
*)
return thePaths


end splitInSeveralPDFs:nbFiles:

-- 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 returnSizeFor:POSIXPath # get the size
set aURL to current application's |NSURL|'s fileURLWithPath:POSIXPath -- make URL
set {theResult, theSize} to aURL's getResourceValue:(reference) forKey:(current application's NSURLFileSizeKey) |error|:(missing value)
if theSize = missing value then return {} -- because when there are none, it returns missing value
return theSize as list
end returnSizeFor:

#=====#=====#=====#=====#=====#=====

on countPages:POSIXPath
local inNSURL, theDoc
set inNSURL to current application's class "NSURL"'s fileURLWithPath:POSIXPath
-- make PDF document from the URL
set theDoc to current application's PDFDocument's alloc()'s initWithURL:inNSURL
return theDoc's pageCount()
end countPages:

#=====#=====#=====#=====#=====#=====

set originalPosixPath to POSIX path of (choose file of type {"pdf"})

set itsSize to item 1 of (my returnSizeFor:originalPosixPath)
set itsPagesCount to my countPages:originalPosixPath
set sizeAllowed to 2000000 # Adjust to fit your needs.
set cntFiles to round (itsSize / sizeAllowed) rounding up
set thePaths to my splitInSeveralPDFs:originalPosixPath nbFiles:cntFiles
--> List of posix paths of the separated files.

I just wrote it this morning to fit my own needs ;-)


Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) mercredi 16 novembre 2016 16:27:29


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

References: 
 >Industry Colleague is Looking for a Script (From: "Jacob M. Small" <email@hidden>)

  • Prev by Date: Re: Industry Colleague is Looking for a Script
  • Next by Date: Sal
  • Previous by thread: Re: Industry Colleague is Looking for a Script
  • Next by thread: Sal
  • Index(es):
    • Date
    • Thread