【问题标题】:applescript to split multipage tiff用于拆分多页 tiff 的 applescript
【发布时间】:2021-08-28 13:23:10
【问题描述】:

我正在尝试制作多页 tiff 文件的 pdf。 我设法在带有图像魔法的窗口中做到了这一点。 在一个项目中,我们正在使用带有 OSX 的 macbook(对我来说是新手)。 而且应该有可能。

是否有脚本/自动机...可以做到这一点。 现在我在预览中手动拆分多页 tiff (2pages):打开 tiff,将每个页面拖到一个文件夹中,然后我运行一个脚本来制作一个 pdf。

这可以在脚本中完成吗?

【问题讨论】:

  • “我设法在带有图像魔法的窗口中做到了这一点。”那么为什么不再使用图像魔法呢?为什么这是一个 AppleScript 问题?
  • 我不是管理员,管理员不喜欢做很多系统改动

标签: macos applescript automator


【解决方案1】:

我向每一位尝试在可能的情况下不使用第三方应用程序的用户致意。 OP 提出的问题比本节中出现的许多琐碎问题更有趣。此外,我不明白两位用户对所提问题的负面看法。这是解决方案:

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

-- select TIFF
set anAlias to choose file of type {"public.tiff"} with prompt "Select Multi-page tiff file"

-- get selected TIFF's  name and base name
tell application "Finder"
    set pathString to NSString's stringWithString:(name of anAlias)
    set baseName to (pathString's stringByDeletingPathExtension()) as text
end tell

-- make new destination folder (if it doesn't exist already)
tell application "Finder"
    try
        set destinationFolder to (make new folder at desktop with properties {name:baseName}) as text
    on error
        set destinationFolder to "" & (path to desktop folder) & baseName
    end try
end tell
set destinationFolder to POSIX path of destinationFolder

--Read Multi-Page TIFF
set aURL to |NSURL|'s fileURLWithPath:(POSIX path of anAlias)
set aImage to NSImage's alloc()'s initWithContentsOfURL:aURL
set aRawimg to aImage's TIFFRepresentation()
set eachTiffPages to (NSBitmapImageRep's imageRepsWithData:aRawimg) as list

-- extract each tiff page as PDF file
set pageNum to 1
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)
    --Make Blank PDF
    set aPDFdoc to PDFDocument's alloc()'s init()
    -- set PDF first page's content to next image
    (aPDFdoc's insertPage:(PDFPage's alloc()'s initWithImage:aImg) atIndex:0)
    -- write PDF to destination
    set outPutPath to destinationFolder & "/Page_" & pageNum & ".pdf"
    (aPDFdoc's writeToFile:outPutPath)
    set pageNum to pageNum + 1
end repeat

【讨论】:

  • 这就是精神。我同意。
  • 但就像我之前所说的,我对 Apple 和 Scripting 完全陌生。那么我该如何使用这个解决方案,因为当我将整个代码复制到脚本编辑器时,什么也没有发生。请从A到Z帮助我?我在 Macbook 上安装了 Mac OSX 版本 10.7.5。
  • 我想我有 Applescript 2.2 版本
  • 在我能够运行脚本的同时。我删除了使用...,使用...开始。我还需要多次替换“:”,并且预期的代码有时会结束而不是......规则错误:告诉应用程序“Finder”将pathString设置为NSString的stringWithString
  • 我想发布我的代码,但它太长了。如何做到这一点
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-10
  • 1970-01-01
  • 2021-09-01
  • 2011-04-04
  • 1970-01-01
相关资源
最近更新 更多