使用脚本将多页 tiff 转换为 pdf

Multipage tiff to pdf with a script

我正在尝试制作一个多页 tiff 文件的 pdf。我们在较新版本的 macOS 上尝试了 this solution,它成功了。其他人可以帮我微调吗?

转换后,我想将两个单独的 pdf 重新加入到一个文件中,并且我想 运行 当一个文件被添加到某个文件夹时自动执行它。

在您的 Mac 上创建常用文件夹。将以下脚本附加到它。该脚本将立即将 1 个多页 TIFF 转换为 1 个多页 PDF:

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

property |NSURL| : a reference to current application's |NSURL|
property NSString : a reference to current application's NSString
property PDFPage : a reference to current application's PDFPage
property NSImage : a reference to current application's NSImage
property PDFDocument : a reference to current application's PDFDocument
property NSBitmapImageRep : a reference to current application's NSBitmapImageRep

on adding folder items to this_folder after receiving these_items
    set aRes to convertMultiPageTiffToPDF(item 1 of these_items) of me
end adding folder items to


on convertMultiPageTiffToPDF(anAlias)
    --Make Output Path
    set b to POSIX path of anAlias
    set bb to changeExtensionInPath("pdf", b) --OutPath
    
    --Read Multi-Page TIFF
    set aURL to |NSURL|'s fileURLWithPath:b
    set aImage to NSImage's alloc()'s initWithContentsOfURL:aURL
    set aRawimg to aImage's TIFFRepresentation()
    set eachTiffPages to (NSBitmapImageRep's imageRepsWithData:aRawimg) as list
    
    --Make Blank PDF
    set aPDFdoc to PDFDocument's alloc()'s init()
    
    set pageNum to 0
    
    repeat with curPage in eachTiffPages
        set thisImage to contents of curPage
        set aImg to (NSImage's alloc()'s initWithSize:(thisImage's |size|()))
        (aImg's addRepresentation:thisImage)
        (aPDFdoc's insertPage:(PDFPage's alloc()'s initWithImage:aImg) atIndex:pageNum)
        set pageNum to pageNum + 1
    end repeat
    
    return (aPDFdoc's writeToFile:bb) as boolean
end convertMultiPageTiffToPDF


on changeExtensionInPath(extStr as string, aPath as string)
    set pathString to NSString's stringWithString:aPath
    set theExtension to pathString's pathExtension()
    set thePathNoExt to pathString's stringByDeletingPathExtension()
    set newPath to thePathNoExt's stringByAppendingPathExtension:extStr
    return newPath as string
end changeExtensionInPath