指定のPDFからTOCを削除して別名保存するAppleScriptです。
TOCを削除した新規ファイルは元ファイルと同じ場所に枝番つきで重複回避しつつ作成されます。
33MBで257ページほどあるPDFで、詳細に目次から記事へのリンクを手動で作成し、TOCを手動で作ったところ、Adobe Actobatが、
などというメッセージを出して保存ができなかったので、中途半端に1個だけエントリが残ったTOCを削除するために用意したものです(既存のScriptに機能があったので、削除機能だけ抽出)。
AppleScript名:指定のPDFからTOCを削除する.scptd |
— – Created by: Takaaki Naganoya – Created on: 2018/08/18 — – Copyright © 2018 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "Quartz" property |NSURL| : a reference to current application’s |NSURL| property NSString : a reference to current application’s NSString property PDFDocument : a reference to current application’s PDFDocument property NSFileManager : a reference to current application’s NSFileManager –PDFの指定 set aFile to choose file of type {"pdf"} with prompt (progress additional description) set filePath to aFile’s POSIX path set fileURL to |NSURL|’s fileURLWithPath:filePath set aPDFdoc to PDFDocument’s alloc()’s initWithURL:fileURL set parentOL to aPDFdoc’s outlineRoot() if parentOL is equal to missing value then display dialog "There is no TOC with this PDF" return end if set outLineCount to parentOL’s numberOfChildren() set outLineCount to outLineCount as number –既存のTOCを削除 repeat with num from (outLineCount – 1) to 0 by -1 (parentOL’s childAtIndex:num)’s removeFromParent() end repeat –保存 set progress description to "PDFを保存" set progress additional description to "保存先の確認" tell (NSString’s stringWithString:filePath) set parentPath to stringByDeletingLastPathComponent() –>親フォルダ set longFileName to lastPathComponent() –>拡張子ありの名前 end tell set newFilePath to my pathExists(parentPath, longFileName) set progress additional description to "書き出し中: " & newFilePath set newFileURL to |NSURL|’s fileURLWithPath:newFilePath aPDFdoc’s writeToURL:newFileURL –名前が重複していないか確認 on pathExists(currentPath as text, fileName as text) tell (NSString’s stringWithString:fileName) set shortFileName to stringByDeletingPathExtension() as text set aSuffix to pathExtension() as text end tell if aSuffix ≠ "" then set aSuffix to "." & aSuffix as text set currentPath to NSString’s stringWithString:currentPath set unkonwnPath to (currentPath’s stringByAppendingPathComponent:fileName) set aSpace to space set num to 0 repeat while ((NSFileManager’s defaultManager)’s fileExistsAtPath:unkonwnPath) set num to num + 1 set tmpName to shortFileName & aSpace & num & aSuffix as text set unkonwnPath to (currentPath’s stringByAppendingPathComponent:tmpName) end repeat return (unkonwnPath as text) end pathExists |
More from my site
(Visited 125 times, 1 visits today)