Keynoteでオープン中の書類すべてをPDF書き出しして、各PDFの最初のページをJPEG画像に書き出すAppleScriptです。書き出し先はデスクトップフォルダです。Keynote v12.1+macOS 12.6.1で動作確認しています。
それぞれ、PDF書き出しするScriptと、PDFをJPEGに変換するAppleScriptは個別に書いて使っていたのですが、処理数が増えるといちいち複数のScriptをかわるがわる実行するのも面倒に。
そこで、PDF書き出し→JPEG変換を行うものを作ってみました。作ったみたといっても、部品はすでに存在していたので繋ぎ合わせただけです。
AppleScript名:すべてデスクトップ上にPDF出力し、最初のページをJPEG出力.scptd |
— – Created by: Takaaki Naganoya – Created on: 2022/10/14 — – Copyright © 2022 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use scripting additions property NSString : a reference to current application’s NSString property NSFileManager : a reference to current application’s NSFileManager property NSOrderedSame : a reference to current application’s NSOrderedSame set outPathList to {} tell application "Keynote" set allDocs to every document if allDocs = {} then return repeat with i in allDocs set j to contents of i set dRes to exportKeynoteDocToPDF(j) of me set pdfPath to (POSIX path of dRes) set pdfPath to chkExistPOSIXpathAndIncrementChildNumberFull(pdfPath) of me set jRes to (my jpegFromPath:(pdfPath) compressFactor:1.0) set the end of outPathList to jRes end repeat end tell return outPathList –Keynote書類からPDF書き出し on exportKeynoteDocToPDF(targKeynoteDoc) tell application "Keynote" set dCount to count every document if dCount = 0 then –オープン中のKeynote書類がない場合はリターン return false end if set aPath to file of targKeynoteDoc end tell set tmpPath to (path to desktop) as string set curPath to (NSString’s stringWithString:(POSIX path of aPath))’s lastPathComponent()’s stringByDeletingPathExtension()’s stringByAppendingString:".pdf" set outPath to (tmpPath & curPath) set outPath to chkExistPOSIXpathAndIncrementChildNumberFull(outPath) of me –do shell script "touch " & quoted form of POSIX path of outPath –Error 6を回避するための記述 tell application "Keynote" –set anOpt to {class:export options, export style:IndividualSlides, all stages:false, skipped slides:true, PDF image quality:Good} –set anOpt to {class:export options, export style:IndividualSlides, all stages:false, skipped slides:true, PDF image quality:Better} set anOpt to {class:export options, export style:IndividualSlides, all stages:false, skipped slides:true, PDF image quality:Best} export targKeynoteDoc to POSIX file outPath as PDF with properties anOpt end tell return ((POSIX file outPath) as alias) end exportKeynoteDocToPDF on jpegFromPath:imagePath compressFactor:compFactor — 0.0 = max compression, 1.0 = none — build destination path set pathNSString to current application’s NSString’s stringWithString:imagePath set destPath to pathNSString’s stringByDeletingPathExtension()’s stringByAppendingPathExtension:"jpg" — load the file as an NSImage set theImage to current application’s NSImage’s alloc()’s initWithContentsOfFile:imagePath if theImage = missing value then return false set theData to theImage’s TIFFRepresentation() set newRep to current application’s NSBitmapImageRep’s imageRepWithData:theData set theData to (newRep’s representationUsingType:(current application’s NSJPEGFileType) |properties|:{NSImageCompressionFactor:compFactor, NSImageProgressive:false}) set theResult to (theData’s writeToFile:destPath atomically:true) as boolean return destPath as string end jpegFromPath:compressFactor: –POSIX path stringを与えると、ファイル名の重複を検出して、ファイル名の名称回避を行って、ファイル名のみを返す on chkExistPOSIXpathAndIncrementChildNumberFull(a) set aa to POSIX path of a set aStr to NSString’s stringWithString:aa set bStr to aStr’s lastPathComponent() set cStr to (bStr’s pathExtension()) as string set dStr to (bStr’s stringByDeletingPathExtension()) as string set eStr to (aStr’s stringByDeletingLastPathComponent()) as string set aManager to NSFileManager’s defaultManager() set aRes to (aManager’s fileExistsAtPath:aStr) as boolean if aRes = false then return aa set hitF to false repeat with i from 1 to 65535 set tmpPath to (eStr & "/" & dStr & "_" & (i as string) & "." & cStr) set tmpStr to (NSString’s stringWithString:tmpPath) set aRes to (aManager’s fileExistsAtPath:tmpStr) as boolean set bRes to ((tmpStr’s caseInsensitiveCompare:eStr) is not equal to (NSOrderedSame)) as boolean if {aRes, bRes} = {false, true} then set hitF to true exit repeat end if end repeat if hitF = false then return false –ファイルパス(フルパス)を返す return (tmpStr as string) end chkExistPOSIXpathAndIncrementChildNumberFull |
More from my site
(Visited 40 times, 1 visits today)