A3サイズのPDFを縮小して2in1 2upでA4サイズにPDFファイルに印刷する (途中)
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #! /usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | # |
005 | # com.cocolog-nifty.quicktimer.icefloe |
006 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
007 | use AppleScript version "2.8" |
008 | use framework "Foundation" |
009 | use framework "UniformTypeIdentifiers" |
010 | use framework "AppKit" |
011 | use framework "PDFKit" |
012 | use scripting additions |
013 | |
014 | property refMe : a reference to current application |
015 | #A4 PTサイズ 小数点以下2位 |
016 | property ocidA4Rect : {{0.0, 0.0}, {595.28, 841.89}} |
017 | property ocidA4Size : {width:595.28, height:841.89} |
018 | # |
019 | property ocidA4HRect : {{0.0, 0.0}, {841.89, 595.28}} |
020 | property ocidA4HSize : {width:841.89, height:595.28} |
021 | # |
022 | property ocidA4LRect : {{0.0, 0.0}, {595.28, 841.89}} |
023 | property ocidA4RRect : {{0.0, 297.64}, {595.28, 841.89}} |
024 | |
025 | set appFileManager to refMe's NSFileManager's defaultManager() |
026 | |
027 | ################ |
028 | #入力 |
029 | set strName to (name of current application) as text |
030 | if strName is "osascript" then |
031 | tell application "Finder" to activate |
032 | else |
033 | tell current application to activate |
034 | end if |
035 | # |
036 | set appFileManager to refMe's NSFileManager's defaultManager() |
037 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
038 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
039 | set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias |
040 | # |
041 | set listUTI to {"com.adobe.pdf"} |
042 | set strMes to ("PDFファイルを選んでください") as text |
043 | set strPrompt to ("PDFファイルを選んでください") as text |
044 | try |
045 | set aliasFilePath to (choose file strMes with prompt strPrompt default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias |
046 | on error |
047 | log "エラーしました" |
048 | return "エラーしました" |
049 | end try |
050 | |
051 | set strFilePath to (POSIX path of aliasFilePath) as text |
052 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
053 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
054 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false) |
055 | # |
056 | set ocidFileName to ocidFilePathURL's lastPathComponent() |
057 | set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension() |
058 | |
059 | ################ |
060 | #出力 |
061 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
062 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
063 | # |
064 | set strMergeFileName to ("A4_" & ocidBaseFileName) as text |
065 | set ocidBaseMergeFilePathURL to (ocidDesktopDirPathURL's URLByAppendingPathComponent:(strMergeFileName) isDirectory:(false)) |
066 | set ocidMergeFilePathURL to (ocidBaseMergeFilePathURL's URLByAppendingPathExtension:("pdf")) |
067 | # |
068 | set ocidMergeDoc to refMe's PDFDocument's alloc()'s init() |
069 | |
070 | ################ |
071 | #PDFDocument |
072 | set ocidActivDoc to refMe's PDFDocument's alloc()'s initWithURL:(ocidFilePathURL) |
073 | set numCntPage to ocidActivDoc's pageCount() |
074 | set ocidPageRange to refMe's NSRange's NSMakeRange(0, (numCntPage - 1)) |
075 | |
076 | ################ |
077 | #テンポラリー |
078 | #中間ファイルを保存する場合利用 |
079 | set ocidTempDirURL to appFileManager's temporaryDirectory() |
080 | set ocidUUID to refMe's NSUUID's alloc()'s init() |
081 | set ocidUUIDString to ocidUUID's UUIDString |
082 | set ocidSaveDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true |
083 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
084 | ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions) |
085 | set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference) |
086 | if (item 2 of listDone) ≠ (missing value) then |
087 | set strErrorNO to (item 2 of listDone)'s code() as text |
088 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
089 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
090 | return "エラーしました" & strErrorNO & strErrorMes |
091 | end if |
092 | |
093 | ################ |
094 | #PDFView |
095 | set ocidPageView to refMe's PDFView's alloc()'s initWithFrame:(ocidA4HRect) |
096 | ocidPageView's setDocument:(ocidActivDoc) |
097 | |
098 | ################ |
099 | #ページの数だけ繰り返す |
100 | set numCntMargPageNo to 0 as integer |
101 | repeat with itemNo from 1 to (numCntPage) by 2 |
102 | set strSaveFileName to (itemNo & "_" & ocidBaseFileName) as text |
103 | set ocidBaseSaveFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false)) |
104 | set ocidSaveFilePathURL to (ocidBaseSaveFilePathURL's URLByAppendingPathExtension:("pdf")) |
105 | ################ |
106 | #PDFView |
107 | set ocidActivPage to (ocidActivDoc's pageAtIndex:(itemNo - 1)) |
108 | (ocidPageView's goToPage:(ocidActivPage)) |
109 | # |
110 | set ocidBackgroundColor to refMe's NSColor's whiteColor() |
111 | (ocidPageView's setBackgroundColor:(ocidBackgroundColor)) |
112 | (ocidPageView's setOpaque:(true)) |
113 | |
114 | |
115 | # |
116 | (ocidPageView's setAdditionalSafeAreaInsets:(refMe's NSEdgeInsetsZero)) |
117 | |
118 | #表紙の有無 |
119 | (ocidPageView's setDisplaysAsBook:(false)) |
120 | (ocidPageView's setInterpolationQuality:(refMe's kPDFInterpolationQualityHigh)) |
121 | (ocidPageView's setDisplaysPageBreaks:(false)) |
122 | (ocidPageView's setPageBreakMargins:(refMe's NSEdgeInsetsZero)) |
123 | (ocidPageView's setDisplayDirection:(refMe's kPDFDisplayDirectionHorizontal)) |
124 | #(ocidPageView's setDisplayBox:(refMe's kPDFDisplayBoxTrimBox)) |
125 | (ocidPageView's setDisplayBox:(refMe's kPDFDisplayBoxCropBox)) |
126 | # |
127 | (ocidPageView's setDisplayMode:(refMe's kPDFDisplayTwoUpContinuous)) |
128 | #右開きの場合はtrue |
129 | (ocidPageView's setDisplaysRTL:(false)) |
130 | (ocidPageView's setAutoresizingMask:(refMe's NSViewNotSizable)) |
131 | # (ocidPageView's setAutoresizingMask:(18)) |
132 | (ocidPageView's setAutoScales:(true)) |
133 | (ocidPageView's setMaxScaleFactor:(0.5)) |
134 | (ocidPageView's setMinScaleFactor:(0.499)) |
135 | log (ocidPageView's scaleFactorForSizeToFit()) as number |
136 | # (ocidPageView's setScaleFactor:(0.49)) |
137 | |
138 | (ocidPageView's setAutoresizesSubviews:(true)) |
139 | (ocidPageView's enablePageShadows:(false)) |
140 | # |
141 | set ocidShadow to refMe's NSShadow's alloc()'s init() |
142 | set ocidShadowColor to (refMe's NSColor's colorWithSRGBRed:(1.0) green:(1.0) blue:(1.0) alpha:(0.0)) |
143 | (ocidShadow's setShadowColor:(ocidShadowColor)) |
144 | (ocidPageView's setShadow:(ocidShadow)) |
145 | (ocidPageView's setAlphaValue:(0.0)) |
146 | #スクロールを表示させない |
147 | set ocidSubViewArray to ocidPageView's subviews() |
148 | set appScrollView to ocidSubViewArray's firstObject() |
149 | (appScrollView's setHasVerticalScroller:(false)) |
150 | (appScrollView's setHasHorizontalScroller:(false)) |
151 | (appScrollView's setHasVerticalRuler:(false)) |
152 | (appScrollView's setHasHorizontalRuler:(false)) |
153 | (appScrollView's setDrawsBackground:(false)) |
154 | (appScrollView's setHasHorizontalRuler:(false)) |
155 | (appScrollView's setAutohidesScrollers:(true)) |
156 | (appScrollView's setScrollerStyle:(refMe's NSScrollerStyleOverlay)) |
157 | # |
158 | (ocidPageView's setWantsLayer:(true)) |
159 | set ocidLay to ocidPageView's layer() |
160 | (ocidLay's setShadowOpacity:(0.0)) |
161 | |
162 | ################ |
163 | #印刷 |
164 | #戻り値 |
165 | set ocidResponse to doPrint(ocidPageView, ocidSaveFilePathURL) |
166 | #失敗時 |
167 | if ocidResponse is false then |
168 | return "印刷失敗" |
169 | else |
170 | #成功の場合は戻り値がPDFページデータ |
171 | set ocidPrintPage to ocidResponse |
172 | end if |
173 | #マージ用のPDFに戻り値のページデータを入れていく |
174 | (ocidMergeDoc's insertPage:(ocidPrintPage) atIndex:(numCntMargPageNo)) |
175 | set numCntMargPageNo to (numCntMargPageNo + 1) as integer |
176 | end repeat |
177 | ##マージしたPDFを保存する |
178 | set boolDone to ocidMergeDoc's writeToURL:ocidMergeFilePathURL |
179 | |
180 | |
181 | to doPrint(argPageView, argSaveFilePathURL) |
182 | ################ |
183 | #NSPrint |
184 | set ocidPrinterInfoDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
185 | ocidPrinterInfoDict's setObject:(refMe's NSPrintSaveJob) forKey:(refMe's NSPrintJobDisposition) |
186 | ocidPrinterInfoDict's setObject:(argSaveFilePathURL) forKey:(refMe's NSPrintJobSavingURL) |
187 | ocidPrinterInfoDict's setObject:(true) forKey:(refMe's NSPrintAllPages) |
188 | ocidPrinterInfoDict's setObject:(1) forKey:(refMe's NSPrintCopies) |
189 | ocidPrinterInfoDict's setObject:(false) forKey:(refMe's NSPrintHeaderAndFooter) |
190 | # |
191 | ocidPrinterInfoDict's setObject:(0.0) forKey:(refMe's NSPrintLeftMargin) |
192 | ocidPrinterInfoDict's setObject:(0.0) forKey:(refMe's NSPrintRightMargin) |
193 | ocidPrinterInfoDict's setObject:(0.0) forKey:(refMe's NSPrintTopMargin) |
194 | ocidPrinterInfoDict's setObject:(0.0) forKey:(refMe's NSPrintBottomMargin) |
195 | #set appPrinterInfo to refMe's NSPrintInfo's sharedPrintInfo() |
196 | set appPrinterInfo to refMe's NSPrintInfo's alloc()'s initWithDictionary:(ocidPrinterInfoDict) |
197 | #DictにAttributeKeyで追加していく方法 |
198 | #set ocidPrinterInfoDict to appPrinterInfo's dictionary() |
199 | |
200 | #setを使って簡易に設定していく方法 |
201 | appPrinterInfo's setPaperName:("A4") |
202 | appPrinterInfo's setPaperSize:(ocidA4HSize) |
203 | appPrinterInfo's setOrientation:(refMe's NSPaperOrientationLandscape) |
204 | # appPrinterInfo's setOrientation:(refMe's NSPaperOrientationPortrait) |
205 | # |
206 | appPrinterInfo's setHorizontallyCentered:(true) |
207 | appPrinterInfo's setVerticallyCentered:(true) |
208 | appPrinterInfo's setHorizontalPagination:(refMe's NSPrintingPaginationModeFit) |
209 | appPrinterInfo's setVerticalPagination:(refMe's NSPrintingPaginationModeFit) |
210 | |
211 | ################ |
212 | #NSPrintOperation |
213 | #URLにファイルとして印刷する場合 |
214 | # set appPrintOperation to refMe's NSPrintOperation's printOperationWithView:(argPageView) printInfo:(appPrinterInfo) |
215 | #パスにファイルとして印刷する場合 |
216 | # set appPrintOperation to refMe's NSPrintOperation's PDFOperationWithView:(argPageView) insideRect:(ocidA4HRect) toPath:(ocidSaveFilePath) printInfo:(appPrinterInfo) |
217 | #データに印刷 |
218 | set ocidPrintData to refMe's NSMutableData's alloc()'s initWithCapacity:(0) |
219 | set appPrintOperation to refMe's NSPrintOperation's PDFOperationWithView:(argPageView) insideRect:(ocidA4HRect) toData:(ocidPrintData) printInfo:(appPrinterInfo) |
220 | # |
221 | appPrintOperation's setShowsPrintPanel:(false) |
222 | appPrintOperation's setShowsProgressPanel:(false) |
223 | appPrintOperation's setPageOrder:(refMe's NSAscendingPageOrder) |
224 | #印刷 |
225 | set boolDone to appPrintOperation's runOperation() |
226 | # |
227 | if boolDone is false then |
228 | log "印刷が失敗しました" |
229 | return false |
230 | end if |
231 | #印刷したデータからPDFを生成して |
232 | set ocidPrintDoc to refMe's PDFDocument's alloc()'s initWithData:(ocidPrintData) |
233 | #1ページ目を取得する |
234 | set ocidPrintPage to ocidPrintDoc's pageAtIndex:(0) |
235 | #ページデータを戻す |
236 | return ocidPrintPage |
237 | |
238 | end doPrint |
AppleScriptで生成しました |
| 固定リンク