PPDの用紙情報をダイアログに戻して表示
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | # com.cocolog-nifty.quicktimer.icefloe |
005 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
006 | use AppleScript version "2.8" |
007 | use framework "Foundation" |
008 | use framework "AppKit" |
009 | use framework "PrintKit" |
010 | use scripting additions |
011 | |
012 | property refMe : a reference to current application |
013 | set appFileManager to refMe's NSFileManager's defaultManager() |
014 | |
015 | |
016 | ############################ |
017 | #作業エリア gz解凍 zcatは使わない |
018 | #起動時に削除される項目内 |
019 | set ocidTempDirURL to appFileManager's temporaryDirectory() |
020 | set ocidUUID to refMe's NSUUID's alloc()'s init() |
021 | set ocidUUIDString to ocidUUID's UUIDString |
022 | set ocidSaveDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:(true) |
023 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
024 | ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions) |
025 | set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference) |
026 | #パス |
027 | set strFileName to "Printer.ppd" as text |
028 | set ocidPPDFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strFileName) isDirectory:false |
029 | set strTempFilePath to (ocidPPDFilePathURL's |path|) as text |
030 | |
031 | ############################ |
032 | #ダイアログ |
033 | set strName to (name of current application) as text |
034 | if strName is "osascript" then |
035 | tell application "Finder" to activate |
036 | else |
037 | tell current application to activate |
038 | end if |
039 | #ローカルドメインのプリンタPPD |
040 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSLocalDomainMask)) |
041 | set ocidLibraryDirPathURL to ocidURLsArray's firstObject() |
042 | set ocidResourcesDirPathURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:("Printers/PPDs/Contents/Resources") isDirectory:(true) |
043 | set aliasDefaultLocation to (ocidResourcesDirPathURL's absoluteURL()) as alias |
044 | set listUTI to {"public.item"} |
045 | set strMes to ("PPDファイルを選んでください") as text |
046 | set strPrompt to ("PPDファイルを選んでください\n拡張子無しの場合や\ngz圧縮されている場合があります") as text |
047 | try |
048 | set aliasChooseFilePath 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 |
049 | on error |
050 | log "エラーしました" |
051 | return "エラーしました" |
052 | end try |
053 | ############################ |
054 | #元になるPPD |
055 | set strChooseFilePath to (POSIX path of aliasChooseFilePath) as text |
056 | set ocidChooseFilePathStr to refMe's NSString's stringWithString:(strChooseFilePath) |
057 | set ocidChooseFilePath to ocidChooseFilePathStr's stringByStandardizingPath() |
058 | set ocidChooseFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidChooseFilePath) isDirectory:(false) |
059 | set strChooseExtensionName to (ocidChooseFilePathURL's pathExtension()) as text |
060 | #gzなら解凍 |
061 | if strChooseExtensionName is "gz" then |
062 | ################ |
063 | #コマンド実行 |
064 | set strCommandText to ("/usr/bin/gunzip -c \"" & strChooseFilePath & "\" > \"" & strTempFilePath & "\"") as text |
065 | log strCommandText |
066 | set strExecCommand to ("/bin/zsh -c '" & strCommandText & "'") as text |
067 | try |
068 | do shell script strExecCommand |
069 | on error |
070 | return "gunzip でエラーしました" |
071 | end try |
072 | |
073 | else |
074 | #gz以外ならそのまま利用 |
075 | ################ |
076 | #コマンド実行 |
077 | set strCommandText to ("/usr/bin/ditto \"" & strChooseFilePath & "\" \"" & strTempFilePath & "\"") as text |
078 | log strCommandText |
079 | set strExecCommand to ("/bin/zsh -c '" & strCommandText & "'") as text |
080 | try |
081 | do shell script strExecCommand |
082 | on error |
083 | return "ditto でエラーしました" |
084 | end try |
085 | |
086 | end if |
087 | |
088 | ############################ |
089 | #PPDを読み込む NSDATA |
090 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
091 | set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidPPDFilePathURL) options:(ocidOption) |error| :(reference) |
092 | if (item 2 of listResponse) = (missing value) then |
093 | log "正常処理" |
094 | set ocidReadData to (item 1 of listResponse) |
095 | else if (item 2 of listResponse) ≠ (missing value) then |
096 | set strErrorNO to (item 2 of listResponse)'s code() as text |
097 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
098 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
099 | return "エラーしました" & strErrorNO & strErrorMes |
100 | end if |
101 | |
102 | ############################ |
103 | #テキストにする NSString's |
104 | set ocidReadStrings to refMe's NSString's alloc()'s initWithData:(ocidReadData) encoding:(refMe's NSUTF8StringEncoding) |
105 | #ここまでのデータをクリアしておく |
106 | set ocidReadData to (missing value) |
107 | #改行をUNIXに強制 |
108 | set ocidReadStringsTMP to (ocidReadStrings's stringByReplacingOccurrencesOfString:("\n\r") withString:("\n")) |
109 | set ocidReadStringsTMP to (ocidReadStringsTMP's stringByReplacingOccurrencesOfString:("\r") withString:("\n")) |
110 | set ocidReadStrings to (ocidReadStringsTMP's stringByReplacingOccurrencesOfString:("\n\n") withString:("\n")) |
111 | |
112 | |
113 | ############################ |
114 | #リストArrayでの処理 |
115 | #改行でリストにする |
116 | set ocidReadStringsArray to ocidReadStrings's componentsSeparatedByString:("\n") |
117 | set ocidImageableAreaDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
118 | set ocidPaperDimensionDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
119 | |
120 | #全行チェック |
121 | repeat with itemLine in ocidReadStringsArray |
122 | if ((itemLine's hasPrefix:("*ImageableArea")) as boolean) then |
123 | set ocidImgAreaStr to (itemLine's stringByReplacingOccurrencesOfString:("*ImageableArea ") withString:("")) |
124 | set ocidImgAreaArray to (ocidImgAreaStr's componentsSeparatedByString:(": \"")) |
125 | set ocidSetKey to ocidImgAreaArray's firstObject() |
126 | set ocidTempRectStr to ocidImgAreaArray's lastObject() |
127 | set ocidTempRectStr to (ocidTempRectStr's stringByReplacingOccurrencesOfString:(" \"") withString:("")) |
128 | set ocidImgAreaRectArray to (ocidTempRectStr's componentsSeparatedByString:(" ")) |
129 | (ocidImageableAreaDict's setValue:(ocidImgAreaRectArray) forKey:(ocidSetKey)) |
130 | |
131 | else if ((itemLine's hasPrefix:("*PaperDimension")) as boolean) then |
132 | set ocidPaperDimStr to (itemLine's stringByReplacingOccurrencesOfString:("*PaperDimension ") withString:("")) |
133 | set ocidPaperDimArray to (ocidPaperDimStr's componentsSeparatedByString:(": \"")) |
134 | set ocidSetKey to ocidPaperDimArray's firstObject() |
135 | set ocidTempRectStr to ocidPaperDimArray's lastObject() |
136 | set ocidTempRectStr to (ocidTempRectStr's stringByReplacingOccurrencesOfString:("\"") withString:("")) |
137 | set ocidPaperDimensionRectArray to (ocidTempRectStr's componentsSeparatedByString:(" ")) |
138 | (ocidPaperDimensionDict's setValue:(ocidPaperDimensionRectArray) forKey:(ocidSetKey)) |
139 | |
140 | else if ((itemLine's hasPrefix:("*HWMargins")) as boolean) then |
141 | set ocidHWMarginsStr to (itemLine's stringByReplacingOccurrencesOfString:("*HWMargins: ") withString:("")) |
142 | set ocidHWMarginsArray to (ocidHWMarginsStr's componentsSeparatedByString:(" ")) |
143 | set strHWMargins to ("HWMargins: ") as text |
144 | set strLeftMargins to doPtToMm(ocidHWMarginsArray's objectAtIndex:(0)) |
145 | set strBottomMargins to doPtToMm(ocidHWMarginsArray's objectAtIndex:(1)) |
146 | set strRightMargins to doPtToMm(ocidHWMarginsArray's objectAtIndex:(2)) |
147 | set strTopMargins to doPtToMm(ocidHWMarginsArray's objectAtIndex:(3)) |
148 | set strHWMargins to ("HWMargins(left bottom right top): " & strLeftMargins & "," & strBottomMargins & "," & strRightMargins & "," & strTopMargins) as text |
149 | |
150 | else if ((itemLine's hasPrefix:("*MaxMediaWidth")) as boolean) then |
151 | set ocidMaxMediaWidthStr to (itemLine's stringByReplacingOccurrencesOfString:("*MaxMediaWidth: ") withString:("")) |
152 | set ocidMaxMediaWidthStr to (ocidMaxMediaWidthStr's stringByReplacingOccurrencesOfString:("\"") withString:("")) |
153 | set strMaxMediaWidthStr to doPtToMm(ocidMaxMediaWidthStr) |
154 | set strMaxMediaWidthStr to ("MaxMediaWidth: " & strMaxMediaWidthStr) as text |
155 | |
156 | else if ((itemLine's hasPrefix:("*MaxMediaHeight")) as boolean) then |
157 | set ocidMaxMediaHeightStr to (itemLine's stringByReplacingOccurrencesOfString:("*MaxMediaHeight: ") withString:("")) |
158 | set ocidMaxMediaHeightStr to (ocidMaxMediaHeightStr's stringByReplacingOccurrencesOfString:("\"") withString:("")) |
159 | set strMaxMediaHeightStr to doPtToMm(ocidMaxMediaHeightStr) |
160 | set strMaxMediaHeightStr to ("MaxMediaHeight: " & strMaxMediaHeightStr) as text |
161 | set strMaxMediaSize to ("MaxMediaSize: " & strMaxMediaWidthStr & "x" & strMaxMediaHeightStr) as text |
162 | end if |
163 | end repeat |
164 | |
165 | |
166 | |
167 | ############################ |
168 | # |
169 | set ocidAllKeysArray to ocidPaperDimensionDict's allKeys() |
170 | set ocidSortedArray to ocidAllKeysArray's sortedArrayUsingSelector:("localizedStandardCompare:") |
171 | set listAllKeysArray to ocidSortedArray as list |
172 | # |
173 | set strName to (name of current application) as text |
174 | if strName is "osascript" then |
175 | tell application "Finder" to activate |
176 | else |
177 | tell current application to activate |
178 | end if |
179 | ### |
180 | set strTitle to ("選んでください") as text |
181 | set strPrompt to ("用紙名を選んでください") as text |
182 | try |
183 | set listResponse to (choose from list listAllKeysArray with title strTitle with prompt strPrompt default items (item 1 of listAllKeysArray) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) as list |
184 | on error |
185 | log "エラーしました" |
186 | return "エラーしました" |
187 | end try |
188 | if (item 1 of listResponse) is false then |
189 | return "キャンセルしましたA" |
190 | else if (item 1 of listResponse) is "キャンセル" then |
191 | return "キャンセルしましたB" |
192 | else |
193 | set strPaperName to (item 1 of listResponse) as text |
194 | end if |
195 | ############################ |
196 | # |
197 | set ocidPaperDimensionArray to (ocidPaperDimensionDict's valueForKey:(strPaperName)) |
198 | set strPaperW to doPtToMm(ocidPaperDimensionArray's objectAtIndex:(0)) |
199 | set strPaperH to doPtToMm(ocidPaperDimensionArray's objectAtIndex:(1)) |
200 | set strPaperDimension to ("PaperDimension(wh): " & strPaperW & "x" & strPaperH) as text |
201 | |
202 | set ocidImageableAreaArray to (ocidImageableAreaDict's valueForKey:(strPaperName)) |
203 | set strLLx to doPtToMm(ocidImageableAreaArray's objectAtIndex:(0)) |
204 | set strLLy to doPtToMm(ocidImageableAreaArray's objectAtIndex:(1)) |
205 | set strURx to doPtToMm(ocidImageableAreaArray's objectAtIndex:(2)) |
206 | set strURy to doPtToMm(ocidImageableAreaArray's objectAtIndex:(3)) |
207 | set strImageableArea to ("「lower left x 」「lower left y 」「upper right x」「upper right y」\nImageableArea: " & strLLx & "," & strLLy & "," & strURx & "," & strURy) as text |
208 | |
209 | #テキストを数値に変更して |
210 | set numSetPaperWpt to (refMe's NSDecimalNumber's decimalNumberWithString:(strPaperW))'s floatValue() |
211 | set numSetPaperHpt to (refMe's NSDecimalNumber's decimalNumberWithString:(strPaperH))'s floatValue() |
212 | set numSetDimensionXpt to (refMe's NSDecimalNumber's decimalNumberWithString:(strLLx))'s floatValue() |
213 | set numSetDimensionYpt to (refMe's NSDecimalNumber's decimalNumberWithString:(strLLy))'s floatValue() |
214 | set numSetDimensionWpt to (refMe's NSDecimalNumber's decimalNumberWithString:(strURx))'s floatValue() |
215 | set numSetDimensionHpt to (refMe's NSDecimalNumber's decimalNumberWithString:(strURy))'s floatValue() |
216 | |
217 | ###小数点 |
218 | set appFormatter to refMe's NSNumberFormatter's alloc()'s init() |
219 | appFormatter's setRoundingMode:(refMe's NSNumberFormatterRoundHalfUp) |
220 | appFormatter's setNumberStyle:(refMe's NSNumberFormatterDecimalStyle) |
221 | appFormatter's setMaximumFractionDigits:(2) |
222 | set numSetMarginT to appFormatter's stringFromNumber:(numSetDimensionYpt) |
223 | set numSetMarginB to appFormatter's stringFromNumber:(numSetPaperHpt - numSetDimensionHpt) |
224 | set numSetMarginR to appFormatter's stringFromNumber:(numSetPaperWpt - numSetDimensionWpt) |
225 | set numSetMarginL to appFormatter's stringFromNumber:(numSetDimensionXpt) |
226 | |
227 | set strPaperMargins to ("PaperMargins(left bottom right top): " & numSetMarginL & "," & numSetMarginB & "," & numSetMarginR & "," & numSetMarginT) as text |
228 | |
229 | |
230 | ############################ |
231 | # |
232 | set strMesText to (strPaperName & "\n") as text |
233 | set strMesText to (strMesText & strPaperDimension & "\n") as text |
234 | set strMesText to (strMesText & strPaperMargins & "\n") as text |
235 | set strMesText to (strMesText & "------\n") as text |
236 | set strMesText to (strMesText & strImageableArea & "\n") as text |
237 | |
238 | set strMesText to (strMesText & strHWMargins & "\n") as text |
239 | set strMesText to (strMesText & strMaxMediaSize & "\n") as text |
240 | |
241 | log strMesText |
242 | |
243 | |
244 | ############################## |
245 | #####ダイアログ |
246 | ############################## |
247 | tell current application |
248 | set strName to name as text |
249 | end tell |
250 | ####スクリプトメニューから実行したら |
251 | if strName is "osascript" then |
252 | tell application "Finder" |
253 | activate |
254 | end tell |
255 | else |
256 | tell current application |
257 | activate |
258 | end tell |
259 | end if |
260 | set aliasIconPath to (POSIX file "/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/PrintCore.framework/Versions/A/Resources/PrinterPool.icns") as alias |
261 | try |
262 | set recordResult to (display dialog "PPD情報mm単位" with title "戻り値です" default answer strMesText buttons {"クリップボードにコピー", "キャンセル", "再実行"} default button "再実行" cancel button "キャンセル" giving up after 20 with icon aliasIconPath without hidden answer) as record |
263 | on error |
264 | log "エラーしました" |
265 | return "エラーしました" |
266 | end try |
267 | if (gave up of recordResult) is true then |
268 | return "時間切れです" |
269 | end if |
270 | ###自分自身を再実行 |
271 | if button returned of recordResult is "再実行" then |
272 | tell application "Finder" |
273 | set aliasPathToMe to (path to me) as alias |
274 | end tell |
275 | run script aliasPathToMe |
276 | end if |
277 | ############################## |
278 | #####値のコピー |
279 | ############################## |
280 | if button returned of recordResult is "クリップボードにコピー" then |
281 | try |
282 | set strText to text returned of recordResult as text |
283 | ####ペーストボード宣言 |
284 | set appPasteboard to refMe's NSPasteboard's generalPasteboard() |
285 | set ocidText to (refMe's NSString's stringWithString:(strText)) |
286 | appPasteboard's clearContents() |
287 | appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString) |
288 | on error |
289 | tell application "Finder" |
290 | set the clipboard to strText as text |
291 | end tell |
292 | end try |
293 | end if |
294 | |
295 | |
296 | return |
297 | |
298 | to doPtToMm(argText) |
299 | #割り算 |
300 | set ocidDecNo to refMe's NSDecimalNumber's alloc()'s initWithString:(argText) |
301 | set ocidDecPt to refMe's NSDecimalNumber's alloc()'s initWithString:("72") |
302 | set ocidNoDivPt to (ocidDecNo's decimalNumberByDividingBy:(ocidDecPt)) |
303 | #掛け算 |
304 | set ocidDecInch to refMe's NSDecimalNumber's alloc()'s initWithString:("25.4") |
305 | set ocidDecMM to (ocidNoDivPt's decimalNumberByMultiplyingBy:(ocidDecInch)) |
306 | ###小数点 |
307 | set appFormatter to refMe's NSNumberFormatter's alloc()'s init() |
308 | appFormatter's setRoundingMode:(refMe's NSNumberFormatterRoundHalfUp) |
309 | appFormatter's setNumberStyle:(refMe's NSNumberFormatterDecimalStyle) |
310 | appFormatter's setMaximumFractionDigits:(2) |
311 | set ocidMM to appFormatter's stringFromNumber:(ocidDecMM) |
312 | set strMM to ocidMM as text |
313 | return strMM |
314 | end doPtToMm |
315 | |
316 | |
317 | |
318 | |
319 | |
320 | |
AppleScriptで生成しました |
| 固定リンク