PDFImageRep

[NSPDFImageRep] PDFを各ページTIFF画像RGBにして保存する(解像度選択式)少し手直し


AppleScript サンプルコード

【スクリプトエディタで開く】 |

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
009use AppleScript version "2.6"
010use framework "Foundation"
011use framework "AppKit"
012use framework "PDFKit"
013use framework "Quartz"
014use scripting additions
015
016property refMe : a reference to current application
017set appFileManager to refMe's NSFileManager's defaultManager()
018
019##############################
020#【設定項目】注釈の画像化 する しない
021set boolAnnotation2Image to false as boolean
022
023##############################
024#ダイアログを前面に出す
025tell current application
026  set strName to name as text
027end tell
028if strName is "osascript" then
029  tell application "Finder"
030    activate
031  end tell
032else
033  tell current application
034    activate
035  end tell
036end if
037
038##############################
039#ファイル選択ダイアログ
040set ocidUserDesktopPath to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
041set aliasDefaultLocation to ocidUserDesktopPath as alias
042set listChooseFileUTI to {"com.adobe.pdf"}
043set strPromptText to "PDFファイルを選んでください" as text
044set strMesText to "PDFファイルを選んでください" as text
045set 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
049set strFilePath to (POSIX path of aliasFilePath) as text
050set ocidFilePathStr to (refMe's NSString's stringWithString:strFilePath)
051set ocidFilePath to ocidFilePathStr's stringByStandardizingPath
052set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath)
053#ファイル名を取得
054set ocidFileName to ocidFilePathURL's lastPathComponent()
055#ファイル名から拡張子を取っていわゆるベースファイル名を取得
056set ocidPrefixName to ocidFileName's stringByDeletingPathExtension
057#コンテナディレクトリを取得
058set ocidContainerDirURL to ocidFilePathURL's URLByDeletingLastPathComponent()
059#保存先ディレクトリを作成
060set strPrefixName to ocidPrefixName as text
061#ディレクトリ名はお好みで
062set ocidSaveDirName to (strPrefixName & "_images") as text
063#保存先ディレクトリURL
064set ocidSaveFileDirPathURL to (ocidContainerDirURL's URLByAppendingPathComponent:ocidSaveDirName isDirectory:true)
065#フォルダ作成
066set appFileManager to refMe's NSFileManager's defaultManager()
067set ocidAttrDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
068(ocidAttrDict's setValue:511 forKey:(refMe's NSFilePosixPermissions))
069set listDone to (appFileManager's createDirectoryAtURL:(ocidSaveFileDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference))
070if (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
075end if
076
077##############################
078#ダイアログを前面に出す
079tell current application
080  set strName to name as text
081end tell
082if strName is "osascript" then
083  tell application "Finder" to activate
084else
085  tell current application to activate
086end if
087#
088set listResolution to {"72", "96", "120", "144", "150", "216", "288", "300", "360"} as list
089try
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
091on error
092  log "エラーしました"
093  return "エラーしました"
094end try
095if (item 1 of listResponse) is false then
096  return "キャンセルしました"
097end if
098set strResolution to (item 1 of listResponse) as text
099#設定項目 出力解像度 ppi
100set numResolution to strResolution as integer
101
102#####################
103#### 本処理
104#####################
105#保存時のカラープロファイルを読み出しておく ファイルはお好みで
106set strIccFilePath to "/System/Library/ColorSync/Profiles/sRGB Profile.icc"
107set ocidIccFilePathStr to (refMe's NSString's stringWithString:strIccFilePath)
108set ocidIccFilePath to ocidIccFilePathStr's stringByStandardizingPath
109set ocidIccFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidIccFilePath isDirectory:false)
110set ocidProfileData to refMe's NSData's alloc()'s initWithContentsOfURL:ocidIccFilePathURL
111
112#PDFファイルを格納
113set ocidActivDoc to refMe's PDFDocument's alloc()'s initWithURL:(ocidFilePathURL)
114#ページ数
115set numPageCnt to ocidActivDoc's pageCount() as integer
116###########
117#ページ順に処理
118repeat 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透過事前処理
184NSBitmapFormatAlphaFirst
185透過保持
186NSBitmapFormatAlphaNonpremultiplied
187HDR用
188NSBitmapFormatFloatingPointSamples
18916-bit BE
190NSBitmapFormatSixteenBitBigEndian
19116-bit LE
192NSBitmapFormatSixteenBitLittleEndian
19332-bit BE
194NSBitmapFormatThirtyTwoBitBigEndian
19532-bit LE
196NSBitmapFormatThirtyTwoBitLittleEndian
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 ""
280end repeat
281#保存先を開く
282set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
283set boolDone to appSharedWorkspace's openURL:(ocidSaveFileDirPathURL)
284
285return "処理終了"
286
AppleScriptで生成しました

|

[NSPDFImageRep] PDFを各ページTIFF画像Grayにして保存する(解像度選択式)


AppleScript サンプルコード

【スクリプトエディタで開く】 |

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
009use AppleScript version "2.6"
010use framework "Foundation"
011use framework "AppKit"
012use framework "PDFKit"
013use framework "Quartz"
014use scripting additions
015
016property refMe : a reference to current application
017set appFileManager to refMe's NSFileManager's defaultManager()
018
019
020##############################
021#ダイアログを前面に出す
022tell current application
023  set strName to name as text
024end tell
025if strName is "osascript" then
026  tell application "Finder"
027    activate
028  end tell
029else
030  tell current application
031    activate
032  end tell
033end if
034
035##############################
036#ファイル選択ダイアログ
037set ocidUserDesktopPath to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
038set aliasDefaultLocation to ocidUserDesktopPath as alias
039set listChooseFileUTI to {"com.adobe.pdf"}
040set strPromptText to "PDFファイルを選んでください" as text
041set strMesText to "PDFファイルを選んでください" as text
042set 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
043
044################################
045#パス URL
046set strFilePath to (POSIX path of aliasFilePath) as text
047set ocidFilePathStr to (refMe's NSString's stringWithString:strFilePath)
048set ocidFilePath to ocidFilePathStr's stringByStandardizingPath
049set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath)
050#ファイル名を取得
051set ocidFileName to ocidFilePathURL's lastPathComponent()
052#ファイル名から拡張子を取っていわゆるベースファイル名を取得
053set ocidPrefixName to ocidFileName's stringByDeletingPathExtension
054#コンテナディレクトリを取得
055set ocidContainerDirURL to ocidFilePathURL's URLByDeletingLastPathComponent()
056#保存先ディレクトリを作成
057set strPrefixName to ocidPrefixName as text
058#ディレクトリ名はお好みで
059set ocidSaveDirName to (strPrefixName & "_images") as text
060#保存先ディレクトリURL
061set ocidSaveFileDirPathURL to (ocidContainerDirURL's URLByAppendingPathComponent:ocidSaveDirName isDirectory:true)
062#フォルダ作成
063set appFileManager to refMe's NSFileManager's defaultManager()
064set ocidAttrDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
065(ocidAttrDict's setValue:511 forKey:(refMe's NSFilePosixPermissions))
066set listDone to (appFileManager's createDirectoryAtURL:(ocidSaveFileDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference))
067if (item 2 of listDone) ≠ (missing value) then
068  set strErrorNO to (item 2 of listDone)'s code() as text
069  set strErrorMes to (item 2 of listDone)'s localizedDescription() as text
070  refMe's NSLog("■:" & strErrorNO & strErrorMes)
071  return "エラーしました" & strErrorNO & strErrorMes
072end if
073
074##############################
075#ダイアログを前面に出す
076tell current application
077  set strName to name as text
078end tell
079if strName is "osascript" then
080  tell application "Finder" to activate
081else
082  tell current application to activate
083end if
084#
085set listResolution to {"72", "96", "120", "144", "150", "216", "288", "300", "360"} as list
086try
087  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
088on error
089  log "エラーしました"
090  return "エラーしました"
091end try
092if (item 1 of listResponse) is false then
093  return "キャンセルしました"
094end if
095set strResolution to (item 1 of listResponse) as text
096#設定項目 出力解像度 ppi
097set numResolution to strResolution as integer
098
099#####################
100#### 本処理
101#####################
102#保存時のカラープロファイルを読み出しておく ファイルはお好みで
103set strIccFilePath to "/System/Library/ColorSync/Profiles/Generic Gray Gamma 2.2 Profile.icc"
104set ocidIccFilePathStr to (refMe's NSString's stringWithString:strIccFilePath)
105set ocidIccFilePath to ocidIccFilePathStr's stringByStandardizingPath
106set ocidIccFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidIccFilePath) isDirectory:false)
107set ocidProfileData to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidIccFilePathURL)
108
109#PDFファイルを格納
110set ocidActivDoc to refMe's PDFDocument's alloc()'s initWithURL:(ocidFilePathURL)
111#ページ数
112set numPageCnt to ocidActivDoc's pageCount() as integer
113#ページ順に処理
114repeat with itemPageNo from 0 to (numPageCnt - 1) by 1
115  #PDFの対象ページを格納
116  set ocidPdfPage to (ocidActivDoc's pageAtIndex:(itemPageNo))
117  #注釈を削除
118  (ocidPdfPage's setDisplaysAnnotations:(true))
119  set ocidAnotationArray to ocidPdfPage's annotations()
120  repeat with itemAnotationArray in ocidAnotationArray
121    (ocidPdfPage's removeAnnotation:(itemAnotationArray))
122  end repeat
123  (ocidPdfPage's setDisplaysAnnotations:(false))
124  (ocidPdfPage's removeAnnotation:(ocidAnotationArray))
125  #CROPサイズを取得しておくpt
126  set ocidPageRect to (ocidPdfPage's boundsForBox:(refMe's kPDFDisplayBoxCropBox))
127  set numPDFPtWidth to refMe's NSWidth(ocidPageRect)
128  set numPDFPtHeight to refMe's NSHeight(ocidPageRect)
129  set ocidPageSize to refMe's NSSize's NSMakeSize(numPDFPtWidth, numPDFPtHeight)
130  #サイズセットする比率
131  set numSetResolution to (numResolution / 72.0) as number
132  #出力イメージピクセルサイズ
133  set numPxWidth to ((numPDFPtWidth * numSetResolution) div 1) as integer
134  set numPxHeigh to ((numPDFPtHeight * numSetResolution) div 1) as integer
135  #今回の処理は仕上がりピクセルサイズを偶数にすることを優先している
136  if ((numPxWidth / 2) mod 1) > 0 then
137    set numPxWidth to numPxWidth + 1
138    set numPxHeigh to numPxHeigh + 1
139  end if
140  if ((numPxHeigh / 2) mod 1) > 0 then
141    set numPxHeigh to numPxHeigh + 1
142  end if
143  #出力用の解像度をセットするためのNSSize'sをピクセルサイズから生成
144  set numSetPtSizeW to (numPxWidth / numSetResolution) as number
145  set numSetPtSizeH to (numPxHeigh / numSetResolution) as number
146  set ocidSetImageSize to refMe's NSSize's NSMakeSize(numSetPtSizeW, numSetPtSizeH)
147  
148  #描画するRECT
149  set ocidDrawRect to refMe's NSRect's NSMakeRect(0, 0, numPxWidth, numPxHeigh)
150  #コピーしてくるRECT=PDFのページPTサイズ
151  set ocidFromRect to refMe's NSRect's NSMakeRect(0, 0, numPDFPtWidth, numPDFPtHeight)
152  #NSDATAに
153  set ocidDataRep to ocidPdfPage's dataRepresentation()
154  #NSDATAに格納
155  set ocidPageData to (refMe's NSData's alloc()'s initWithData:(ocidDataRep))
156  #NSPDFImageRepに変換
157  set ocidPagePep to (refMe's NSPDFImageRep's alloc()'s initWithData:(ocidDataRep))
158  #念の為最初のページをセット
159  (ocidPagePep's setCurrentPage:(0))
160  
161  #出力される画像(アートボード)=解像度換算のピクセルサイズ
162  set ocidAardboardRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numPxWidth) pixelsHigh:(numPxHeigh) bitsPerSample:(8) samplesPerPixel:(1) hasAlpha:(false) isPlanar:(false) colorSpaceName:(refMe's NSCalibratedWhiteColorSpace) bitmapFormat:(refMe's NSBitmapFormatAlphaNonpremultiplied) bytesPerRow:(0) bitsPerPixel:(0))
163  #
164  (ocidAardboardRep's setProperty:(refMe's NSImageColorSyncProfileData) withValue:(ocidProfileData))
165  
166  ########################
167  #NSGraphicsContext初期化
168  set appGraphicsContext to (refMe's NSGraphicsContext)
169  #編集開始
170  appGraphicsContext's saveGraphicsState()
171  #アートボード画像読み込み
172  set ocidContext to (appGraphicsContext's graphicsContextWithBitmapImageRep:(ocidAardboardRep))
173  #アートボード画像をContextとしてセット
174  (appGraphicsContext's setCurrentContext:(ocidContext))
175  #PDFpageRepをアートボードにペースト
176  (ocidPagePep's drawInRect:(ocidDrawRect) fromRect:(ocidFromRect) operation:(refMe's NSCompositeSourceOver) fraction:(1.0) respectFlipped:(false) hints:(missing value))
177  #編集終了
178  appGraphicsContext's restoreGraphicsState()
179  
180  ##################
181  #変換設定 オプション設定
182  set ocidPropertyDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
183  (ocidPropertyDict's setValue:(0.0) forKey:(refMe's NSImageCompressionFactor))
184  (ocidPropertyDict's setValue:(refMe's NSTIFFCompressionNone) forKey:(refMe's NSImageCompressionMethod))
185  (ocidPropertyDict's setValue:(missing value) forKey:(refMe's NSImageIPTCData))
186  (ocidPropertyDict's setValue:(missing value) forKey:(refMe's NSImageEXIFData))
187  (ocidPropertyDict's setValue:(ocidProfileData) forKey:(refMe's NSImageColorSyncProfileData))
188  
189  set ocidFileType to (refMe's NSBitmapImageFileTypeTIFF)
190  set strExtension to "tiff"
191  #サイズ指定 ここで解像度が決まる
192  (ocidAardboardRep's setSize:(ocidSetImageSize))
193  #変換
194  set ocidNSInlineData to (ocidAardboardRep's representationUsingType:(ocidFileType) |properties|:(ocidPropertyDict))
195  
196  ###################
197  ###ページ数連番ゼロサプレス
198  set strZeroAdd to ("0000" & (itemPageNo + 1)) as text
199  set strPageNO to text -4 through -1 of strZeroAdd as text
200  ###ファイル名に整形して
201  set strSaveImageFileName to ("" & strPrefixName & "." & strPageNO & "." & strExtension & "") as text
202  ####保存先URL
203  set ocidSaveFilePathURL to (ocidSaveFileDirPathURL's URLByAppendingPathComponent:strSaveImageFileName isDirectory:false)
204  #####保存
205  set boolDone to (ocidNSInlineData's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error| :(reference))
206  #一応リセットしたりして
207  set ocidAardboardRep to ""
208  set ocidNSInlineData to ""
209  set ocidBmpImageRep to ""
210  set ocidOsDispatchData to ""
211  set ocidPageImageData to ""
212end repeat
213#保存先を開く
214set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
215set boolDone to appSharedWorkspace's openURL:(ocidSaveFileDirPathURL)
216
217return "処理終了"
218
AppleScriptで生成しました

|

[NSPDFImageRep] PDFを各ページTIFF画像CMYKにして保存する(解像度選択式)


AppleScript サンプルコード

【スクリプトエディタで開く】 |

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
009use AppleScript version "2.6"
010use framework "Foundation"
011use framework "AppKit"
012use framework "PDFKit"
013use framework "Quartz"
014use scripting additions
015
016property refMe : a reference to current application
017set appFileManager to refMe's NSFileManager's defaultManager()
018
019
020##############################
021#ダイアログを前面に出す
022tell current application
023  set strName to name as text
024end tell
025if strName is "osascript" then
026  tell application "Finder"
027    activate
028  end tell
029else
030  tell current application
031    activate
032  end tell
033end if
034
035##############################
036#ファイル選択ダイアログ
037set ocidUserDesktopPath to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
038set aliasDefaultLocation to ocidUserDesktopPath as alias
039set listChooseFileUTI to {"com.adobe.pdf"}
040set strPromptText to "PDFファイルを選んでください" as text
041set strMesText to "PDFファイルを選んでください" as text
042set 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
043
044################################
045#パス URL
046set strFilePath to (POSIX path of aliasFilePath) as text
047set ocidFilePathStr to (refMe's NSString's stringWithString:strFilePath)
048set ocidFilePath to ocidFilePathStr's stringByStandardizingPath
049set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath)
050#ファイル名を取得
051set ocidFileName to ocidFilePathURL's lastPathComponent()
052#ファイル名から拡張子を取っていわゆるベースファイル名を取得
053set ocidPrefixName to ocidFileName's stringByDeletingPathExtension
054#コンテナディレクトリを取得
055set ocidContainerDirURL to ocidFilePathURL's URLByDeletingLastPathComponent()
056#保存先ディレクトリを作成
057set strPrefixName to ocidPrefixName as text
058#ディレクトリ名はお好みで
059set ocidSaveDirName to (strPrefixName & "_images") as text
060#保存先ディレクトリURL
061set ocidSaveFileDirPathURL to (ocidContainerDirURL's URLByAppendingPathComponent:ocidSaveDirName isDirectory:true)
062#フォルダ作成
063set appFileManager to refMe's NSFileManager's defaultManager()
064set ocidAttrDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
065(ocidAttrDict's setValue:511 forKey:(refMe's NSFilePosixPermissions))
066set listDone to (appFileManager's createDirectoryAtURL:(ocidSaveFileDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference))
067if (item 2 of listDone) ≠ (missing value) then
068  set strErrorNO to (item 2 of listDone)'s code() as text
069  set strErrorMes to (item 2 of listDone)'s localizedDescription() as text
070  refMe's NSLog("■:" & strErrorNO & strErrorMes)
071  return "エラーしました" & strErrorNO & strErrorMes
072end if
073
074##############################
075#ダイアログを前面に出す
076tell current application
077  set strName to name as text
078end tell
079if strName is "osascript" then
080  tell application "Finder" to activate
081else
082  tell current application to activate
083end if
084#
085set listResolution to {"72", "96", "120", "144", "150", "216", "288", "300", "360"} as list
086try
087  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
088on error
089  log "エラーしました"
090  return "エラーしました"
091end try
092if (item 1 of listResponse) is false then
093  return "キャンセルしました"
094end if
095set strResolution to (item 1 of listResponse) as text
096#設定項目 出力解像度 ppi
097set numResolution to strResolution as integer
098
099#####################
100#### 本処理
101#####################
102#保存時のカラープロファイルを読み出しておく ファイルはお好みで
103set strIccFilePath to "/System/Library/ColorSync/Profiles/Generic CMYK Profile.icc"
104set ocidIccFilePathStr to (refMe's NSString's stringWithString:strIccFilePath)
105set ocidIccFilePath to ocidIccFilePathStr's stringByStandardizingPath
106set ocidIccFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidIccFilePath) isDirectory:false)
107set ocidProfileData to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidIccFilePathURL)
108
109#PDFファイルを格納
110set ocidActivDoc to refMe's PDFDocument's alloc()'s initWithURL:(ocidFilePathURL)
111#ページ数
112set numPageCnt to ocidActivDoc's pageCount() as integer
113#ページ順に処理
114repeat with itemPageNo from 0 to (numPageCnt - 1) by 1
115  #PDFの対象ページを格納
116  set ocidPdfPage to (ocidActivDoc's pageAtIndex:(itemPageNo))
117  #注釈を削除
118  (ocidPdfPage's setDisplaysAnnotations:(true))
119  set ocidAnotationArray to ocidPdfPage's annotations()
120  repeat with itemAnotationArray in ocidAnotationArray
121    (ocidPdfPage's removeAnnotation:(itemAnotationArray))
122  end repeat
123  (ocidPdfPage's setDisplaysAnnotations:(false))
124  (ocidPdfPage's removeAnnotation:(ocidAnotationArray))
125  #CROPサイズを取得しておくpt
126  set ocidPageRect to (ocidPdfPage's boundsForBox:(refMe's kPDFDisplayBoxCropBox))
127  set numPDFPtWidth to refMe's NSWidth(ocidPageRect)
128  set numPDFPtHeight to refMe's NSHeight(ocidPageRect)
129  set ocidPageSize to refMe's NSSize's NSMakeSize(numPDFPtWidth, numPDFPtHeight)
130  #サイズセットする比率
131  set numSetResolution to (numResolution / 72.0) as number
132  #出力イメージピクセルサイズ
133  set numPxWidth to ((numPDFPtWidth * numSetResolution) div 1) as integer
134  set numPxHeigh to ((numPDFPtHeight * numSetResolution) div 1) as integer
135  #今回の処理は仕上がりピクセルサイズを偶数にすることを優先している
136  if ((numPxWidth / 2) mod 1) > 0 then
137    set numPxWidth to numPxWidth + 1
138    set numPxHeigh to numPxHeigh + 1
139  end if
140  if ((numPxHeigh / 2) mod 1) > 0 then
141    set numPxHeigh to numPxHeigh + 1
142  end if
143  #出力用の解像度をセットするためのNSSize'sをピクセルサイズから生成
144  set numSetPtSizeW to (numPxWidth / numSetResolution) as number
145  set numSetPtSizeH to (numPxHeigh / numSetResolution) as number
146  set ocidSetImageSize to refMe's NSSize's NSMakeSize(numSetPtSizeW, numSetPtSizeH)
147  
148  #描画するRECT
149  set ocidDrawRect to refMe's NSRect's NSMakeRect(0, 0, numPxWidth, numPxHeigh)
150  #コピーしてくるRECT=PDFのページPTサイズ
151  set ocidFromRect to refMe's NSRect's NSMakeRect(0, 0, numPDFPtWidth, numPDFPtHeight)
152  #NSDATAに
153  set ocidDataRep to ocidPdfPage's dataRepresentation()
154  #NSDATAに格納
155  set ocidPageData to (refMe's NSData's alloc()'s initWithData:(ocidDataRep))
156  #NSPDFImageRepに変換
157  set ocidPagePep to (refMe's NSPDFImageRep's alloc()'s initWithData:(ocidDataRep))
158  #念の為最初のページをセット
159  (ocidPagePep's setCurrentPage:(0))
160  #出力される画像(アートボード)=解像度換算のピクセルサイズ
161  set ocidAardboardRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numPxWidth) pixelsHigh:(numPxHeigh) bitsPerSample:(8) samplesPerPixel:(4) hasAlpha:(false) isPlanar:(false) colorSpaceName:(refMe's NSDeviceCMYKColorSpace) bitmapFormat:(refMe's NSBitmapFormatAlphaNonpremultiplied) bytesPerRow:(0) bitsPerPixel:(32))
162  #
163  (ocidAardboardRep's setProperty:(refMe's NSImageColorSyncProfileData) withValue:(ocidProfileData))
164  
165  ########################
166  #NSGraphicsContext初期化
167  set appGraphicsContext to (refMe's NSGraphicsContext)
168  #編集開始
169  appGraphicsContext's saveGraphicsState()
170  #アートボード画像読み込み
171  set ocidContext to (appGraphicsContext's graphicsContextWithBitmapImageRep:(ocidAardboardRep))
172  #アートボード画像をContextとしてセット
173  (appGraphicsContext's setCurrentContext:(ocidContext))
174  #PDFpageRepをアートボードにペースト
175  (ocidPagePep's drawInRect:(ocidDrawRect) fromRect:(ocidFromRect) operation:(refMe's NSCompositeSourceOver) fraction:(1.0) respectFlipped:(false) hints:(missing value))
176  #編集終了
177  appGraphicsContext's restoreGraphicsState()
178  
179  ##################
180  #変換設定 オプション設定
181  set ocidPropertyDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
182  (ocidPropertyDict's setValue:(0.0) forKey:(refMe's NSImageCompressionFactor))
183  (ocidPropertyDict's setValue:(refMe's NSTIFFCompressionNone) forKey:(refMe's NSImageCompressionMethod))
184  (ocidPropertyDict's setValue:(missing value) forKey:(refMe's NSImageIPTCData))
185  (ocidPropertyDict's setValue:(missing value) forKey:(refMe's NSImageEXIFData))
186  (ocidPropertyDict's setValue:(ocidProfileData) forKey:(refMe's NSImageColorSyncProfileData))
187  
188  set ocidFileType to (refMe's NSBitmapImageFileTypeTIFF)
189  set strExtension to "tiff"
190  #サイズ指定 ここで解像度が決まる
191  (ocidAardboardRep's setSize:(ocidSetImageSize))
192  #変換
193  set ocidNSInlineData to (ocidAardboardRep's representationUsingType:(ocidFileType) |properties|:(ocidPropertyDict))
194  
195  ###################
196  ###ページ数連番ゼロサプレス
197  set strZeroAdd to ("0000" & (itemPageNo + 1)) as text
198  set strPageNO to text -4 through -1 of strZeroAdd as text
199  ###ファイル名に整形して
200  set strSaveImageFileName to ("" & strPrefixName & "." & strPageNO & "." & strExtension & "") as text
201  ####保存先URL
202  set ocidSaveFilePathURL to (ocidSaveFileDirPathURL's URLByAppendingPathComponent:strSaveImageFileName isDirectory:false)
203  #####保存
204  set boolDone to (ocidNSInlineData's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error| :(reference))
205  #一応リセットしたりして
206  set ocidAardboardRep to ""
207  set ocidNSInlineData to ""
208  set ocidBmpImageRep to ""
209  set ocidOsDispatchData to ""
210  set ocidPageImageData to ""
211end repeat
212#保存先を開く
213set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
214set boolDone to appSharedWorkspace's openURL:(ocidSaveFileDirPathURL)
215
216return "処理終了"
217
AppleScriptで生成しました

|

[NSPDFImageRep] PDFを各ページTIFF画像RGBにして保存する(解像度選択式)


AppleScript サンプルコード

【スクリプトエディタで開く】 |

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
009use AppleScript version "2.6"
010use framework "Foundation"
011use framework "AppKit"
012use framework "PDFKit"
013use framework "Quartz"
014use scripting additions
015
016property refMe : a reference to current application
017set appFileManager to refMe's NSFileManager's defaultManager()
018
019
020##############################
021#ダイアログを前面に出す
022tell current application
023  set strName to name as text
024end tell
025if strName is "osascript" then
026  tell application "Finder"
027    activate
028  end tell
029else
030  tell current application
031    activate
032  end tell
033end if
034
035##############################
036#ファイル選択ダイアログ
037set ocidUserDesktopPath to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
038set aliasDefaultLocation to ocidUserDesktopPath as alias
039set listChooseFileUTI to {"com.adobe.pdf"}
040set strPromptText to "PDFファイルを選んでください" as text
041set strMesText to "PDFファイルを選んでください" as text
042set 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
043
044################################
045#パス URL
046set strFilePath to (POSIX path of aliasFilePath) as text
047set ocidFilePathStr to (refMe's NSString's stringWithString:strFilePath)
048set ocidFilePath to ocidFilePathStr's stringByStandardizingPath
049set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath)
050#ファイル名を取得
051set ocidFileName to ocidFilePathURL's lastPathComponent()
052#ファイル名から拡張子を取っていわゆるベースファイル名を取得
053set ocidPrefixName to ocidFileName's stringByDeletingPathExtension
054#コンテナディレクトリを取得
055set ocidContainerDirURL to ocidFilePathURL's URLByDeletingLastPathComponent()
056#保存先ディレクトリを作成
057set strPrefixName to ocidPrefixName as text
058#ディレクトリ名はお好みで
059set ocidSaveDirName to (strPrefixName & "_images") as text
060#保存先ディレクトリURL
061set ocidSaveFileDirPathURL to (ocidContainerDirURL's URLByAppendingPathComponent:ocidSaveDirName isDirectory:true)
062#フォルダ作成
063set appFileManager to refMe's NSFileManager's defaultManager()
064set ocidAttrDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
065(ocidAttrDict's setValue:511 forKey:(refMe's NSFilePosixPermissions))
066set listDone to (appFileManager's createDirectoryAtURL:(ocidSaveFileDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference))
067if (item 2 of listDone) ≠ (missing value) then
068  set strErrorNO to (item 2 of listDone)'s code() as text
069  set strErrorMes to (item 2 of listDone)'s localizedDescription() as text
070  refMe's NSLog("■:" & strErrorNO & strErrorMes)
071  return "エラーしました" & strErrorNO & strErrorMes
072end if
073
074##############################
075#ダイアログを前面に出す
076tell current application
077  set strName to name as text
078end tell
079if strName is "osascript" then
080  tell application "Finder" to activate
081else
082  tell current application to activate
083end if
084#
085set listResolution to {"72", "96", "120", "144", "150", "216", "288", "300", "360"} as list
086try
087  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
088on error
089  log "エラーしました"
090  return "エラーしました"
091end try
092if (item 1 of listResponse) is false then
093  return "キャンセルしました"
094end if
095set strResolution to (item 1 of listResponse) as text
096#設定項目 出力解像度 ppi
097set numResolution to strResolution as integer
098
099#####################
100#### 本処理
101#####################
102#PDFファイルを格納
103set ocidActivDoc to refMe's PDFDocument's alloc()'s initWithURL:(ocidFilePathURL)
104#ページ数
105set numPageCnt to ocidActivDoc's pageCount() as integer
106#ページ順に処理
107repeat with itemPageNo from 0 to (numPageCnt - 1) by 1
108  #PDFの対象ページを格納
109  set ocidPdfPage to (ocidActivDoc's pageAtIndex:(itemPageNo))
110  #注釈を削除
111  (ocidPdfPage's setDisplaysAnnotations:(true))
112  set ocidAnotationArray to ocidPdfPage's annotations()
113  repeat with itemAnotationArray in ocidAnotationArray
114    (ocidPdfPage's removeAnnotation:(itemAnotationArray))
115  end repeat
116  (ocidPdfPage's setDisplaysAnnotations:(false))
117  (ocidPdfPage's removeAnnotation:(ocidAnotationArray))
118  #CROPサイズを取得しておくpt
119  set ocidPageRect to (ocidPdfPage's boundsForBox:(refMe's kPDFDisplayBoxCropBox))
120  set numPDFPtWidth to refMe's NSWidth(ocidPageRect)
121  set numPDFPtHeight to refMe's NSHeight(ocidPageRect)
122  set ocidPageSize to refMe's NSSize's NSMakeSize(numPDFPtWidth, numPDFPtHeight)
123  #サイズセットする比率
124  set numSetResolution to (numResolution / 72.0) as number
125  #出力イメージピクセルサイズ
126  set numPxWidth to ((numPDFPtWidth * numSetResolution) div 1) as integer
127  set numPxHeigh to ((numPDFPtHeight * numSetResolution) div 1) as integer
128  #今回の処理は仕上がりピクセルサイズを偶数にすることを優先している
129  if ((numPxWidth / 2) mod 1) > 0 then
130    set numPxWidth to numPxWidth + 1
131    set numPxHeigh to numPxHeigh + 1
132  end if
133  if ((numPxHeigh / 2) mod 1) > 0 then
134    set numPxHeigh to numPxHeigh + 1
135  end if
136  #出力用の解像度をセットするためのNSSize'sをピクセルサイズから生成
137  set numSetPtSizeW to (numPxWidth / numSetResolution) as number
138  set numSetPtSizeH to (numPxHeigh / numSetResolution) as number
139  set ocidSetImageSize to refMe's NSSize's NSMakeSize(numSetPtSizeW, numSetPtSizeH)
140  
141  #描画するRECT
142  set ocidDrawRect to refMe's NSRect's NSMakeRect(0, 0, numPxWidth, numPxHeigh)
143  #コピーしてくるRECT=PDFのページPTサイズ
144  set ocidFromRect to refMe's NSRect's NSMakeRect(0, 0, numPDFPtWidth, numPDFPtHeight)
145  #NSDATAに
146  set ocidDataRep to ocidPdfPage's dataRepresentation()
147  #NSDATAに格納
148  set ocidPageData to (refMe's NSData's alloc()'s initWithData:(ocidDataRep))
149  #NSPDFImageRepに変換
150  set ocidPagePep to (refMe's NSPDFImageRep's alloc()'s initWithData:(ocidDataRep))
151  #念の為最初のページをセット
152  (ocidPagePep's setCurrentPage:(0))
153  #出力される画像(アートボード)=解像度換算のピクセルサイズ
154  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 NSDeviceRGBColorSpace) bitmapFormat:(refMe's NSBitmapFormatAlphaFirst) bytesPerRow:(0) bitsPerPixel:(32))
155  
156  ########################
157  #NSGraphicsContext初期化
158  set appGraphicsContext to (refMe's NSGraphicsContext)
159  #編集開始
160  appGraphicsContext's saveGraphicsState()
161  #アートボード画像読み込み
162  set ocidContext to (appGraphicsContext's graphicsContextWithBitmapImageRep:(ocidAardboardRep))
163  #アートボード画像をContextとしてセット
164  (appGraphicsContext's setCurrentContext:(ocidContext))
165  #PDFpageRepをアートボードにペースト
166  (ocidPagePep's drawInRect:(ocidDrawRect) fromRect:(ocidFromRect) operation:(refMe's NSCompositeSourceOver) fraction:(1.0) respectFlipped:(false) hints:(missing value))
167  #編集終了
168  appGraphicsContext's restoreGraphicsState()
169  
170  ##################
171  #変換設定 オプション設定
172  set ocidPropertyDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
173  (ocidPropertyDict's setObject:1 forKey:(refMe's NSImageCompressionFactor))
174  (ocidPropertyDict's setObject:(refMe's NSTIFFCompressionNone) forKey:(refMe's NSImageCompressionMethod))
175  set ocidFileType to (refMe's NSBitmapImageFileTypeTIFF)
176  set strExtension to "tiff"
177  #サイズ指定 ここで解像度が決まる
178  (ocidAardboardRep's setSize:(ocidSetImageSize))
179  #変換
180  set ocidNSInlineData to (ocidAardboardRep's representationUsingType:(ocidFileType) |properties|:(ocidPropertyDict))
181  
182  ###################
183  ###ページ数連番ゼロサプレス
184  set strZeroAdd to ("0000" & (itemPageNo + 1)) as text
185  set strPageNO to text -4 through -1 of strZeroAdd as text
186  ###ファイル名に整形して
187  set strSaveImageFileName to ("" & strPrefixName & "." & strPageNO & "." & strExtension & "") as text
188  ####保存先URL
189  set ocidSaveFilePathURL to (ocidSaveFileDirPathURL's URLByAppendingPathComponent:strSaveImageFileName isDirectory:false)
190  #####保存
191  set boolDone to (ocidNSInlineData's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error| :(reference))
192  #一応リセットしたりして
193  set ocidAardboardRep to ""
194  set ocidNSInlineData to ""
195  set ocidBmpImageRep to ""
196  set ocidOsDispatchData to ""
197  set ocidPageImageData to ""
198end repeat
199#保存先を開く
200set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
201set boolDone to appSharedWorkspace's openURL:(ocidSaveFileDirPathURL)
202
203return "処理終了"
204
AppleScriptで生成しました

|

PDFの各ページを個別の画像ファイルにする(注釈除)修正版


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.6"
use framework "Foundation"
use framework "PDFKit"
use framework "Quartz"
use scripting additions


property refMe : a reference to current application
set appFileManager to refMe's NSFileManager's defaultManager()


##################################
#### 文書を開いているか?
##################################
####何が前面か
tell application "System Events"
  set strAppTitile to title of (front process whose frontmost is true)
end tell

set listAliasFilePath to {}
###前面がプレビュー
if strAppTitile is "プレビュー" then
  tell application "Preview"
    tell front document
      set strFilePath to path as text
      set boolMode to modified
      ###変更箇所があるなら保存する
      if boolMode is true then
close saving yes
      else
close saving no
      end if
    end tell
  end tell
  set aliasFilePath to POSIX file strFilePath
  copy aliasFilePath to end of listAliasFilePath
  ###前面がリーダー
else if strAppTitile is "Acrobat Reader" then
  tell application id "com.adobe.Reader"
activate
    ##ファイルパス
    tell active doc
      set aliasFilePath to file alias
    end tell
  end tell
  set strFilePath to (POSIX path of aliasFilePath) as text
  tell application id "com.adobe.Reader"
activate
    set objAvtivDoc to active doc
    tell objAvtivDoc
      set boolMode to modified
      ###変更箇所があるなら保存する
      if boolMode is true then
save
      end if
    end tell
close objAvtivDoc
  end tell
  copy aliasFilePath to end of listAliasFilePath
  ###それ以外ならダイアログ
else
  ##############################
  #####ダイアログを前面に出す
  ##############################
  tell current application
    set strName to name as text
  end tell
  ####スクリプトメニューから実行したら
  if strName is "osascript" then
    tell application "Finder"
activate
    end tell
  else
    tell current application
activate
    end tell
  end if
  #######################################
  #####ファイル選択ダイアログ
  #######################################
  ###ダイアログのデフォルト
  set ocidUserDesktopPath to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
  set aliasDefaultLocation to ocidUserDesktopPath as alias
  tell application "Finder"
  end tell
  set listChooseFileUTI to {"com.adobe.pdf"}
  set strPromptText to "PDFファイルを選んでください" as text
  set aliasFilePath to (choose file with prompt strPromptText default location (aliasDefaultLocation) of type listChooseFileUTI without invisibles, multiple selections allowed and showing package contents) as alias
end if

##############################
#####ダイアログを前面に出す
##############################
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder"
activate
  end tell
else
  tell current application
activate
  end tell
end if
##########################
####ファイル形式ダイアログ
##########################
set listChooser to {"JPEG", "PNG", "BMP", "TIFF"}
try
  set listResponse to (choose from list listChooser with title "選んでください" with prompt "書き出し形式" default items (item 2 of listChooser) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed)
on error
log "エラーしました"
return "エラーしました"
end try
if listResponse is false then
return "キャンセルしました"
end if
set strResponse to (item 1 of listResponse) as text
################################
###カラープロファイルを読み出しておく ファイルはお好みで
set strIccFilePath to "/System/Library/ColorSync/Profiles/sRGB Profile.icc"
set ocidIccFilePathStr to (refMe's NSString's stringWithString:strIccFilePath)
set ocidIccFilePath to ocidIccFilePathStr's stringByStandardizingPath
set ocidIccFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidIccFilePath isDirectory:false)
set ocidProfileData to refMe's NSData's alloc()'s initWithContentsOfURL:ocidIccFilePathURL

####まずはUNIXパスにして
set strFilePath to (POSIX path of aliasFilePath) as text
set ocidFilePathStr to (refMe's NSString's stringWithString:strFilePath)
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath
set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath)
####ファイル名を取得
set ocidFileName to ocidFilePathURL's lastPathComponent()
####ファイル名から拡張子を取っていわゆるベースファイル名を取得
set ocidPrefixName to ocidFileName's stringByDeletingPathExtension
####コンテナディレクトリを取得
set ocidContainerDirURL to ocidFilePathURL's URLByDeletingLastPathComponent()
########################
###保存先ディレクトリを作成
set strPrefixName to ocidPrefixName as text
###ディレクトリ名はお好みで
set ocidSaveDirName to (strPrefixName & "_images") as text
##保存先URL
set ocidSaveFileDirPathURL to (ocidContainerDirURL's URLByAppendingPathComponent:ocidSaveDirName isDirectory:true)
###フォルダ作成
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidAttrDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
(ocidAttrDict's setValue:511 forKey:(refMe's NSFilePosixPermissions))
set listBoolMakeDir to (appFileManager's createDirectoryAtURL:(ocidSaveFileDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference))
#####################
#### 本処理
#####################
####PDFファイルを格納
set ocidActivDoc to (refMe's PDFDocument's alloc()'s initWithURL:ocidFilePathURL)
####書き出し用のPDF
set ocidExpDoc to (refMe's PDFDocument's alloc()'s init())

####################################
####注釈削除
####ページ数
set numPageCnt to ocidActivDoc's pageCount() as integer
###ページ数カウンター
set numCntPageNoJs to 0 as number

repeat numPageCnt times
  ####PDFの対象ページを格納
  set ocidPdfPage to (ocidActivDoc's pageAtIndex:numCntPageNoJs)
(ocidPdfPage's setDisplaysAnnotations:true)
  ###アノテーション注釈を削除
  set ocidAnotationArray to ocidPdfPage's annotations()
  repeat with itemAnotationArray in ocidAnotationArray
(ocidPdfPage's removeAnnotation:itemAnotationArray)
  end repeat
(ocidPdfPage's setDisplaysAnnotations:false)
ocidPdfPage's removeAnnotation:ocidAnotationArray
ocidExpDoc's insertPage:ocidPdfPage atIndex:numCntPageNoJs
  
  set numCntPageNoJs to numCntPageNoJs + 1 as number
end repeat


###################################
####PDFDocumentとNSDataに変換して
set ocidDataRep to ocidExpDoc's dataRepresentation()
####NSdataに格納
set ocidReadPdfData to (refMe's NSData's alloc()'s initWithData:ocidDataRep)
####NSPDFImageRepに変換
set ocidPdfPep to (refMe's NSPDFImageRep's alloc()'s initWithData:ocidReadPdfData)
###ページ数数えて
set numPageCnt to ocidPdfPep's pageCount() as integer
###ページカウントの書き出しファイル用のページ番号
set numCntPageNo to 1 as integer
###ページカウントの初期値JS
set numCntPageNoJs to numCntPageNo - 1 as integer
####################
#####ページ数だけ繰り返し
####################
repeat numPageCnt times
  
  ####順番にページデータを読み出し
(ocidPdfPep's setCurrentPage:numCntPageNoJs)
  ####ページデータをNSIMAGEに格納
  set ocidPageImageData to refMe's NSImage's alloc()'s init()
(ocidPageImageData's addRepresentation:ocidPdfPep)
  ###中間ファイルとしてのTIFFに変換
  set ocidOsDispatchData to ocidPageImageData's TIFFRepresentation()
  ####出力されるBmpImageRepに格納
  set ocidBmpImageRep to (refMe's NSBitmapImageRep's imageRepWithData:ocidOsDispatchData)
  #############################
  #####変換設定 オプション設定
  #############################
  if strResponse is "JPEG" then
    ###可変レコードに
    set ocidPropertyDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
    ###各種値をキーでセットしておく
(ocidPropertyDict's setObject:1 forKey:(refMe's NSImageCompressionFactor))
    set ocidSetColor to refMe's NSColor's whiteColor()
(ocidPropertyDict's setObject:ocidSetColor forKey:(refMe's NSImageFallbackBackgroundColor))
(ocidPropertyDict's setObject:ocidProfileData forKey:(refMe's NSImageColorSyncProfileData))
    ####ファイルタイプの指定
    set ocidFileType to refMe's NSBitmapImageFileTypeJPEG
    ###パスで使う拡張子
    set strExtension to "jpg"
    
  else if strResponse is "PNG" then
    set ocidPropertyDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
(ocidPropertyDict's setObject:true forKey:(refMe's NSImageInterlaced))
    ####ガンマ値が不要な場合はコメントアウト
(ocidPropertyDict's setObject:0.45455 forKey:(refMe's NSImageGamma))
(ocidPropertyDict's setObject:ocidProfileData forKey:(refMe's NSImageColorSyncProfileData))
    set ocidFileType to refMe's NSBitmapImageFileTypePNG
    set strExtension to "png"
    
  else if strResponse is "BMP" then
    set ocidPropertyDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
    set ocidFileType to refMe's NSBitmapImageFileTypeBMP
    set strExtension to "bmp"
    
  else if strResponse is "TIFF" then
    set ocidPropertyDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
(ocidPropertyDict's setObject:1 forKey:(refMe's NSImageCompressionFactor))
(ocidPropertyDict's setObject:(refMe's NSTIFFCompressionNone) forKey:(refMe's NSImageCompressionMethod))
(ocidPropertyDict's setObject:ocidProfileData forKey:(refMe's NSImageColorSyncProfileData))
    set ocidFileType to refMe's NSBitmapImageFileTypeTIFF
    set strExtension to "tiff"
    
  end if
  ##########変換
  set ocidNSInlineData to (ocidBmpImageRep's representationUsingType:ocidFileType |properties|:ocidPropertyDict)
  
  ############################
  #########保存
  ###########################
  ###ページ数連番ゼロパディング
  set strZeroAdd to ("0000" & numCntPageNo) as text
  set strPageNO to text -4 through -1 of strZeroAdd as text
  ###ファイル名に整形して
  set strSaveImageFileName to ("" & strPrefixName & "." & strPageNO & "." & strExtension & "") as text
  ####保存先URL
  set ocidSaveFilePathURL to (ocidSaveFileDirPathURL's URLByAppendingPathComponent:strSaveImageFileName isDirectory:false)
  #####保存
  set boolDone to (ocidNSInlineData's writeToURL:ocidSaveFilePathURL options:(refMe's NSDataWritingAtomic) |error|:(reference))
  
  #############カウントアップ
  set numCntPageNoJs to numCntPageNoJs + 1 as integer
  set numCntPageNo to numCntPageNo + 1 as integer
  ###データ解放
  set ocidNSInlineData to ""
  set ocidPageImageData to ""
  set ocidOsDispatchData to ""
  set ocidBmpImageRep to ""
end repeat


return "処理終了"



|

PDFの各ページを個別の画像ファイルにする(注釈含)修正版


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.6"
use framework "Foundation"
use framework "PDFKit"
use framework "Quartz"
use scripting additions


property refMe : a reference to current application
set appFileManager to refMe's NSFileManager's defaultManager()


##################################
#### 文書を開いているか?
##################################
####何が前面か
tell application "System Events"
  set strAppTitile to title of (front process whose frontmost is true)
end tell

set listAliasFilePath to {}
###前面がプレビュー
if strAppTitile is "プレビュー" then
  tell application "Preview"
    tell front document
      set strFilePath to path as text
      set boolMode to modified
      ###変更箇所があるなら保存する
      if boolMode is true then
close saving yes
      else
close saving no
      end if
    end tell
  end tell
  set aliasFilePath to POSIX file strFilePath
  copy aliasFilePath to end of listAliasFilePath
  ###前面がリーダー
else if strAppTitile is "Acrobat Reader" then
  tell application id "com.adobe.Reader"
activate
    ##ファイルパス
    tell active doc
      set aliasFilePath to file alias
    end tell
  end tell
  set strFilePath to (POSIX path of aliasFilePath) as text
  tell application id "com.adobe.Reader"
activate
    set objAvtivDoc to active doc
    tell objAvtivDoc
      set boolMode to modified
      ###変更箇所があるなら保存する
      if boolMode is true then
save
      end if
    end tell
close objAvtivDoc
  end tell
  copy aliasFilePath to end of listAliasFilePath
  ###それ以外ならダイアログ
else
  ##############################
  #####ダイアログを前面に出す
  ##############################
  tell current application
    set strName to name as text
  end tell
  ####スクリプトメニューから実行したら
  if strName is "osascript" then
    tell application "Finder"
activate
    end tell
  else
    tell current application
activate
    end tell
  end if
  #######################################
  #####ファイル選択ダイアログ
  #######################################
  ###ダイアログのデフォルト
  set ocidUserDesktopPath to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
  set aliasDefaultLocation to ocidUserDesktopPath as alias
  tell application "Finder"
  end tell
  set listChooseFileUTI to {"com.adobe.pdf"}
  set strPromptText to "PDFファイルを選んでください" as text
  set aliasFilePath to (choose file with prompt strPromptText default location (aliasDefaultLocation) of type listChooseFileUTI without invisibles, multiple selections allowed and showing package contents) as alias
end if

##############################
#####ダイアログを前面に出す
##############################
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder"
activate
  end tell
else
  tell current application
activate
  end tell
end if
##########################
####ファイル形式ダイアログ
##########################
set listChooser to {"JPEG", "PNG", "BMP", "TIFF"}
try
  set listResponse to (choose from list listChooser with title "選んでください" with prompt "書き出し形式" default items (item 2 of listChooser) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed)
on error
log "エラーしました"
return "エラーしました"
end try
if listResponse is false then
return "キャンセルしました"
end if
set strResponse to (item 1 of listResponse) as text
################################
###カラープロファイルを読み出しておく ファイルはお好みで
set strIccFilePath to "/System/Library/ColorSync/Profiles/sRGB Profile.icc"
set ocidIccFilePathStr to (refMe's NSString's stringWithString:strIccFilePath)
set ocidIccFilePath to ocidIccFilePathStr's stringByStandardizingPath
set ocidIccFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidIccFilePath isDirectory:false)
set ocidProfileData to refMe's NSData's alloc()'s initWithContentsOfURL:ocidIccFilePathURL


####まずはUNIXパスにして
set strFilePath to (POSIX path of aliasFilePath) as text
set ocidFilePathStr to (refMe's NSString's stringWithString:strFilePath)
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath
set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath)
####ファイル名を取得
set ocidFileName to ocidFilePathURL's lastPathComponent()
####ファイル名から拡張子を取っていわゆるベースファイル名を取得
set ocidPrefixName to ocidFileName's stringByDeletingPathExtension
####コンテナディレクトリを取得
set ocidContainerDirURL to ocidFilePathURL's URLByDeletingLastPathComponent()
########################
###保存先ディレクトリを作成
set strPrefixName to ocidPrefixName as text
###ディレクトリ名はお好みで
set ocidSaveDirName to (strPrefixName & "_images") as text
##保存先URL
set ocidSaveFileDirPathURL to (ocidContainerDirURL's URLByAppendingPathComponent:ocidSaveDirName isDirectory:true)
###フォルダ作成
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidAttrDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
(ocidAttrDict's setValue:511 forKey:(refMe's NSFilePosixPermissions))
set listBoolMakeDir to (appFileManager's createDirectoryAtURL:(ocidSaveFileDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference))
#####################
#### 本処理
#####################
####PDFファイルを格納
set ocidActivDoc to (refMe's PDFDocument's alloc()'s initWithURL:ocidFilePathURL)
####PDFDocumentとNSDataに変換して
set ocidDataRep to ocidActivDoc's dataRepresentation()
####NSdataに格納
set ocidReadPdfData to (refMe's NSData's alloc()'s initWithData:ocidDataRep)
####NSPDFImageRepに変換
set ocidPdfPep to (refMe's NSPDFImageRep's alloc()'s initWithData:ocidReadPdfData)
###ページ数数えて
set numPageCnt to ocidPdfPep's pageCount() as integer
###ページカウントの書き出しファイル用のページ番号
set numCntPageNo to 1 as integer
###ページカウントの初期値JS
set numCntPageNoJs to numCntPageNo - 1 as integer
####################
#####ページ数だけ繰り返し
####################
repeat numPageCnt times
  ####順番にページデータを読み出し
(ocidPdfPep's setCurrentPage:numCntPageNoJs)
  ####ページデータをNSIMAGEに格納
  set ocidPageImageData to refMe's NSImage's alloc()'s init()
(ocidPageImageData's addRepresentation:ocidPdfPep)
  ###中間ファイルとしてのTIFFに変換
  set ocidOsDispatchData to ocidPageImageData's TIFFRepresentation()
  ####出力されるBmpImageRepに格納
  set ocidBmpImageRep to (refMe's NSBitmapImageRep's imageRepWithData:ocidOsDispatchData)
  #############################
  #####変換設定 オプション設定
  #############################
  if strResponse is "JPEG" then
    ###可変レコードに
    set ocidPropertyDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
    ###各種値をキーでセットしておく
(ocidPropertyDict's setObject:1 forKey:(refMe's NSImageCompressionFactor))
    set ocidSetColor to refMe's NSColor's whiteColor()
(ocidPropertyDict's setObject:ocidSetColor forKey:(refMe's NSImageFallbackBackgroundColor))
(ocidPropertyDict's setObject:ocidProfileData forKey:(refMe's NSImageColorSyncProfileData))
    ####ファイルタイプの指定
    set ocidFileType to refMe's NSBitmapImageFileTypeJPEG
    ###パスで使う拡張子
    set strExtension to "jpg"
    
  else if strResponse is "PNG" then
    set ocidPropertyDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
(ocidPropertyDict's setObject:true forKey:(refMe's NSImageInterlaced))
    ####ガンマ値が不要な場合はコメントアウト
(ocidPropertyDict's setObject:0.45455 forKey:(refMe's NSImageGamma))
(ocidPropertyDict's setObject:ocidProfileData forKey:(refMe's NSImageColorSyncProfileData))
    set ocidFileType to refMe's NSBitmapImageFileTypePNG
    set strExtension to "png"
    
  else if strResponse is "BMP" then
    set ocidPropertyDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
    set ocidFileType to refMe's NSBitmapImageFileTypeBMP
    set strExtension to "bmp"
    
  else if strResponse is "TIFF" then
    set ocidPropertyDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
(ocidPropertyDict's setObject:1 forKey:(refMe's NSImageCompressionFactor))
(ocidPropertyDict's setObject:(refMe's NSTIFFCompressionNone) forKey:(refMe's NSImageCompressionMethod))
(ocidPropertyDict's setObject:ocidProfileData forKey:(refMe's NSImageColorSyncProfileData))
    set ocidFileType to refMe's NSBitmapImageFileTypeTIFF
    set strExtension to "tiff"
    
  end if
  ##########変換
  set ocidNSInlineData to (ocidBmpImageRep's representationUsingType:ocidFileType |properties|:ocidPropertyDict)
  
  ############################
  #########保存
  ###########################
  ###ページ数連番ゼロパディング
  set strZeroAdd to ("000" & numCntPageNo) as text
  set strPageNO to text -3 through -1 of strZeroAdd as text
  ###ファイル名に整形して
  set strSaveImageFileName to ("" & strPrefixName & "." & strPageNO & "." & strExtension & "") as text
  ####保存先URL
  set ocidSaveFilePathURL to (ocidSaveFileDirPathURL's URLByAppendingPathComponent:strSaveImageFileName isDirectory:false)
  #####保存
  set boolDone to (ocidNSInlineData's writeToURL:ocidSaveFilePathURL options:(refMe's NSDataWritingAtomic) |error|:(reference))
  
  #############カウントアップ
  set numCntPageNoJs to numCntPageNoJs + 1 as integer
  set numCntPageNo to numCntPageNo + 1 as integer
  ###データ解放
  set ocidNSInlineData to ""
  set ocidPageImageData to ""
  set ocidOsDispatchData to ""
  set ocidBmpImageRep to ""
end repeat


return "処理終了"



|

PDF内容を画像化したPDFにする(TIFF圧縮無し)ドロップレット修正版


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
# use framework "Quartz"
# use framework "CoreImage"
use framework "AppKit"
use framework "PDFKit"
use scripting additions

property refMe : a reference to current application

on run
  
  set aliasIconPass to (POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/MultipleItemsIcon.icns") as alias
  set strDialogText to "ドロップしても利用できます"
  set strTitleText to "PDFファイルを選んでください"
  set listButton to {"ファイルを選びます", "キャンセル"} as list
display dialog strDialogText buttons listButton default button 1 cancel button 2 with title strTitleText with icon aliasIconPass giving up after 1 with hidden answer
  
  set aliasDefaultLocation to (path to desktop folder from user domain) as alias
  set listChooseFileUTI to {"com.adobe.pdf"}
  set strPromptText to "ファイルを選んでください" as text
  set strPromptMes to "PDFファイルを選んでください" as text
  set listAliasFilePath to (choose file strPromptMes with prompt strPromptText default location (aliasDefaultLocation) of type listChooseFileUTI with showing package contents, invisibles and multiple selections allowed) as list
  
  -->値をOpenに渡たす
open listAliasFilePath
end run


on open listAliasFilePath
  repeat with itemFilePath in listAliasFilePath
    tell application "Finder"
      set strKindName to (kind of itemFilePath) as text
    end tell
    if strKindName is "フォルダ" then
display alert "フォルダは処理しません"
return "フォルダは処理しません"
    else if strKindName is not "PDF書類" then
display alert "PDF書類専用です"
return "PDF書類専用です"
    end if
  end repeat
  ################################
  ###カラープロファイルを読み出しておく ファイルはお好みで
  set strIccFilePath to "/System/Library/ColorSync/Profiles/sRGB Profile.icc"
  
  set ocidIccFilePathStr to (refMe's NSString's stringWithString:strIccFilePath)
  set ocidIccFilePath to ocidIccFilePathStr's stringByStandardizingPath
  set ocidIccFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidIccFilePath isDirectory:false)
  set ocidProfileData to refMe's NSData's alloc()'s initWithContentsOfURL:ocidIccFilePathURL
  
  ##########################
  ####ファイルの数だけ繰り返し
  ##########################
  
  repeat with itemAliasFilePath in listAliasFilePath
    ####まずはUNIXパスにして
    set strFilePath to (POSIX path of itemAliasFilePath) as text
    set ocidFilePathStr to (refMe's NSString's stringWithString:strFilePath)
    set ocidFilePath to ocidFilePathStr's stringByStandardizingPath
    set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath)
    ####ファイル名を取得
    set ocidFileName to ocidFilePathURL's lastPathComponent()
    ####ファイル名から拡張子を取っていわゆるベースファイル名を取得
    set strPrefixName to ocidFileName's stringByDeletingPathExtension as text
    ####コンテナディレクトリを取得
    set ocidContainerDirURL to ocidFilePathURL's URLByDeletingLastPathComponent()
    ###################################
    #####保存ダイアログ
    ###################################
    ###保存ファイル名
    set strSaveFileName to (strPrefixName & ".imaged.pdf") as text
    ###
    set ocidSaveFilePathURL to (ocidContainerDirURL's URLByAppendingPathComponent:strSaveFileName isDirectory:true)
    ###
    set aliasSaveFilePath to ocidSaveFilePathURL as «class furl»
    ####パス
    set strSaveFilePath to POSIX path of aliasSaveFilePath as text
    ####保存先URL
    set ocidSaveFilePathStr to (refMe's NSString's stringWithString:strSaveFilePath)
    set ocidSaveFilePath to ocidSaveFilePathStr's stringByStandardizingPath
    set ocidSaveFilePathURL to (refMe's NSURL's fileURLWithPath:ocidSaveFilePath)
    
    #####################
    #### 本処理
    #####################
    #読み込み
    set ocidActiveDoc to (refMe's PDFDocument's alloc()'s initWithURL:(ocidFilePathURL))
    #ページ数数えて
    set numCntPage to ocidActiveDoc's pageCount() as integer
    #ページ数繰り返し
    repeat with itemIntNo from 0 to (numCntPage - 1) by 1
      #ページ読み込み
      set ocidPdfPage to (ocidActiveDoc's pageAtIndex:(itemIntNo))
      #DATAに変換
      set ocidPdfPageData to ocidPdfPage's dataRepresentation()
      set ocidPageData to (refMe's NSData's alloc()'s initWithData:(ocidPdfPageData))
      #NSPDFImageRepに変換
      set ocidPdfPep to (refMe's NSPDFImageRep's alloc()'s initWithData:(ocidPageData))
(ocidPdfPep's setCurrentPage:0)
      #NSimageに変換
      set ocidPageImage to refMe's NSImage's alloc()'s init()
(ocidPageImage's addRepresentation:(ocidPdfPep))
      #NSBitmapImageRepに変換
      set ocidPageDispatchData to ocidPageImage's TIFFRepresentation()
      set ocidPageBmpImageRep to (refMe's NSBitmapImageRep's imageRepWithData:(ocidPageDispatchData))
      #TIFFにする この方法だとプロパティがうまく反映されない
      set ocidPropertyDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
      #set ocidNum to (refMe's NSNumber's numberWithFloat:(0.5))
      #(ocidPropertyDict's setObject:(ocidNum) forKey:(refMe's NSImageCompressionFactor))
      #(ocidPropertyDict's setObject:(refMe's NSTIFFCompressionLZW) forKey:(refMe's NSImageCompressionMethod))
(ocidPropertyDict's setObject:(refMe's NSTIFFCompressionNone) forKey:(refMe's NSImageCompressionMethod))
(ocidPropertyDict's setObject:(ocidProfileData) forKey:(refMe's NSImageColorSyncProfileData))
      set ocidFileType to (refMe's NSBitmapImageFileTypeTIFF)
      set ocidPageInlineData to (ocidPageBmpImageRep's representationUsingType:(ocidFileType) |properties|:(ocidPropertyDict))
      #####TIFFをPDFpageに
      set ocidPageImageData to (refMe's NSImage's alloc()'s initWithData:(ocidPageInlineData))
      set ocdiAddPage to (refMe's PDFPage's alloc()'s initWithImage:(ocidPageImageData))
(ocidActiveDoc's insertPage:(ocdiAddPage) atIndex:(itemIntNo))
      #####元あったページを削除する
(ocidActiveDoc's removePageAtIndex:(itemIntNo + 1))
    end repeat
    
    ####保存
    set ocidOptionDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
(ocidOptionDict's setValue:(missing value) forKey:(refMe's PDFDocumentOwnerPasswordOption))
(ocidOptionDict's setValue:(missing value) forKey:(refMe's PDFDocumentUserPasswordOption))
    set boolDone to (ocidActiveDoc's writeToURL:(ocidSaveFilePathURL) withOptions:(ocidOptionDict))
    
  end repeat
end open


|

PDF内容を画像化したPDFにする(TIFF圧縮無し) 修正版


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
#use framework "Quartz"
#use framework "CoreImage"
use framework "AppKit"
use framework "PDFKit"
use scripting additions

property refMe : a reference to current application
set appFileManager to refMe's NSFileManager's defaultManager()

##################################
#### 文書を開いているか?
##################################
####何が前面か
tell application "System Events"
  set strAppTitile to title of (front process whose frontmost is true)
end tell

set listAliasFilePath to {}
###前面がプレビュー
if strAppTitile is "プレビュー" then
  tell application "Preview"
    tell front document
      set strFilePath to path as text
      set boolMode to modified
      ###変更箇所があるなら保存する
      if boolMode is true then
close saving yes
      else
close saving no
      end if
    end tell
  end tell
  set aliasFilePath to POSIX file strFilePath
  copy aliasFilePath to end of listAliasFilePath
  ###前面がリーダー
else if strAppTitile is "Acrobat Reader" then
  tell application id "com.adobe.Reader"
activate
    ##ファイルパス
    tell active doc
      set aliasFilePath to file alias
    end tell
  end tell
  set strFilePath to (POSIX path of aliasFilePath) as text
  tell application id "com.adobe.Reader"
activate
    set objAvtivDoc to active doc
    tell objAvtivDoc
      set boolMode to modified
      ###変更箇所があるなら保存する
      if boolMode is true then
save
      end if
    end tell
close objAvtivDoc
  end tell
  copy aliasFilePath to end of listAliasFilePath
  ###それ以外ならダイアログ
else
  ##############################
  #####ダイアログを前面に出す
  ##############################
  tell current application
    set strName to name as text
  end tell
  ####スクリプトメニューから実行したら
  if strName is "osascript" then
    tell application "Finder"
activate
    end tell
  else
    tell current application
activate
    end tell
  end if
  #######################################
  #####ファイル選択ダイアログ
  #######################################
  ###ダイアログのデフォルト
  set ocidUserDesktopPath to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
  set aliasDefaultLocation to ocidUserDesktopPath as alias
  tell application "Finder"
  end tell
  set listChooseFileUTI to {"com.adobe.pdf"}
  set strPromptText to "PDFファイルを選んでください" as text
  set aliasFilePath to (choose file with prompt strPromptText default location (aliasDefaultLocation) of type listChooseFileUTI without invisibles, multiple selections allowed and showing package contents) as alias
end if



################################
###カラープロファイルを読み出しておく ファイルはお好みで
set strIccFilePath to "/System/Library/ColorSync/Profiles/sRGB Profile.icc"

set ocidIccFilePathStr to (refMe's NSString's stringWithString:strIccFilePath)
set ocidIccFilePath to ocidIccFilePathStr's stringByStandardizingPath
set ocidIccFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidIccFilePath isDirectory:false)
set ocidProfileData to refMe's NSData's alloc()'s initWithContentsOfURL:ocidIccFilePathURL



####まずはUNIXパスにして
set strFilePath to (POSIX path of aliasFilePath) as text
set ocidFilePathStr to (refMe's NSString's stringWithString:strFilePath)
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath
set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath)
####ファイル名を取得
set ocidFileName to ocidFilePathURL's lastPathComponent()
####ファイル名から拡張子を取っていわゆるベースファイル名を取得
set ocidPrefixName to ocidFileName's stringByDeletingPathExtension
####コンテナディレクトリを取得
set ocidContainerDirURL to ocidFilePathURL's URLByDeletingLastPathComponent()
###################################
#####保存ダイアログ
###################################
###ファイル名
set strPrefixName to ocidPrefixName as text
###拡張子
###同じ拡張子の場合
##set strFileExtension to ocidFileExtension as text
###拡張子変える場合
set strFileExtension to "pdf"
###ダイアログに出すファイル名
set strDefaultName to (strPrefixName & ".画像化済." & strFileExtension) as text
set strPromptText to "名前を決めてください"
set strPromptMes to "名前を決めてください"
###選んだファイルの同階層をデフォルトの場合
set aliasDefaultLocation to ocidContainerDirURL as alias

tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder"
activate
  end tell
else
  tell current application
activate
  end tell
end if

####ファイル名ダイアログ
####実在しない『はず』なのでas «class furl»で
set aliasSaveFilePath to (choose file name strPromptMes default location aliasDefaultLocation default name strDefaultName with prompt strPromptText) as «class furl»
####UNIXパス
set strSaveFilePath to POSIX path of aliasSaveFilePath as text
####ドキュメントのパスをNSString
set ocidSaveFilePath to refMe's NSString's stringWithString:strSaveFilePath
####ドキュメントのパスをNSURLに
set ocidSaveFilePathURL to refMe's NSURL's fileURLWithPath:ocidSaveFilePath
###拡張子取得
set strFileExtensionName to ocidSaveFilePathURL's pathExtension() as text
###ダイアログで拡張子を取っちゃった時対策
if strFileExtensionName is not strFileExtension then
  set ocidSaveFilePathURL to ocidSaveFilePathURL's URLByAppendingPathExtension:strFileExtension
end if
#####################
#### 本処理
#####################
#読み込み
set ocidActiveDoc to refMe's PDFDocument's alloc()'s initWithURL:(ocidFilePathURL)
#ページ数数えて
set numCntPage to ocidActiveDoc's pageCount() as integer
#ページ数繰り返し
repeat with itemIntNo from 0 to (numCntPage - 1) by 1
  #ページ読み込み
  set ocidPdfPage to (ocidActiveDoc's pageAtIndex:(itemIntNo))
  #DATAに変換
  set ocidPdfPageData to ocidPdfPage's dataRepresentation()
  set ocidPageData to (refMe's NSData's alloc()'s initWithData:(ocidPdfPageData))
  #NSPDFImageRepに変換
  set ocidPdfPep to (refMe's NSPDFImageRep's alloc()'s initWithData:(ocidPageData))
(ocidPdfPep's setCurrentPage:0)
  #NSimageに変換
  set ocidPageImage to refMe's NSImage's alloc()'s init()
(ocidPageImage's addRepresentation:(ocidPdfPep))
  #NSBitmapImageRepに変換
  set ocidPageDispatchData to ocidPageImage's TIFFRepresentation()
  set ocidPageBmpImageRep to (refMe's NSBitmapImageRep's imageRepWithData:(ocidPageDispatchData))
  #TIFFにする この方法だとプロパティがうまく反映されない
  set ocidPropertyDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
  #set ocidNum to (refMe's NSNumber's numberWithFloat:(0.5))
  #(ocidPropertyDict's setObject:(ocidNum) forKey:(refMe's NSImageCompressionFactor))
  #(ocidPropertyDict's setObject:(refMe's NSTIFFCompressionLZW) forKey:(refMe's NSImageCompressionMethod))
(ocidPropertyDict's setObject:(refMe's NSTIFFCompressionNone) forKey:(refMe's NSImageCompressionMethod))
(ocidPropertyDict's setObject:(ocidProfileData) forKey:(refMe's NSImageColorSyncProfileData))
  set ocidFileType to (refMe's NSBitmapImageFileTypeTIFF)
  set ocidPageInlineData to (ocidPageBmpImageRep's representationUsingType:(ocidFileType) |properties|:(ocidPropertyDict))
  #####TIFFをPDFpageに
  set ocidPageImageData to (refMe's NSImage's alloc()'s initWithData:(ocidPageInlineData))
  set ocdiAddPage to (refMe's PDFPage's alloc()'s initWithImage:(ocidPageImageData))
(ocidActiveDoc's insertPage:(ocdiAddPage) atIndex:(itemIntNo))
  #####元あったページを削除する
(ocidActiveDoc's removePageAtIndex:(itemIntNo + 1))
  
end repeat

####保存
set ocidOptionDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidOptionDict's setValue:(missing value) forKey:(refMe's PDFDocumentOwnerPasswordOption)
ocidOptionDict's setValue:(missing value) forKey:(refMe's PDFDocumentUserPasswordOption)
set boolDone to ocidActiveDoc's writeToURL:(ocidSaveFilePathURL) withOptions:(ocidOptionDict)




|

PDF内容を画像化したPDFにする(JPG圧縮) 修正版


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
#use framework "Quartz"
#use framework "CoreImage"
use framework "AppKit"
use framework "PDFKit"
use scripting additions

property refMe : a reference to current application
set appFileManager to refMe's NSFileManager's defaultManager()

##################################
#### 文書を開いているか?
##################################
####何が前面か
tell application "System Events"
  set strAppTitile to title of (front process whose frontmost is true)
end tell

set listAliasFilePath to {}
###前面がプレビュー
if strAppTitile is "プレビュー" then
  tell application "Preview"
    tell front document
      set strFilePath to path as text
      set boolMode to modified
      ###変更箇所があるなら保存する
      if boolMode is true then
close saving yes
      else
close saving no
      end if
    end tell
  end tell
  set aliasFilePath to POSIX file strFilePath
  copy aliasFilePath to end of listAliasFilePath
  ###前面がリーダー
else if strAppTitile is "Acrobat Reader" then
  tell application id "com.adobe.Reader"
activate
    ##ファイルパス
    tell active doc
      set aliasFilePath to file alias
    end tell
  end tell
  set strFilePath to (POSIX path of aliasFilePath) as text
  tell application id "com.adobe.Reader"
activate
    set objAvtivDoc to active doc
    tell objAvtivDoc
      set boolMode to modified
      ###変更箇所があるなら保存する
      if boolMode is true then
save
      end if
    end tell
close objAvtivDoc
  end tell
  copy aliasFilePath to end of listAliasFilePath
  ###それ以外ならダイアログ
else
  ##############################
  #####ダイアログを前面に出す
  ##############################
  tell current application
    set strName to name as text
  end tell
  ####スクリプトメニューから実行したら
  if strName is "osascript" then
    tell application "Finder"
activate
    end tell
  else
    tell current application
activate
    end tell
  end if
  #######################################
  #####ファイル選択ダイアログ
  #######################################
  ###ダイアログのデフォルト
  set ocidUserDesktopPath to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
  set aliasDefaultLocation to ocidUserDesktopPath as alias
  tell application "Finder"
  end tell
  set listChooseFileUTI to {"com.adobe.pdf"}
  set strPromptText to "PDFファイルを選んでください" as text
  set aliasFilePath to (choose file with prompt strPromptText default location (aliasDefaultLocation) of type listChooseFileUTI without invisibles, multiple selections allowed and showing package contents) as alias
end if



################################
###カラープロファイルを読み出しておく ファイルはお好みで
set strIccFilePath to "/System/Library/ColorSync/Profiles/sRGB Profile.icc"
set ocidIccFilePathStr to (refMe's NSString's stringWithString:strIccFilePath)
set ocidIccFilePath to ocidIccFilePathStr's stringByStandardizingPath
set ocidIccFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidIccFilePath isDirectory:false)
set ocidProfileData to refMe's NSData's alloc()'s initWithContentsOfURL:ocidIccFilePathURL

####まずはUNIXパスにして
set strFilePath to (POSIX path of aliasFilePath) as text
set ocidFilePathStr to (refMe's NSString's stringWithString:strFilePath)
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath
set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath)
####ファイル名を取得
set ocidFileName to ocidFilePathURL's lastPathComponent()
####ファイル名から拡張子を取っていわゆるベースファイル名を取得
set ocidPrefixName to ocidFileName's stringByDeletingPathExtension
####コンテナディレクトリを取得
set ocidContainerDirURL to ocidFilePathURL's URLByDeletingLastPathComponent()
###################################
#####保存ダイアログ
###################################
###ファイル名
set strPrefixName to ocidPrefixName as text
###拡張子
###同じ拡張子の場合
##set strFileExtension to ocidFileExtension as text
###拡張子変える場合
set strFileExtension to "pdf"
###ダイアログに出すファイル名
set strDefaultName to (strPrefixName & ".画像化済." & strFileExtension) as text
set strPromptText to "名前を決めてください"
set strPromptMes to "名前を決めてください"
###選んだファイルの同階層をデフォルトの場合
set aliasDefaultLocation to ocidContainerDirURL as alias

tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder"
activate
  end tell
else
  tell current application
activate
  end tell
end if

####ファイル名ダイアログ
####実在しない『はず』なのでas «class furl»で
set aliasSaveFilePath to (choose file name strPromptMes default location aliasDefaultLocation default name strDefaultName with prompt strPromptText) as «class furl»
####UNIXパス
set strSaveFilePath to POSIX path of aliasSaveFilePath as text
####ドキュメントのパスをNSString
set ocidSaveFilePath to refMe's NSString's stringWithString:strSaveFilePath
####ドキュメントのパスをNSURLに
set ocidSaveFilePathURL to refMe's NSURL's fileURLWithPath:ocidSaveFilePath
###拡張子取得
set strFileExtensionName to ocidSaveFilePathURL's pathExtension() as text
###ダイアログで拡張子を取っちゃった時対策
if strFileExtensionName is not strFileExtension then
  set ocidSaveFilePathURL to ocidSaveFilePathURL's URLByAppendingPathExtension:strFileExtension
end if
###################################
#####圧縮率選択ダイアログ
###################################
set listCmpNo to {"1.0", 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.3, 0.2, 0.1, "0.0"} as list
###ダイアログを前面に出す
set strName to (name of current application) as text
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
###
set strTitle to ("選んでください") as text
set strPrompt to ("圧縮率指定\n1.0が高画質\n0.0が最高圧縮") as text
try
  set listResponse to (choose from list listCmpNo with title strTitle with prompt strPrompt default items (item 4 of listCmpNo) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) as list
on error
log "エラーしました"
return "エラーしました"
end try
if (item 1 of listResponse) is false then
return "キャンセルしましたA"
else if (item 1 of listResponse) is "キャンセル" then
return "キャンセルしましたB"
else
  set strResponse to (item 1 of listResponse) as text
end if
set numSetValue to strResponse as number

#####################
#### 本処理
#####################
#読み込み
set ocidActiveDoc to refMe's PDFDocument's alloc()'s initWithURL:(ocidFilePathURL)
#ページ数数えて
set numCntPage to ocidActiveDoc's pageCount() as integer
#ページ数繰り返し
repeat with itemIntNo from 0 to (numCntPage - 1) by 1
  #ページ読み込み
  set ocidPdfPage to (ocidActiveDoc's pageAtIndex:(itemIntNo))
  #DATAに変換
  set ocidPdfPageData to ocidPdfPage's dataRepresentation()
  set ocidPageData to (refMe's NSData's alloc()'s initWithData:(ocidPdfPageData))
  #NSPDFImageRepに変換
  set ocidPdfPep to (refMe's NSPDFImageRep's alloc()'s initWithData:(ocidPageData))
(ocidPdfPep's setCurrentPage:0)
  #NSimageに変換
  set ocidPageImage to refMe's NSImage's alloc()'s init()
(ocidPageImage's addRepresentation:(ocidPdfPep))
  #NSBitmapImageRepに変換
  set ocidPageDispatchData to ocidPageImage's TIFFRepresentation()
  set ocidPageBmpImageRep to (refMe's NSBitmapImageRep's imageRepWithData:(ocidPageDispatchData))
  #TIFFにする この方法だとプロパティがうまく反映されない
  set ocidPropertyDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
  set ocidNum to (refMe's NSNumber's numberWithFloat:(numSetValue))
(ocidPropertyDict's setObject:(ocidNum) forKey:(refMe's NSImageCompressionFactor))
(ocidPropertyDict's setObject:(ocidProfileData) forKey:(refMe's NSImageColorSyncProfileData))
  set ocidFileType to (refMe's NSBitmapImageFileTypeJPEG)
  set ocidPageInlineData to (ocidPageBmpImageRep's representationUsingType:(ocidFileType) |properties|:(ocidPropertyDict))
  #####TIFFをPDFpageに
  set ocidPageImageData to (refMe's NSImage's alloc()'s initWithData:(ocidPageInlineData))
  set ocdiAddPage to (refMe's PDFPage's alloc()'s initWithImage:(ocidPageImageData))
(ocidActiveDoc's insertPage:(ocdiAddPage) atIndex:(itemIntNo))
  #####元あったページを削除する
(ocidActiveDoc's removePageAtIndex:(itemIntNo + 1))
  
end repeat

####保存
set ocidOptionDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidOptionDict's setValue:(missing value) forKey:(refMe's PDFDocumentOwnerPasswordOption)
ocidOptionDict's setValue:(missing value) forKey:(refMe's PDFDocumentUserPasswordOption)
set boolDone to ocidActiveDoc's writeToURL:(ocidSaveFilePathURL) withOptions:(ocidOptionDict)



|

[pstopdf]postscriptファイルをコンテンツを画像化したPDFにする



「pstopdf」ユーティリティは、macOS 14 Sonomaで廃止されました


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application


###################################
#####ダイアログ
###################################a
tell application "Finder"
  ##set aliasDefaultLocation to container of (path to me) as alias
  set aliasDefaultLocation to (path to desktop folder from user domain) as alias
end tell
set listChooseFileUTI to {"com.adobe.postscript"}
set strPromptText to "ファイルを選んでください" as text
set listAliasFilePath to (choose file with prompt strPromptText default location (aliasDefaultLocation) of type listChooseFileUTI with invisibles and showing package contents without multiple selections allowed) as list
###################################
#####パス処理
###################################
###エリアス
set aliasFilePath to item 1 of listAliasFilePath as alias
###UNIXパス
set strFilePath to POSIX path of aliasFilePath as text
###String
set ocidFilePath to refMe's NSString's stringWithString:(strFilePath)
###NSURL
set ocidFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false
####ファイル名を取得
set ocidFileName to ocidFilePathURL's lastPathComponent()
####拡張子を取得
set ocidFileExtension to ocidFilePathURL's pathExtension()
####ファイル名から拡張子を取っていわゆるベースファイル名を取得
set ocidPrefixName to ocidFileName's stringByDeletingPathExtension
####コンテナディレクトリ
set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()

###################################
#####保存ダイアログ
###################################
###ファイル名
set strPrefixName to ocidPrefixName as text
###拡張子
###同じ拡張子の場合
##set strFileExtension to ocidFileExtension as text
###拡張子変える場合
set strFileExtension to "pdf"
###ダイアログに出すファイル名
set strDefaultName to (strPrefixName & ".output." & strFileExtension) as text
set strPromptText to "名前を決めてください"
###選んだファイルの同階層をデフォルトの場合
set aliasDefaultLocation to ocidContainerDirPathURL as alias
####デスクトップの場合
##set aliasDefaultLocation to (path to desktop folder from user domain) as alias
####ファイル名ダイアログ
####実在しない『はず』なのでas «class furl»で
set aliasSaveFilePath to (choose file name default location aliasDefaultLocation default name strDefaultName with prompt strPromptText) as «class furl»
####UNIXパス
set strSaveFilePath to POSIX path of aliasSaveFilePath as text
####ドキュメントのパスをNSString
set ocidSaveFilePath to refMe's NSString's stringWithString:(strSaveFilePath)
####ドキュメントのパスをNSURLに
set ocidSaveFilePathURL to refMe's NSURL's fileURLWithPath:(ocidSaveFilePath)
###拡張子取得
set strFileExtensionName to ocidSaveFilePathURL's pathExtension() as text
###ダイアログで拡張子を取っちゃった時対策
if strFileExtensionName is not strFileExtension then
  set ocidSaveFilePathURL to ocidSaveFilePathURL's URLByAppendingPathExtension:(strFileExtension)
end if

###################################
#####テンポラリーにPDFを生成する
###################################
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidTemporaryDirURL to appFileManager's temporaryDirectory()
set str8Digit to (random number from 10000000 to 99999999) as text
set strTempFileName to (str8Digit & ".pdf") as text
set ocidTemporaryFileURL to ocidTemporaryDirURL's URLByAppendingPathComponent:(strTempFileName)
set strTemporaryFileURL to (ocidTemporaryFileURL's |path|()) as text
####コマンドパス
set strBinPath to "/usr/bin/pstopdf"
#####コマンド整形
set strCommandText to ("\"" & strBinPath & "\"  \"" & strFilePath & "\" -o \"" & strTemporaryFileURL & "\"")
do shell script strCommandText

###################################
#####本処理
###################################
###カラープロファイルを読み出しておく ファイルはお好みで
set strIccFilePath to "/System/Library/ColorSync/Profiles/sRGB Profile.icc"

set ocidIccFilePathStr to (refMe's NSString's stringWithString:strIccFilePath)
set ocidIccFilePath to ocidIccFilePathStr's stringByStandardizingPath
set ocidIccFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidIccFilePath isDirectory:false)
set ocidProfileData to refMe's NSData's alloc()'s initWithContentsOfURL:ocidIccFilePathURL

###NSDATAに読み込む
set ocidReadPdfData to (refMe's NSData's alloc()'s initWithContentsOfURL:ocidTemporaryFileURL)
####NSPDFImageRepに変換
set ocidPdfPep to (refMe's NSPDFImageRep's alloc()'s initWithData:ocidReadPdfData)
###ページ数数えて
set numPageCnt to ocidPdfPep's pageCount() as integer
###ページカウントの書き出しファイル用のページ番号
set numCntPageNo to 1 as integer
###ページカウントの初期値JS
set numCntPageNoJs to numCntPageNo - 1 as integer
####################
#####保存されるPDF
####################
set ocidSaveDoc to (refMe's PDFDocument's alloc()'s init())
####################
#####ページ数だけ繰り返し
####################
repeat numPageCnt times
  ####順番にページデータを読み出し
(ocidPdfPep's setCurrentPage:numCntPageNoJs)
  ##################################
  ####ページデータをNSIMAGEに格納
  ##################################
  set ocidPageImageData to refMe's NSImage's alloc()'s init()
(ocidPageImageData's addRepresentation:ocidPdfPep)
  ###中間ファイルとしてのTIFFに変換
  set ocidOsDispatchData to ocidPageImageData's TIFFRepresentation()
  ####出力されるBmpImageRepに格納 TIFFのままにする
  set ocidBmpImageRep to (refMe's NSBitmapImageRep's imageRepWithData:ocidOsDispatchData)
  set ocidPropertyDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
(ocidPropertyDict's setObject:1 forKey:(refMe's NSImageCompressionFactor))
(ocidPropertyDict's setObject:(refMe's NSTIFFCompressionNone) forKey:(refMe's NSImageCompressionMethod))
(ocidPropertyDict's setObject:ocidProfileData forKey:(refMe's NSImageColorSyncProfileData))
  set ocidFileType to refMe's NSBitmapImageFileTypeTIFF
  ##########変換
  set ocidNSInlineData to (ocidBmpImageRep's representationUsingType:ocidFileType |properties|:ocidPropertyDict)
  ##################################
  ###変換済みイメージをページデータに
  ##################################
  set ocidPageImageData to refMe's NSImage's alloc()'s initWithData:ocidNSInlineData
  set ocdiAddPage to refMe's PDFPage's alloc()'s initWithImage:ocidPageImageData
ocidSaveDoc's insertPage:ocdiAddPage atIndex:numCntPageNoJs
  #############カウントアップ
  set numCntPageNoJs to numCntPageNoJs + 1 as integer
  ###データ解放
  set ocidNSInlineData to ""
  set ocidPageImageData to ""
  set ocidOsDispatchData to ""
  set ocidBmpImageRep to ""
end repeat

(ocidSaveDoc's writeToURL:ocidSaveFilePathURL)

|

その他のカテゴリー

Accessibility Acrobat Acrobat 2020 Acrobat 2024 Acrobat AddOn Acrobat Annotation Acrobat AppleScript Acrobat ARMDC Acrobat AV2 Acrobat BookMark Acrobat Classic Acrobat DC Acrobat Dialog Acrobat Distiller Acrobat Form Acrobat GentechAI Acrobat JS Acrobat JS Word Search Acrobat Maintenance Acrobat Manifest Acrobat Menu Acrobat Merge Acrobat Open Acrobat PDFPage Acrobat Plugin Acrobat Preferences Acrobat Preflight Acrobat Print Acrobat Python Acrobat Reader Acrobat Reader Localized Acrobat Reference Acrobat Registered Products Acrobat SCA Acrobat SCA Updater Acrobat Sequ Acrobat Sign Acrobat Stamps Acrobat URL List Mac Acrobat URL List Windows Acrobat Watermark Acrobat Windows Acrobat Windows Reader Admin Admin Account Admin Apachectl Admin ConfigCode Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Mobileconfig Admin NetWork Admin Permission Admin Pkg Admin Power Management Admin Printer Admin Printer Basic Admin Printer Custompapers Admin SetUp Admin SMB Admin softwareupdate Admin Support Admin System Information Admin TCC Admin Tools Admin Umask Admin Users Admin Volumes Admin XProtect Adobe Adobe AUSST Adobe Bridge Adobe Documents Adobe FDKO Adobe Fonts Adobe Reference Adobe RemoteUpdateManager Adobe Sap Code AppKit Apple AppleScript AppleScript Duplicate AppleScript entire contents AppleScript List AppleScript ObjC AppleScript Osax AppleScript PDF AppleScript Pictures AppleScript record AppleScript Video Applications AppStore Archive Archive Keka Attributes Automator BackUp Barcode Barcode Decode Barcode QR Bash Basic Basic Path Bluetooth BOX Browser Calendar CD/DVD Choose Chrome Chromedriver CIImage CityCode CloudStorage Color Color NSColor Color NSColorList com.apple.LaunchServices.OpenWith Console Contacts CotEditor CURL current application Date&Time delimiters Desktop Desktop Position Device Diff Disk do shell script Dock Dock Launchpad DropBox Droplet eMail Encode % Encode Decode Encode HTML Entity Encode UTF8 Error EXIFData exiftool ffmpeg File File Name Finder Finder Window Firefox Folder FolderAction Font List FontCollections Fonts Fonts Asset_Font Fonts ATS Fonts Emoji Fonts Maintenance Fonts Morisawa Fonts Python Fonts Variable Foxit GIF github Guide HTML Icon Icon Assets.car Illustrator Image Events ImageOptim Input Dictionary iPhone iWork Javascript Jedit Ω Json Label Language Link locationd lsappinfo m3u8 Mail Map Math Media Media AVAsset Media AVconvert Media AVFoundation Media AVURLAsset Media Movie Media Music Memo Messages Microsoft Microsoft Edge Microsoft Excel Microsoft Fonts Microsoft Office Microsoft Office Link Microsoft OneDrive Microsoft Teams Mouse Music Node Notes NSArray NSArray Sort NSAttributedString NSBezierPath NSBitmapImageRep NSBundle NSCFBoolean NSCharacterSet NSData NSDecimalNumber NSDictionary NSError NSEvent NSFileAttributes NSFileManager NSFileManager enumeratorAtURL NSFont NSFontManager NSGraphicsContext NSGraphicsContext Crop NSImage NSIndex NSKeyedArchiver NSKeyedUnarchiver NSLocale NSMetadataItem NSMutableArray NSMutableDictionary NSMutableString NSNotFound NSNumber NSOpenPanel NSPasteboard NSpoint NSPredicate NSRange NSRect NSRegularExpression NSRunningApplication NSScreen NSSet NSSize NSString NSString stringByApplyingTransform NSStringCompareOptions NSTask NSTimeZone NSUbiquitous NSURL NSURL File NSURLBookmark NSURLComponents NSURLResourceKey NSURLSession NSUserDefaults NSUUID NSView NSWorkspace Numbers OAuth PDF PDF Image2PDF PDF MakePDF PDF nUP PDF Pymupdf PDF Pypdf PDFContext PDFDisplayBox PDFImageRep PDFKit PDFKit Annotation PDFKit AnnotationWidget PDFKit DocumentPermissions PDFKit OCR PDFKit Outline PDFKit Start PDFPage PDFPage Rotation PDFView perl Photoshop PlistBuddy pluginkit plutil postalcode PostScript PowerShell prefPane Preview Python Python eyed3 Python pip QuickLook QuickTime ReadMe Regular Expression Reminders ReName Repeat RTF Safari SaveFile ScreenCapture ScreenSaver Script Editor Script Menu SF Symbols character id SF Symbols Entity Shortcuts Shortcuts Events sips Skype Slack Sound Spotlight sqlite StandardAdditions StationSearch Subtitles LRC Subtitles SRT Subtitles VTT Swift swiftDialog System Events System Settings TemporaryItems Terminal Text Text CSV Text MD Text TSV TextEdit Tools Translate Trash Twitter Typography UI Unit Conversion UTType valueForKeyPath Video VisionKit Visual Studio Code VMware Fusion Wacom Weather webarchive webp Wifi Windows XML XML EPUB XML HTML XML LSSharedFileList XML LSSharedFileList sfl2 XML LSSharedFileList sfl3 XML objectsForXQuery XML OPML XML Plist XML Plist System Events XML RSS XML savedSearch XML SVG XML TTML XML webloc XML xmllint XML XMP YouTube Zero Padding zoom