Thursday, December 14, 2017

#1 2017-08-14 05:46:21 am

Joy
Member
From:: South Tirol
Registered: 2008-07-04
Posts: 539
Website

Convert pictures to Pdf

The script "convert to pdf" shipped by Apple is outdated and i have no backup of the "convert" shell involved in that script doing the conversion.

I hate to download for little things apps on the App store, somebody knows a workaround or shell binary to share to merge pictures in a pdf?
Without Automator please as i have to integrate the code it in a *.scpt file

Offline

 

#2 2017-08-14 06:41:38 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5199

Re: Convert pictures to Pdf

Try this:

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use framework "Foundation"
use framework "AppKit" -- for NSImage
use framework "Quartz" -- required for PDF stuff

set inFiles to (choose file of type {"public.image", "com.adobe.pdf"} with prompt "Choose your files:" with multiple selections allowed)
set destPosixPath to POSIX path of (choose file name default name "Combined.pdf" with prompt "Save new PDF to:")
my combineFiles:inFiles savingToPDF:destPosixPath

on combineFiles:inFiles savingToPDF:destPosixPath
   -- make URL of the first file
   set inNSURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of item 1 of inFiles)
   -- make PDF document from the URL
   if (inNSURL's pathExtension()'s isEqualToString:"pdf") as boolean then
       set theDoc to current application's PDFDocument's alloc()'s initWithURL:inNSURL
   else
       set theDoc to my pdfDocFromImageURL:inNSURL
   end if
   -- 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 |NSURL|'s fileURLWithPath:(POSIX path of aFile))
       -- make PDF document from the URL
       if (inNSURL's pathExtension()'s isEqualToString:"pdf") as boolean then
           set newDoc to (current application's PDFDocument's alloc()'s initWithURL:inNSURL)
       else
           set newDoc to (my pdfDocFromImageURL:inNSURL)
       end if
       -- loop through, moving pages
       set newDocCount to newDoc's pageCount()
       repeat with i from 1 to newDocCount
           -- get page of PDF
           set thePDFPage to (newDoc's pageAtIndex:(i - 1)) -- zero-based indexes
           -- insert the page into main PDF
           (theDoc's insertPage:thePDFPage atIndex:oldDocCount)
           set oldDocCount to oldDocCount + 1
       end repeat
   end repeat
   set outNSURL to current application's |NSURL|'s fileURLWithPath:destPosixPath
   -- save the main PDF
   (theDoc's writeToURL:outNSURL)
end combineFiles:savingToPDF:

on pdfDocFromImageURL:inNSURL
   set theImage to current application's NSImage's alloc()'s initWithContentsOfURL:inNSURL
   set theSize to theImage's |size|()
   set theRect to {{0, 0}, theSize}
   set theImageView to current application's NSImageView's alloc()'s initWithFrame:theRect
   theImageView's setImage:theImage
   set theData to theImageView's dataWithPDFInsideRect:theRect
   return current application's PDFDocument's alloc()'s initWithData:theData
end pdfDocFromImageURL:


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#3 2017-08-14 03:15:32 pm

Joy
Member
From:: South Tirol
Registered: 2008-07-04
Posts: 539
Website

Re: Convert pictures to Pdf

Works like a charm!
ApplescriptOBJC is always particular cool

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)