[NSPDFImageRep] PDFを各ページTIFF画像RGBにして保存する(解像度選択式)少し手直し
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #! /usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | # ピクセルサイズを偶数にすることを優先している |
005 | # 出力解像度は選択式 |
006 | # 注釈アノテーションは削除する |
007 | # com.cocolog-nifty.quicktimer.icefloe |
008 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
009 | use AppleScript version "2.6" |
010 | use framework "Foundation" |
011 | use framework "AppKit" |
012 | use framework "PDFKit" |
013 | use framework "Quartz" |
014 | use scripting additions |
015 | |
016 | property refMe : a reference to current application |
017 | set appFileManager to refMe's NSFileManager's defaultManager() |
018 | |
019 | ############################## |
020 | #【設定項目】注釈の画像化 する しない |
021 | set boolAnnotation2Image to false as boolean |
022 | |
023 | ############################## |
024 | #ダイアログを前面に出す |
025 | tell current application |
026 | set strName to name as text |
027 | end tell |
028 | if strName is "osascript" then |
029 | tell application "Finder" |
030 | activate |
031 | end tell |
032 | else |
033 | tell current application |
034 | activate |
035 | end tell |
036 | end if |
037 | |
038 | ############################## |
039 | #ファイル選択ダイアログ |
040 | set ocidUserDesktopPath to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
041 | set aliasDefaultLocation to ocidUserDesktopPath as alias |
042 | set listChooseFileUTI to {"com.adobe.pdf"} |
043 | set strPromptText to "PDFファイルを選んでください" as text |
044 | set strMesText to "PDFファイルを選んでください" as text |
045 | set aliasFilePath to (choose file strMesText with prompt strPromptText default location (aliasDefaultLocation) of type listChooseFileUTI without invisibles, multiple selections allowed and showing package contents) as alias |
046 | |
047 | ################################ |
048 | #パス URL |
049 | set strFilePath to (POSIX path of aliasFilePath) as text |
050 | set ocidFilePathStr to (refMe's NSString's stringWithString:strFilePath) |
051 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath |
052 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath) |
053 | #ファイル名を取得 |
054 | set ocidFileName to ocidFilePathURL's lastPathComponent() |
055 | #ファイル名から拡張子を取っていわゆるベースファイル名を取得 |
056 | set ocidPrefixName to ocidFileName's stringByDeletingPathExtension |
057 | #コンテナディレクトリを取得 |
058 | set ocidContainerDirURL to ocidFilePathURL's URLByDeletingLastPathComponent() |
059 | #保存先ディレクトリを作成 |
060 | set strPrefixName to ocidPrefixName as text |
061 | #ディレクトリ名はお好みで |
062 | set ocidSaveDirName to (strPrefixName & "_images") as text |
063 | #保存先ディレクトリURL |
064 | set ocidSaveFileDirPathURL to (ocidContainerDirURL's URLByAppendingPathComponent:ocidSaveDirName isDirectory:true) |
065 | #フォルダ作成 |
066 | set appFileManager to refMe's NSFileManager's defaultManager() |
067 | set ocidAttrDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0) |
068 | (ocidAttrDict's setValue:511 forKey:(refMe's NSFilePosixPermissions)) |
069 | set listDone to (appFileManager's createDirectoryAtURL:(ocidSaveFileDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference)) |
070 | if (item 2 of listDone) ≠ (missing value) then |
071 | set strErrorNO to (item 2 of listDone)'s code() as text |
072 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
073 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
074 | return "エラーしました" & strErrorNO & strErrorMes |
075 | end if |
076 | |
077 | ############################## |
078 | #ダイアログを前面に出す |
079 | tell current application |
080 | set strName to name as text |
081 | end tell |
082 | if strName is "osascript" then |
083 | tell application "Finder" to activate |
084 | else |
085 | tell current application to activate |
086 | end if |
087 | # |
088 | set listResolution to {"72", "96", "120", "144", "150", "216", "288", "300", "360"} as list |
089 | try |
090 | set listResponse to (choose from list listResolution with title "選んでください\n解像度が高いとそれなりに時間がかかります" with prompt "解像度を選んでください" default items (item 4 of listResolution) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) as list |
091 | on error |
092 | log "エラーしました" |
093 | return "エラーしました" |
094 | end try |
095 | if (item 1 of listResponse) is false then |
096 | return "キャンセルしました" |
097 | end if |
098 | set strResolution to (item 1 of listResponse) as text |
099 | #設定項目 出力解像度 ppi |
100 | set numResolution to strResolution as integer |
101 | |
102 | ##################### |
103 | #### 本処理 |
104 | ##################### |
105 | #保存時のカラープロファイルを読み出しておく ファイルはお好みで |
106 | set strIccFilePath to "/System/Library/ColorSync/Profiles/sRGB Profile.icc" |
107 | set ocidIccFilePathStr to (refMe's NSString's stringWithString:strIccFilePath) |
108 | set ocidIccFilePath to ocidIccFilePathStr's stringByStandardizingPath |
109 | set ocidIccFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidIccFilePath isDirectory:false) |
110 | set ocidProfileData to refMe's NSData's alloc()'s initWithContentsOfURL:ocidIccFilePathURL |
111 | |
112 | #PDFファイルを格納 |
113 | set ocidActivDoc to refMe's PDFDocument's alloc()'s initWithURL:(ocidFilePathURL) |
114 | #ページ数 |
115 | set numPageCnt to ocidActivDoc's pageCount() as integer |
116 | ########### |
117 | #ページ順に処理 |
118 | repeat with itemPageNo from 0 to (numPageCnt - 1) by 1 |
119 | #PDFの対象ページを格納 |
120 | set ocidPdfPage to (ocidActivDoc's pageAtIndex:(itemPageNo)) |
121 | ########### |
122 | #注釈を画像化するか?の分岐 |
123 | if boolAnnotation2Image is false then |
124 | #注釈を削除 |
125 | (ocidPdfPage's setDisplaysAnnotations:(true)) |
126 | set ocidAnotationArray to ocidPdfPage's annotations() |
127 | repeat with itemAnotationArray in ocidAnotationArray |
128 | (ocidPdfPage's removeAnnotation:(itemAnotationArray)) |
129 | end repeat |
130 | (ocidPdfPage's setDisplaysAnnotations:(false)) |
131 | (ocidPdfPage's removeAnnotation:(ocidAnotationArray)) |
132 | end if |
133 | #CROPサイズを取得しておくpt |
134 | set ocidPageRect to (ocidPdfPage's boundsForBox:(refMe's kPDFDisplayBoxCropBox)) |
135 | (* |
136 | #用紙サイズ |
137 | kPDFDisplayBoxMediaBox |
138 | #塗り足し サイズ |
139 | kPDFDisplayBoxBleedBox |
140 | #表示サイズ |
141 | kPDFDisplayBoxCropBox |
142 | #タチ落とし 仕上がりサイズ |
143 | kPDFDisplayBoxTrimBox |
144 | #アートサイズ |
145 | kPDFDisplayBoxArtBox |
146 | *) |
147 | set numPDFPtWidth to refMe's NSWidth(ocidPageRect) |
148 | set numPDFPtHeight to refMe's NSHeight(ocidPageRect) |
149 | set ocidPageSize to refMe's NSSize's NSMakeSize(numPDFPtWidth, numPDFPtHeight) |
150 | #サイズセットする比率 |
151 | set numSetResolution to (numResolution / 72.0) as number |
152 | #出力イメージピクセルサイズ |
153 | set numPxWidth to ((numPDFPtWidth * numSetResolution) div 1) as integer |
154 | set numPxHeigh to ((numPDFPtHeight * numSetResolution) div 1) as integer |
155 | #今回の処理は仕上がりピクセルサイズを偶数にすることを優先している |
156 | if ((numPxWidth / 2) mod 1) > 0 then |
157 | set numPxWidth to numPxWidth + 1 |
158 | set numPxHeigh to numPxHeigh + 1 |
159 | end if |
160 | if ((numPxHeigh / 2) mod 1) > 0 then |
161 | set numPxHeigh to numPxHeigh + 1 |
162 | end if |
163 | #出力用の解像度をセットするためのNSSize'sをピクセルサイズから生成 |
164 | set numSetPtSizeW to (numPxWidth / numSetResolution) as number |
165 | set numSetPtSizeH to (numPxHeigh / numSetResolution) as number |
166 | set ocidSetImageSize to refMe's NSSize's NSMakeSize(numSetPtSizeW, numSetPtSizeH) |
167 | |
168 | #描画するRECT |
169 | set ocidDrawRect to refMe's NSRect's NSMakeRect(0, 0, numPxWidth, numPxHeigh) |
170 | #コピーしてくるRECT=PDFのページPTサイズ |
171 | set ocidFromRect to refMe's NSRect's NSMakeRect(0, 0, numPDFPtWidth, numPDFPtHeight) |
172 | #NSDATAに |
173 | set ocidDataRep to ocidPdfPage's dataRepresentation() |
174 | #NSDATAに格納 |
175 | set ocidPageData to (refMe's NSData's alloc()'s initWithData:(ocidDataRep)) |
176 | #NSPDFImageRepに変換 |
177 | set ocidPagePep to (refMe's NSPDFImageRep's alloc()'s initWithData:(ocidDataRep)) |
178 | #念の為最初のページをセット |
179 | (ocidPagePep's setCurrentPage:(0)) |
180 | #出力される画像(アートボード)=解像度換算のピクセルサイズ |
181 | set ocidAardboardRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numPxWidth) pixelsHigh:(numPxHeigh) bitsPerSample:(8) samplesPerPixel:(4) hasAlpha:(true) isPlanar:(false) colorSpaceName:(refMe's NSCalibratedRGBColorSpace) bitmapFormat:(refMe's NSBitmapFormatAlphaFirst) bytesPerRow:(0) bitsPerPixel:(32)) |
182 | (* bitmapFormat |
183 | 透過事前処理 |
184 | NSBitmapFormatAlphaFirst |
185 | 透過保持 |
186 | NSBitmapFormatAlphaNonpremultiplied |
187 | HDR用 |
188 | NSBitmapFormatFloatingPointSamples |
189 | 16-bit BE |
190 | NSBitmapFormatSixteenBitBigEndian |
191 | 16-bit LE |
192 | NSBitmapFormatSixteenBitLittleEndian |
193 | 32-bit BE |
194 | NSBitmapFormatThirtyTwoBitBigEndian |
195 | 32-bit LE |
196 | NSBitmapFormatThirtyTwoBitLittleEndian |
197 | *) |
198 | #アートボード画像にカラープロファイルをエンベッドする |
199 | (ocidAardboardRep's setProperty:(refMe's NSImageColorSyncProfileData) withValue:(ocidProfileData)) |
200 | |
201 | ######################## |
202 | #NSGraphicsContext初期化 |
203 | set appGraphicsContext to (refMe's NSGraphicsContext) |
204 | #編集開始 |
205 | appGraphicsContext's saveGraphicsState() |
206 | #アートボード画像読み込み |
207 | set ocidContext to (appGraphicsContext's graphicsContextWithBitmapImageRep:(ocidAardboardRep)) |
208 | #アンチエイリアスの処理有無 バーコードや線画の場合はfalseを |
209 | (ocidContext's setShouldAntialias:(true)) |
210 | #NSImageInterpolationHigh |
211 | (ocidContext's setImageInterpolation:(refMe's NSImageInterpolationHigh)) |
212 | (* |
213 | 標準 自動判断 |
214 | NSImageInterpolationDefault |
215 | アンチエイリアスが立ちにくい バーコードや線画等 |
216 | NSImageInterpolationNone |
217 | ボケ足多め 低負荷 高速処理向き |
218 | NSImageInterpolationLow |
219 | ボケ中間 |
220 | NSImageInterpolationMedium |
221 | 高画質 高負荷 |
222 | NSImageInterpolationHigh |
223 | *) |
224 | #NSColorRenderingIntentRelativeColorimetric |
225 | (ocidContext's setColorRenderingIntent:(refMe's NSColorRenderingIntentRelativeColorimetric)) |
226 | (* |
227 | 標準 |
228 | NSColorRenderingIntentDefault |
229 | 絶対的な色彩を保持 (色の再現重視 印刷向き) |
230 | NSColorRenderingIntentAbsoluteColorimetric |
231 | 相対的な色彩を保持 (全体的なバランス重視 モニター向き) |
232 | NSColorRenderingIntentRelativeColorimetric |
233 | 知覚的な色再現 (写真向き 色数が多い場合) |
234 | NSColorRenderingIntentPerceptual |
235 | 彩度を優先 (グラフィック向き 色数が少ない場合) |
236 | NSColorRenderingIntentSaturation |
237 | *) |
238 | #アートボード画像をContextとしてセット |
239 | (appGraphicsContext's setCurrentContext:(ocidContext)) |
240 | #PDFpageRepをアートボードにペースト |
241 | (ocidPagePep's drawInRect:(ocidDrawRect) fromRect:(ocidFromRect) operation:(refMe's NSCompositeSourceOver) fraction:(1.0) respectFlipped:(false) hints:(missing value)) |
242 | #編集終了 |
243 | appGraphicsContext's restoreGraphicsState() |
244 | |
245 | ################## |
246 | #変換設定 オプションTIFF |
247 | set ocidPropertyDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0) |
248 | (ocidPropertyDict's setValue:(0.0) forKey:(refMe's NSImageCompressionFactor)) |
249 | (ocidPropertyDict's setValue:(refMe's NSTIFFCompressionNone) forKey:(refMe's NSImageCompressionMethod)) |
250 | (ocidPropertyDict's setValue:(missing value) forKey:(refMe's NSImageIPTCData)) |
251 | (ocidPropertyDict's setValue:(missing value) forKey:(refMe's NSImageEXIFData)) |
252 | (ocidPropertyDict's setValue:(ocidProfileData) forKey:(refMe's NSImageColorSyncProfileData)) |
253 | |
254 | set ocidFileType to (refMe's NSBitmapImageFileTypeTIFF) |
255 | set strExtension to "tiff" |
256 | #サイズ指定 ここで解像度が決まる |
257 | (ocidAardboardRep's setSize:(ocidSetImageSize)) |
258 | #変換 |
259 | set ocidNSInlineData to (ocidAardboardRep's representationUsingType:(ocidFileType) |properties|:(ocidPropertyDict)) |
260 | |
261 | ################### |
262 | ###ページ数連番ゼロサプレス |
263 | set numSaveImageNo to (itemPageNo + 1) as integer |
264 | set strSaveImageNo to numSaveImageNo as text |
265 | set strZeroAdd to ("0000" & strSaveImageNo) as text |
266 | set strPageNO to text -4 through -1 of strZeroAdd as text |
267 | ###ファイル名に整形して |
268 | set strSaveImageFileName to ("" & strPrefixName & "." & strPageNO & "." & strExtension & "") as text |
269 | ####保存先URL |
270 | set ocidSaveFilePathURL to (ocidSaveFileDirPathURL's URLByAppendingPathComponent:strSaveImageFileName isDirectory:false) |
271 | #####保存 |
272 | set boolDone to (ocidNSInlineData's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error| :(reference)) |
273 | #一応リセットしたりして |
274 | ocidContext's flushGraphics() |
275 | set ocidAardboardRep to "" |
276 | set ocidNSInlineData to "" |
277 | set ocidBmpImageRep to "" |
278 | set ocidOsDispatchData to "" |
279 | set ocidPageImageData to "" |
280 | end repeat |
281 | #保存先を開く |
282 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
283 | set boolDone to appSharedWorkspace's openURL:(ocidSaveFileDirPathURL) |
284 | |
285 | return "処理終了" |
286 | |
AppleScriptで生成しました |
| 固定リンク