QRコード(WEB配布用)への工夫
| 固定リンク
行番号 | ソース |
---|---|
001 | #! /usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | (* |
005 | QRコード生成 |
006 | クワイエットゾーン用画像を作成 |
007 | 出力用のアートボード画像を作成 |
008 | 出力イメージの解像度を設定 |
009 | ファイルに保存 |
010 | *) |
011 | # com.cocolog-nifty.quicktimer.icefloe |
012 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
013 | use AppleScript version "2.8" |
014 | use framework "Foundation" |
015 | use framework "AppKit" |
016 | use framework "PDFKit" |
017 | use framework "CoreImage" |
018 | use scripting additions |
019 | |
020 | property refMe : a reference to current application |
021 | |
022 | ################ |
023 | #設定項目 |
024 | #バーコードサイズmm指定(四捨五入するので近似値になる) |
025 | set strMMsize to ("30") as text |
026 | |
027 | #出力画像mm |
028 | set numOutPutMmW to 91.1 as number |
029 | set numOutPutMmH to 55.1 as number |
030 | |
031 | #出力解像度PPI |
032 | set numResolution to 288 as integer |
033 | |
034 | #バーコードの中身 |
035 | set strText to ("http://news.yahoo.co.jp/") as text |
036 | |
037 | #QRバーコードのセルの色 |
038 | set ocidQRColor to refMe's CIColor's colorWithRed:(0.0) green:(0.0) blue:(0.0) alpha:(1.0) |
039 | #QRバーコードの背景 白 #alpha0にすると透過になってQRコードの背景色 |
040 | set ocidWhiteColor to refMe's CIColor's colorWithRed:(1.0) green:(1.0) blue:(1.0) alpha:(0.0) |
041 | #QRコードの背景色 白 #alpha0にすると透過になって出力イメージの背景色 |
042 | set ocidQuietZoneColor to refMe's NSColor's colorWithRed:(1.0) green:(1.0) blue:(1.0) alpha:(0.0) |
043 | #出力イメージの背景色 |
044 | set ocidArtBoardColor to refMe's NSColor's colorWithRed:(0.4392) green:(0.8196) blue:(0.7764) alpha:(1.0) |
045 | |
046 | ################ |
047 | set strFileName to "解像度指定QRバーコード" as text |
048 | #出力先パス デスクトップ |
049 | set appFileManager to refMe's NSFileManager's defaultManager() |
050 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
051 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
052 | set ocidBaseFilePathURL to ocidDesktopDirPathURL's URLByAppendingPathComponent:(strFileName) isDirectory:(false) |
053 | set argSaveFilePathURL to ocidBaseFilePathURL's URLByAppendingPathExtension:("png") |
054 | |
055 | |
056 | ############## |
057 | #テキスト |
058 | set ocidText to refMe's NSString's alloc()'s initWithString:(strText) |
059 | set ocidTextData to ocidText's dataUsingEncoding:(refMe's NSUTF8StringEncoding) |
060 | |
061 | ############## |
062 | #mmをPTに換算 |
063 | set numPtSize to doConversionMM2PT(strMMsize) |
064 | |
065 | #最終的に出力したいQRコードのサイズ |
066 | set numScaleMax to (numPtSize * (numResolution / 72)) as integer |
067 | |
068 | ############## |
069 | #【1】QRコード生成 |
070 | #CIFilter QRコード生成 |
071 | set appQRGenerator to refMe's CIFilter's filterWithName:("CIQRCodeGenerator") |
072 | appQRGenerator's setDefaults() |
073 | #テキスト指定 |
074 | appQRGenerator's setValue:(ocidTextData) forKey:("inputMessage") |
075 | #読み取り誤差値設定L, M, Q, H |
076 | appQRGenerator's setValue:("Q") forKey:("inputCorrectionLevel") |
077 | #QRコード本体のイメージ |
078 | set ocidCIImage to appQRGenerator's outputImage() |
079 | |
080 | ############## |
081 | #【2】色の置換 |
082 | #色の置換 |
083 | set appCIFalseColor to refMe's CIFilter's filterWithName:"CIFalseColor" |
084 | appCIFalseColor's setDefaults() |
085 | appCIFalseColor's setValue:(ocidQRColor) forKey:("inputColor0") |
086 | appCIFalseColor's setValue:(ocidWhiteColor) forKey:("inputColor1") |
087 | appCIFalseColor's setValue:(ocidCIImage) forKey:("inputImage") |
088 | #色が変わった後のイメージの取り出し |
089 | set ocidCIImage to appCIFalseColor's outputImage() |
090 | |
091 | ############## |
092 | #【3】サイズ拡大 |
093 | #拡大 まずは作成したQRのピクセルサイズ取得 |
094 | set ocidQRRect to ocidCIImage's extent() |
095 | #QRバーコードは正方形なので幅しか取らない |
096 | set ocidCIImageWidth to refMe's NSRect's NSWidth(ocidQRRect) |
097 | #↑の値のニアなサイズになります |
098 | #整数で拡大しないとアンチエイリアスが立つので |
099 | #出力サイズ÷出来上がりQRコードサイズ÷1で整数に |
100 | set numScale to ((numScaleMax / ocidCIImageWidth) div 1) as integer |
101 | |
102 | ############## |
103 | #【3】サイズ拡大 |
104 | #変換スケール作成-->拡大 |
105 | set recordScalse to refMe's CGAffineTransform's CGAffineTransformMakeScale(numScale, numScale) |
106 | #変換スケールを適応(元のサイズに元のサイズのスケール適応しても意味ないけど |
107 | set ocidCIImageScaled to ocidCIImage's imageByApplyingTransform:(recordScalse) |
108 | #QRイメージIMAGEREPに |
109 | set ocidCIImageRep to refMe's NSCIImageRep's imageRepWithCIImage:(ocidCIImageScaled) |
110 | #拡大したQRコードのサイズ |
111 | set ocidCIImageSize to ocidCIImageRep's |size|() |
112 | #出力用のNSIMAGEをQRコードのサイズで作成 |
113 | set ocidNSImageScaled to refMe's NSImage's alloc()'s initWithSize:(ocidCIImageSize) |
114 | #QRコードを挿入 |
115 | ocidNSImageScaled's addRepresentation:(ocidCIImageRep) |
116 | #TIFFRepresentation |
117 | set ocidOsDispatchData to ocidNSImageScaled's TIFFRepresentation() |
118 | #NSBitmapImageRep |
119 | set ocidQRImageRep to refMe's NSBitmapImageRep's imageRepWithData:(ocidOsDispatchData) |
120 | set numScaledSize to ocidQRImageRep's pixelsWide() |
121 | |
122 | ############## |
123 | #【4】クワイエットゾーン用画像を作成 |
124 | #quiet zone用に画像をパディングする |
125 | set numQuietZonePxW to ((numScaledSize) + (numScale * 6)) as integer |
126 | #左右に3セル分づつ=縦横6セル分の余白 quiet zoneを足す |
127 | #まずは元のQRコードのサイズに3セルサイズ分足したサイズの画像=出力サイズイメージ |
128 | #アートボードになる出力サイズイメージ |
129 | #CIFilterのQRコードは1セルクワイエットゾーンがあるので3セル足して4セルにする |
130 | set ocidColorSpaceName to (refMe's NSDeviceRGBColorSpace) |
131 | set ocidBitmapFormat to (refMe's NSBitmapFormatAlphaFirst) |
132 | set ocidQuietZoneRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numQuietZonePxW) pixelsHigh:(numQuietZonePxW) bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:false colorSpaceName:(ocidColorSpaceName) bitmapFormat:(ocidBitmapFormat) bytesPerRow:0 bitsPerPixel:32) |
133 | |
134 | ############## |
135 | #【5】クワイエットゾーン画像背景を白に |
136 | #初期化 |
137 | set appGraphicsContext to (refMe's NSGraphicsContext) |
138 | #編集開始 |
139 | appGraphicsContext's saveGraphicsState() |
140 | #イメージ読み込み |
141 | set ocidReadImageContext to appGraphicsContext's graphicsContextWithBitmapImageRep:(ocidQuietZoneRep) |
142 | #出力サイズイメージ読み込み |
143 | appGraphicsContext's setCurrentContext:(ocidReadImageContext) |
144 | #塗り色を『白』に指定して |
145 | ocidQuietZoneColor's |set|() |
146 | #画像として色を塗る |
147 | set ocidFillRect to refMe's NSRect's NSMakeRect(0, 0, numQuietZonePxW, numQuietZonePxW) |
148 | refMe's NSRectFill(ocidFillRect) |
149 | #編集終了 |
150 | appGraphicsContext's restoreGraphicsState() |
151 | |
152 | ############## |
153 | #【6】QRコードを5の画像にNSCompositeSourceOver |
154 | set appGraphicsContext to (refMe's NSGraphicsContext) |
155 | #編集開始 |
156 | appGraphicsContext's saveGraphicsState() |
157 | #イメージ読み込み |
158 | set ocidReadImageContext to appGraphicsContext's graphicsContextWithBitmapImageRep:(ocidQuietZoneRep) |
159 | #出力サイズイメージ読み込み |
160 | appGraphicsContext's setCurrentContext:(ocidReadImageContext) |
161 | #背景画像の上にQRバーコードの画像をNSCompositeSourceOver |
162 | #位置関係 ペーストする位置 |
163 | set ocidDrawRect to refMe's NSRect's NSMakeRect((numScale * 3), (numScale * 3), numScaledSize, numScaledSize) |
164 | #元画像をコピーしてくる位置 |
165 | set ocidFromRect to refMe's NSRect's NSMakeRect(0, 0, numScaledSize, numScaledSize) |
166 | #NSCompositeSourceOver |
167 | set appOperation to (refMe's NSCompositeSourceOver) |
168 | ocidQRImageRep's drawInRect:(ocidDrawRect) fromRect:(ocidFromRect) operation:(appOperation) fraction:1.0 respectFlipped:true hints:(missing value) |
169 | #画像作成終了 |
170 | appGraphicsContext's restoreGraphicsState() |
171 | |
172 | |
173 | ############## |
174 | #【7】出力用のアートボード画像を作成 |
175 | #アートボード出力用画像生成 MM to PT |
176 | #サイズ MM to PT |
177 | set numOutPutPtW to doConversionMM2PT(numOutPutMmW) |
178 | set numOutPutPtH to doConversionMM2PT(numOutPutMmH) |
179 | #サイズ PT to PX |
180 | set numArtBoardPxW to (numOutPutPtW * (numResolution / 72)) as integer |
181 | set numArtBoardPxH to (numOutPutPtH * (numResolution / 72)) as integer |
182 | # |
183 | set ocidColorSpaceName to (refMe's NSDeviceRGBColorSpace) |
184 | set ocidBitmapFormat to (refMe's NSBitmapFormatAlphaFirst) |
185 | set ocidArtBoardRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numArtBoardPxW) pixelsHigh:(numArtBoardPxH) bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:false colorSpaceName:(ocidColorSpaceName) bitmapFormat:(ocidBitmapFormat) bytesPerRow:0 bitsPerPixel:32) |
186 | |
187 | ############## |
188 | #【8】アートボード部の背景色 |
189 | #初期化 |
190 | set appGraphicsContext to (refMe's NSGraphicsContext) |
191 | #編集開始 |
192 | appGraphicsContext's saveGraphicsState() |
193 | #イメージ読み込み |
194 | set ocidReadImageContext to appGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep) |
195 | #出力サイズイメージ読み込み |
196 | appGraphicsContext's setCurrentContext:(ocidReadImageContext) |
197 | #塗り色を『白』に指定して |
198 | ocidArtBoardColor's |set|() |
199 | #画像として色を塗る |
200 | set ocidFillRect to refMe's NSRect's NSMakeRect(0, 0, numArtBoardPxW, numArtBoardPxH) |
201 | refMe's NSRectFill(ocidFillRect) |
202 | #編集終了 |
203 | appGraphicsContext's restoreGraphicsState() |
204 | |
205 | ############## |
206 | #【9】6の画像をNSCompositeSourceOver |
207 | set appGraphicsContext to (refMe's NSGraphicsContext) |
208 | #編集開始 |
209 | appGraphicsContext's saveGraphicsState() |
210 | #イメージ読み込み |
211 | set ocidReadImageContext to appGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep) |
212 | #出力サイズイメージ読み込み |
213 | appGraphicsContext's setCurrentContext:(ocidReadImageContext) |
214 | #背景画像の上にQRバーコードの画像をNSCompositeSourceOver |
215 | #位置関係 ペーストする位置 |
216 | set numPadding to 25 |
217 | set ocidDrawRect to refMe's NSRect's NSMakeRect((numArtBoardPxW - numQuietZonePxW - numPadding), (numArtBoardPxH - numQuietZonePxW - numPadding), numQuietZonePxW, numQuietZonePxW) |
218 | #元画像をコピーしてくる位置 |
219 | set ocidFromRect to refMe's NSRect's NSMakeRect(0, 0, numQuietZonePxW, numQuietZonePxW) |
220 | #NSCompositeSourceOver |
221 | set appOperation to (refMe's NSCompositeSourceOver) |
222 | ocidQuietZoneRep's drawInRect:(ocidDrawRect) fromRect:(ocidFromRect) operation:(appOperation) fraction:1.0 respectFlipped:true hints:(missing value) |
223 | #画像作成終了 |
224 | appGraphicsContext's restoreGraphicsState() |
225 | |
226 | |
227 | ############## |
228 | #【10】解像度変更 |
229 | #出力画像サイズ |
230 | set ocidArtBoardSize to ocidArtBoardRep's |size|() |
231 | #幅Pt=Pxだけ取得して |
232 | set numPtW to (ocidArtBoardSize's width()) as number |
233 | set numPtH to (ocidArtBoardSize's height()) as number |
234 | #設定解像度÷72÷出来上がりサイズで |
235 | # 2なら144ppi 3なら216ppi 4なら288ppi 5なら360ppi |
236 | set numSetSizeW to (numPtW / (numResolution / 72)) as number |
237 | set numSetSizeH to (numPtH / (numResolution / 72)) as number |
238 | #サイズを生成して |
239 | set ocidOutPutSize to refMe's NSSize's NSMakeSize(numSetSizeW, numSetSizeH) |
240 | #サイズを設定(ここで設定するのはPTサイズ) |
241 | ocidArtBoardRep's setSize:(ocidOutPutSize) |
242 | |
243 | |
244 | |
245 | |
246 | ############## |
247 | #PNG保存のプロパティ |
248 | set ocidPropertiesDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
249 | ocidPropertiesDict's setValue:(false) forKey:(refMe's NSImageInterlaced) |
250 | ocidPropertiesDict's setValue:(1.0 / 2.2) forKey:(refMe's NSImageGamma) |
251 | ocidPropertiesDict's setValue:(missing value) forKey:(refMe's NSImageIPTCData) |
252 | ocidPropertiesDict's setValue:(missing value) forKey:(refMe's NSImageEXIFData) |
253 | #出力イメージへ変換 |
254 | set ocidType to (refMe's NSBitmapImageFileTypePNG) |
255 | set ocidSaveImageData to ocidArtBoardRep's representationUsingType:(ocidType) |properties|:(ocidPropertiesDict) |
256 | |
257 | (* |
258 | ############## |
259 | #TIFF保存のプロパティ |
260 | set ocidPropertiesDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
261 | ocidPropertiesDict's setValue:(0.0) forKey:(refMe's NSImageCompressionFactor) |
262 | ocidPropertiesDict's setValue:(missing value) forKey:(refMe's NSImageCompressionMethod) |
263 | ocidPropertiesDict's setValue:(missing value) forKey:(refMe's NSImageIPTCData) |
264 | ocidPropertiesDict's setValue:(missing value) forKey:(refMe's NSImageEXIFData) |
265 | #出力イメージへ変換 |
266 | set ocidType to (refMe's NSBitmapImageFileTypeTIFF) |
267 | set ocidSaveImageData to ocidArtBoardRep's representationUsingType:(ocidType) |properties|:(ocidPropertiesDict) |
268 | *) |
269 | ############## |
270 | #【11】保存 |
271 | set ocidOption to (refMe's NSDataWritingAtomic) |
272 | set listDone to ocidSaveImageData's writeToURL:(argSaveFilePathURL) options:(ocidOption) |error| :(reference) |
273 | if (item 1 of listDone) is true then |
274 | return true |
275 | else if (item 2 of listDone) ≠ (missing value) then |
276 | set strErrorNO to (item 2 of listDone)'s code() as text |
277 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
278 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
279 | log "エラーしました" & strErrorNO & strErrorMes |
280 | return false |
281 | end if |
282 | return true |
283 | |
284 | |
285 | |
286 | |
287 | |
288 | ############################ |
289 | # mmをPTに換算 |
290 | ############################ |
291 | to doConversionMM2PT(argNumMM) |
292 | set strArgNumMM to argNumMM as text |
293 | #指数 |
294 | set ocidCm2InchNo to refMe's NSDecimalNumber's alloc()'s initWithString:("25.4") |
295 | set ocidDivPt to refMe's NSDecimalNumber's alloc()'s initWithString:("72") |
296 | set ocidDivNo to (ocidCm2InchNo's decimalNumberByDividingBy:(ocidDivPt)) |
297 | #計算 |
298 | set ocidDecNo to refMe's NSDecimalNumber's alloc()'s initWithString:(strArgNumMM) |
299 | set ocidRawNo to (ocidDecNo's decimalNumberByDividingBy:(ocidDivNo)) |
300 | #小数点以下切り上げ |
301 | set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init() |
302 | ocidFormatter's setRoundingMode:(refMe's NSNumberFormatterRoundCeiling) |
303 | ocidFormatter's setNumberStyle:(refMe's NSNumberFormatterDecimalStyle) |
304 | ocidFormatter's setMaximumFractionDigits:(4) |
305 | set ocidRoundCeiling to ocidFormatter's stringFromNumber:(ocidRawNo) |
306 | #ポイントサイズ |
307 | set strPtSize to ocidRoundCeiling as text |
308 | set numPtSize to strPtSize as number |
309 | return numPtSize |
310 | end doConversionMM2PT |
311 | |
312 | |
AppleScriptで生成しました |
| 固定リンク
行番号 | ソース |
---|---|
001 | #! /usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | # |
005 | # python3のPillowを利用します インストールが必要です |
006 | # com.cocolog-nifty.quicktimer.icefloe |
007 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
008 | use AppleScript version "2.8" |
009 | use framework "Foundation" |
010 | use framework "AppKit" |
011 | use framework "PDFKit" |
012 | use framework "CoreImage" |
013 | use scripting additions |
014 | |
015 | property refMe : a reference to current application |
016 | |
017 | ################ |
018 | #設定項目 |
019 | #出力サイズmm指定 |
020 | #(四捨五入するので近似値になる) |
021 | property strMMsize : ("30") as text |
022 | #出力解像度PPI |
023 | property numResolution : 288 as integer |
024 | |
025 | |
026 | set appFileManager to refMe's NSFileManager's defaultManager() |
027 | |
028 | ############################# |
029 | #ダイアログ |
030 | set strName to (name of current application) as text |
031 | if strName is "osascript" then |
032 | tell application "Finder" to activate |
033 | else |
034 | tell current application to activate |
035 | end if |
036 | # |
037 | tell application "Finder" |
038 | set aliasPathToMe to (path to me) as alias |
039 | set aliasContainerDirPath to (container of aliasPathToMe) as alias |
040 | set aliasDefaultLocation to (folder "サンプルデータ" of folder aliasContainerDirPath) as alias |
041 | end tell |
042 | |
043 | set listUTI to {"public.delimited-values-text", "public.tab-separated-values-text"} |
044 | set strMes to ("ファイルを選んでください") as text |
045 | set strPrompt to ("TSVタブ区切りテキストファイルを選んでください") as text |
046 | try |
047 | set aliasTSVFilePath 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 |
048 | on error |
049 | log "エラーしました" |
050 | return "エラーしました" |
051 | end try |
052 | ########### |
053 | #パスこのファイル |
054 | set strPathToMe to (POSIX path of aliasPathToMe) as text |
055 | set ocidPathToMeStr to refMe's NSString's stringWithString:(strPathToMe) |
056 | set ocidPathToMe to ocidPathToMeStr's stringByStandardizingPath() |
057 | set ocidPathToMeURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidPathToMe) isDirectory:(false) |
058 | #コンテナ |
059 | set ocidContainerPathURL to ocidPathToMeURL's URLByDeletingLastPathComponent() |
060 | ############# |
061 | #PDF保存先 |
062 | set ocidSavePDFDirPathURL to ocidContainerPathURL's URLByAppendingPathComponent:("SavePDF") isDirectory:(true) |
063 | #フォルダを作っておく |
064 | set appFileManager to refMe's NSFileManager's defaultManager() |
065 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
066 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
067 | set listDone to appFileManager's createDirectoryAtURL:(ocidSavePDFDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference) |
068 | if (item 1 of listDone) is true then |
069 | log "正常処理" |
070 | else if (item 2 of listDone) ≠ (missing value) then |
071 | set strErrorNO to (item 2 of listDone)'s code() as text |
072 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
073 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
074 | return "エラーしました" & strErrorNO & strErrorMes |
075 | end if |
076 | ############# |
077 | #QRコード保存先 |
078 | set ocidSaveQRDirPathURL to ocidContainerPathURL's URLByAppendingPathComponent:("SaveQR") isDirectory:(true) |
079 | #フォルダを作っておく |
080 | set listDone to appFileManager's createDirectoryAtURL:(ocidSaveQRDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference) |
081 | if (item 1 of listDone) is true then |
082 | log "正常処理" |
083 | else if (item 2 of listDone) ≠ (missing value) then |
084 | set strErrorNO to (item 2 of listDone)'s code() as text |
085 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
086 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
087 | return "エラーしました" & strErrorNO & strErrorMes |
088 | end if |
089 | |
090 | ################################ |
091 | #TSVファイル読み込み |
092 | set strTSVFilePath to (POSIX path of aliasTSVFilePath) as text |
093 | set ocidTSVFilePathStr to refMe's NSString's stringWithString:(strTSVFilePath) |
094 | set ocidTSVFilePath to ocidTSVFilePathStr's stringByStandardizingPath() |
095 | set ocidTSVFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidTSVFilePath) isDirectory:false) |
096 | #ファイル読み込み |
097 | set listResponse to (refMe's NSString's alloc()'s initWithContentsOfURL:(ocidTSVFilePathURL) usedEncoding:(refMe's NSUTF8StringEncoding) |error| :(reference)) |
098 | if (item 2 of listResponse) = (missing value) then |
099 | #log "正常処理" |
100 | set ocidReadString to (item 1 of listResponse) |
101 | else if (item 2 of listResponse) ≠ (missing value) then |
102 | set strErrorNO to (item 2 of listDone)'s code() as text |
103 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
104 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
105 | return "エラーしました" & strErrorNO & strErrorMes |
106 | end if |
107 | #改行コードをUNIXに強制 |
108 | set ocidCRChar to refMe's NSString's stringWithString:("\r") |
109 | set ocidLFChar to refMe's NSString's stringWithString:("\n") |
110 | set ocidCRLFChar to refMe's NSString's stringWithString:("\r\n") |
111 | set ocidReadStringsA to ocidReadString's stringByReplacingOccurrencesOfString:(ocidCRLFChar) withString:(ocidLFChar) |
112 | set ocidReadStrings to ocidReadStringsA's stringByReplacingOccurrencesOfString:(ocidCRChar) withString:(ocidLFChar) |
113 | ############# |
114 | #改行毎でリストにする |
115 | set ocidCharSet to (refMe's NSCharacterSet's newlineCharacterSet) |
116 | set ocidLineArray to (ocidReadStrings's componentsSeparatedByCharactersInSet:(ocidCharSet)) |
117 | set numCntLineArray to ocidLineArray's |count|() as integer |
118 | ############# |
119 | #行データの抽出用 |
120 | set ocidEditLineArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
121 | #行順に繰り返す |
122 | repeat with itemIntNo from 1 to (numCntLineArray - 1) by 1 |
123 | #1行分読み込んで |
124 | set ocidLineText to (ocidLineArray's objectAtIndex:(itemIntNo)) |
125 | #タブを区切りにしてリストにする |
126 | set ocidLineRowArray to (ocidLineText's componentsSeparatedByString:("\t")) |
127 | if (ocidLineRowArray's |count|() as integer) = 1 then |
128 | exit repeat |
129 | end if |
130 | #1項目目が氏名 |
131 | set strName to (ocidLineRowArray's objectAtIndex:(0)) as text |
132 | #5項目目がメールアドレス |
133 | set strEmail to (ocidLineRowArray's objectAtIndex:(4)) as text |
134 | #6項目目が電番 |
135 | set strTelNo to (ocidLineRowArray's objectAtIndex:(5)) as text |
136 | ############### |
137 | #項番 |
138 | set strItemNO to ("No: " & itemIntNo) as text |
139 | |
140 | ############### |
141 | #ファイル名は連番+氏名 |
142 | set strSaveFileName to (itemIntNo & "_" & strName) as text |
143 | |
144 | ############### |
145 | #QRイメージの保存先 |
146 | set ocidSaveQRBaseFilePathURL to (ocidSaveQRDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false)) |
147 | set ocidSaveQRFilePathURL to (ocidSaveQRBaseFilePathURL's URLByAppendingPathExtension:("png")) |
148 | ### |
149 | log doMakeQR(strEmail, ocidSaveQRFilePathURL) |
150 | |
151 | #PDFの保存先 |
152 | set ocidSavePDFBaseFilePathURL to (ocidSavePDFDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false)) |
153 | set ocidSavePDFFilePathURL to (ocidSavePDFBaseFilePathURL's URLByAppendingPathExtension:("pdf")) |
154 | |
155 | set strSaveQRFilePath to (ocidSaveQRFilePathURL's |path|()) as text |
156 | set strSavePDFFilePath to (ocidSavePDFFilePathURL's |path|()) as text |
157 | |
158 | set strCommandText to ("\"/usr/bin/python3\" -c \"from PIL import Image; Image.open('" & strSaveQRFilePath & "').save('" & strSavePDFFilePath & "', save_all=True, resolution=" & numResolution & ")\"") as text |
159 | log strCommandText |
160 | try |
161 | do shell script strCommandText |
162 | on error |
163 | return "コマンドでエラーしました" |
164 | end try |
165 | |
166 | end repeat |
167 | |
168 | return "終了" |
169 | |
170 | ################################ |
171 | #QR |
172 | ################################ |
173 | to doMakeQR(argText, argSaveFilePathURL) |
174 | ############## |
175 | #受け取りテキスト |
176 | set strText to argText as text |
177 | #NSString |
178 | set ocidText to refMe's NSString's alloc()'s initWithString:(strText) |
179 | #NSDATA |
180 | set ocidTextData to ocidText's dataUsingEncoding:(refMe's NSUTF8StringEncoding) |
181 | ############## |
182 | #mmをPTに換算 |
183 | set numPtSize to doConversionMM2PT(strMMsize) |
184 | #最終的に出力したいQRコードのサイズ |
185 | set numScaleMax to (numPtSize * (numResolution / 72)) as integer |
186 | |
187 | ############## |
188 | #CIFilter QRコード生成 |
189 | set appQRcodeImage to refMe's CIFilter's filterWithName:("CIQRCodeGenerator") |
190 | appQRcodeImage's setDefaults() |
191 | #テキスト指定 |
192 | appQRcodeImage's setValue:(ocidTextData) forKey:("inputMessage") |
193 | #読み取り誤差値設定L, M, Q, H |
194 | appQRcodeImage's setValue:("Q") forKey:("inputCorrectionLevel") |
195 | #QRコード本体のイメージ |
196 | set ocidCIImage to appQRcodeImage's outputImage() |
197 | |
198 | ############## |
199 | #色の置き換え色 QRコードの出力色になります |
200 | set ocidQRColor to refMe's CIColor's colorWithRed:(0.0) green:(0.0) blue:(0.0) alpha:(1.0) |
201 | #背景透過 |
202 | set ocidBlackColor to refMe's CIColor's colorWithRed:(1.0) green:(1.0) blue:(1.0) alpha:(0.0) |
203 | #QRコードの背景色 透過 |
204 | set ocidArtBoardColor to refMe's NSColor's colorWithRed:(1.0) green:(1.0) blue:(1.0) alpha:(0.0) |
205 | |
206 | #色の置換 |
207 | set appCIFalseColor to refMe's CIFilter's filterWithName:"CIFalseColor" |
208 | appCIFalseColor's setDefaults() |
209 | appCIFalseColor's setValue:(ocidQRColor) forKey:("inputColor0") |
210 | appCIFalseColor's setValue:(ocidBlackColor) forKey:("inputColor1") |
211 | appCIFalseColor's setValue:(ocidCIImage) forKey:("inputImage") |
212 | #色が変わった後のイメージの取り出し |
213 | #set ocidCIImage to ocidFilterColor's valueForKey:"outputImage" |
214 | set ocidCIImage to appCIFalseColor's outputImage() |
215 | |
216 | ############## |
217 | #拡大 まずは作成したQRのピクセルサイズ取得 |
218 | set ocidQRRect to ocidCIImage's extent() |
219 | #必ず正方形なので幅しか取らない |
220 | set ocidCIImageWidth to refMe's NSRect's NSWidth(ocidQRRect) |
221 | #整数で拡大しないとアレなので↑の値のニアなサイズになります |
222 | #出力サイズ÷出来上がりQRコードサイズ÷1で整数に |
223 | set numScale to ((numScaleMax / ocidCIImageWidth) div 1) as integer |
224 | |
225 | ############## |
226 | #変換スケール作成-->拡大 |
227 | set recordScalse to refMe's CGAffineTransform's CGAffineTransformMakeScale(numScale, numScale) |
228 | #変換スケールを適応(元のサイズに元のサイズのスケール適応しても意味ないけど |
229 | set ocidCIImageScaled to ocidCIImage's imageByApplyingTransform:(recordScalse) |
230 | #QRイメージIMAGEREPに |
231 | set ocidCIImageRep to refMe's NSCIImageRep's imageRepWithCIImage:(ocidCIImageScaled) |
232 | #拡大したQRコードのサイズ |
233 | set ocidCIImageSize to ocidCIImageRep's |size|() |
234 | #出力用のNSIMAGEをQRコードのサイズで作成 |
235 | set ocidNSImageScaled to refMe's NSImage's alloc()'s initWithSize:(ocidCIImageSize) |
236 | #QRコードを挿入 |
237 | ocidNSImageScaled's addRepresentation:(ocidCIImageRep) |
238 | #TIFFRepresentation |
239 | set ocidOsDispatchData to ocidNSImageScaled's TIFFRepresentation() |
240 | #NSBitmapImageRep |
241 | set ocidBitmapImageRep to refMe's NSBitmapImageRep's imageRepWithData:(ocidOsDispatchData) |
242 | set numScaledSize to ocidBitmapImageRep's pixelsWide() |
243 | |
244 | ############## |
245 | #quiet zone用に画像をパディングする |
246 | set numArtBoardPtW to ((numScaledSize) + (numScale * 4)) as integer |
247 | #左右に2セル分づつ=縦横4セル分の余白 quiet zoneを足す |
248 | #まずは元のQRコードのサイズに4セルサイズ分足したサイズの画像=出力サイズイメージ |
249 | #アートボードになる出力サイズイメージ |
250 | #CIFilterのQRコードは1セルクワイエットゾーンがあるので2セル足して3セルにする |
251 | set ocidArtBoardRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numArtBoardPtW) pixelsHigh:(numArtBoardPtW) bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:false colorSpaceName:(refMe's NSCalibratedRGBColorSpace) bitmapFormat:(refMe's NSAlphaFirstBitmapFormat) bytesPerRow:0 bitsPerPixel:32) |
252 | |
253 | ############## |
254 | #まずアートボードになる画像と背景色 |
255 | #初期化 |
256 | set appGraphicsContext to (refMe's NSGraphicsContext) |
257 | #編集開始 |
258 | appGraphicsContext's saveGraphicsState() |
259 | #イメージ読み込み |
260 | set ocidReadImageContext to appGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep) |
261 | #出力サイズイメージ読み込み |
262 | appGraphicsContext's setCurrentContext:(ocidReadImageContext) |
263 | #塗り色を『白』に指定して |
264 | ocidArtBoardColor's |set|() |
265 | #画像として色を塗る |
266 | set ocidFillRect to refMe's NSRect's NSMakeRect(0, 0, numArtBoardPtW, numArtBoardPtW) |
267 | refMe's NSRectFill(ocidFillRect) |
268 | #編集終了 |
269 | appGraphicsContext's restoreGraphicsState() |
270 | |
271 | ############## |
272 | set appGraphicsContext to (refMe's NSGraphicsContext) |
273 | #編集開始 |
274 | appGraphicsContext's saveGraphicsState() |
275 | #イメージ読み込み |
276 | set ocidReadImageContext to appGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep) |
277 | #出力サイズイメージ読み込み |
278 | appGraphicsContext's setCurrentContext:(ocidReadImageContext) |
279 | #背景画像の上にQRバーコードの画像をNSCompositeSourceOver |
280 | #位置関係 ペーストする位置 |
281 | set ocidDrawRect to refMe's NSRect's NSMakeRect((numScale * 2), (numScale * 2), numScaledSize, numScaledSize) |
282 | #元画像をコピーしてくる位置 |
283 | set ocidFromRect to refMe's NSRect's NSMakeRect(0, 0, numScaledSize, numScaledSize) |
284 | #NSCompositeSourceOver |
285 | set appOperation to (refMe's NSCompositeSourceOver) |
286 | ocidBitmapImageRep's drawInRect:(ocidDrawRect) fromRect:(ocidFromRect) operation:(appOperation) fraction:1.0 respectFlipped:true hints:(missing value) |
287 | #画像作成終了 |
288 | appGraphicsContext's restoreGraphicsState() |
289 | |
290 | ############## |
291 | #解像度変更 |
292 | #出力画像サイズ |
293 | set ocidQRsize to ocidArtBoardRep's |size|() |
294 | #幅Pt=Pxだけ取得して |
295 | set numPxW to (ocidQRsize's width()) as number |
296 | #設定解像度÷72÷出来上がりサイズで |
297 | # 2なら144ppi 3なら216ppi 4なら288ppi 5なら360ppi |
298 | set numSetSize to (numPxW / (numResolution / 72)) as number |
299 | #サイズを生成して |
300 | set ocidOutPutSize to refMe's NSSize's NSMakeSize(numSetSize, numSetSize) |
301 | #サイズを設定(ここで設定するのはPTサイズ) |
302 | ocidArtBoardRep's setSize:(ocidOutPutSize) |
303 | |
304 | ############## |
305 | #PNG保存のプロパティ |
306 | set ocidPropertiesDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
307 | ocidPropertiesDict's setValue:(false) forKey:(refMe's NSImageInterlaced) |
308 | ocidPropertiesDict's setValue:(1.0 / 2.2) forKey:(refMe's NSImageGamma) |
309 | ocidPropertiesDict's setValue:(missing value) forKey:(refMe's NSImageIPTCData) |
310 | ocidPropertiesDict's setValue:(missing value) forKey:(refMe's NSImageEXIFData) |
311 | #出力イメージへ変換 |
312 | set ocidType to (refMe's NSBitmapImageFileTypePNG) |
313 | set ocidSaveImageData to ocidArtBoardRep's representationUsingType:(ocidType) |properties|:(ocidPropertiesDict) |
314 | |
315 | ############## |
316 | #保存 |
317 | set ocidOption to (refMe's NSDataWritingAtomic) |
318 | set listDone to ocidSaveImageData's writeToURL:(argSaveFilePathURL) options:(ocidOption) |error| :(reference) |
319 | if (item 1 of listDone) is true then |
320 | return true |
321 | else if (item 2 of listDone) ≠ (missing value) then |
322 | set strErrorNO to (item 2 of listDone)'s code() as text |
323 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
324 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
325 | log "エラーしました" & strErrorNO & strErrorMes |
326 | return false |
327 | end if |
328 | return true |
329 | end doMakeQR |
330 | |
331 | ################################ |
332 | #日付生成(今回は使わない) |
333 | set strDateText to doGetDateNo() |
334 | |
335 | ################################ |
336 | # 日付 doGetDateNo() |
337 | ################################ |
338 | to doGetDateNo() |
339 | #戻す日付テキスト |
340 | set ocidRetuenString to refMe's NSMutableString's alloc()'s initWithCapacity:(0) |
341 | ###今日 |
342 | set ocidDate to (current application's NSDate's |date|()) |
343 | ###フォーマット初期化 |
344 | set ocidFormatterJP to current application's NSDateFormatter's alloc()'s init() |
345 | ###日本のカレンダー |
346 | set ocidCalendarID to (current application's NSCalendarIdentifierJapanese) |
347 | set ocidCalendarJP to current application's NSCalendar's alloc()'s initWithCalendarIdentifier:(ocidCalendarID) |
348 | ###東京タイムゾーン |
349 | set ocidTimezoneJP to current application's NSTimeZone's alloc()'s initWithName:("Asia/Tokyo") |
350 | ###日本語 |
351 | set ocidLocaleJP to current application's NSLocale's alloc()'s initWithLocaleIdentifier:("ja_JP_POSIX") |
352 | ###フォーマットをセット(年号) |
353 | ocidFormatterJP's setTimeZone:(ocidTimezoneJP) |
354 | ocidFormatterJP's setLocale:(ocidLocaleJP) |
355 | ocidFormatterJP's setCalendar:(ocidCalendarJP) |
356 | ocidFormatterJP's setDateStyle:(current application's NSDateFormatterFullStyle) |
357 | set strDateFormat to (" ㋿y年") |
358 | ocidFormatterJP's setDateFormat:(strDateFormat) |
359 | set ocidDateStringEra to ocidFormatterJP's stringFromDate:(ocidDate) |
360 | ocidRetuenString's appendString:(ocidDateStringEra) |
361 | ###フォマットから月合字を取得(使わない) |
362 | set recordMonth to {|01月|:"㋀", |02月|:"㋁", |03月|:"㋂", |04月|:"㋃", |05月|:"㋄", |06月|:"㋅", |07月|:"㋆", |08月|:"㋇", |09月|:"㋈", |10月|:"㋉", |11月|:"㋊", |12月|:"㋋"} as record |
363 | set ocidMonthDict to refMe's NSMutableDictionary's alloc()'s initWithDictionary:(recordMonth) |
364 | set strDateFormat to ("MM月") |
365 | ocidFormatterJP's setDateFormat:(strDateFormat) |
366 | set ocidDateStringMonth to ocidFormatterJP's stringFromDate:(ocidDate) |
367 | #set ocidMonthValue to ocidMonthDict's valueForKey:(ocidDateStringMonth) |
368 | ocidRetuenString's appendString:(ocidDateStringMonth) |
369 | ##フォーマットから日付を取得(使わない) |
370 | set recordDate to {|01|:"㏠", |02|:"㏡", |03|:"㏢", |04|:"㏣", |05|:"㏤", |06|:"㏥", |07|:"㏦", |08|:"㏧", |09|:"㏨", |10|:"㏩", |11|:"㏪", |12|:"㏫", |13|:"㏬", |14|:"㏭", |15|:"㏮", |16|:"㏯", |17|:"㏰", |18|:"㏱", |19|:"㏲", |20|:"㏳", |21|:"㏴", |22|:"㏵", |23|:"㏶", |24|:"㏷", |25|:"㏸", |26|:"㏹", |27|:"㏺", |28|:"㏻", |29|:"㏼", |30|:"㏽", |31|:"㏾"} as record |
371 | set ocidDateDict to refMe's NSMutableDictionary's alloc()'s initWithDictionary:(recordDate) |
372 | set strDateFormat to ("dd日 ") |
373 | ocidFormatterJP's setDateFormat:(strDateFormat) |
374 | set ocidDateStringDate to ocidFormatterJP's stringFromDate:(ocidDate) |
375 | # set ocidDateValue to ocidDateDict's valueForKey:(ocidDateStringDate) |
376 | ocidRetuenString's appendString:(ocidDateStringDate) |
377 | |
378 | set strDateAndTime to ((ocidRetuenString as text)) as text |
379 | ###テキストで戻す |
380 | return strDateAndTime |
381 | end doGetDateNo |
382 | |
383 | |
384 | |
385 | |
386 | ############################ |
387 | # mmをPTに換算 |
388 | ############################ |
389 | to doConversionMM2PT(argNumMM) |
390 | set strArgNumMM to argNumMM as text |
391 | #指数 |
392 | set ocidCm2InchNo to refMe's NSDecimalNumber's alloc()'s initWithString:("25.4") |
393 | set ocidDivPt to refMe's NSDecimalNumber's alloc()'s initWithString:("72") |
394 | set ocidDivNo to (ocidCm2InchNo's decimalNumberByDividingBy:(ocidDivPt)) |
395 | #計算 |
396 | set ocidDecNo to refMe's NSDecimalNumber's alloc()'s initWithString:(strArgNumMM) |
397 | set ocidRawNo to (ocidDecNo's decimalNumberByDividingBy:(ocidDivNo)) |
398 | #小数点以下切り上げ |
399 | set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init() |
400 | ocidFormatter's setRoundingMode:(refMe's NSNumberFormatterRoundCeiling) |
401 | ocidFormatter's setNumberStyle:(refMe's NSNumberFormatterDecimalStyle) |
402 | ocidFormatter's setMaximumFractionDigits:(0) |
403 | set ocidRoundCeiling to ocidFormatter's stringFromNumber:(ocidRawNo) |
404 | #ポイントサイズ |
405 | set numPtSize to ocidRoundCeiling as integer |
406 | return numPtSize |
407 | |
408 | end doConversionMM2PT |
409 | |
410 | |
AppleScriptで生成しました |
| 固定リンク
行番号 | ソース |
---|---|
001 | #! /usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | # CMYKカラーのQRコードを生成する |
005 | # 黒はリッチブラックになるので留意 |
006 | # python3のPillowを利用します インストールが必要です |
007 | # com.cocolog-nifty.quicktimer.icefloe |
008 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
009 | use AppleScript version "2.8" |
010 | use framework "Foundation" |
011 | use framework "AppKit" |
012 | use framework "PDFKit" |
013 | use framework "CoreImage" |
014 | use scripting additions |
015 | |
016 | property refMe : a reference to current application |
017 | |
018 | ################ |
019 | #設定項目 |
020 | #出力サイズmm指定 |
021 | #(四捨五入するので近似値になる) |
022 | property strMMsize : ("30") as text |
023 | #出力解像度PPI |
024 | property numResolution : 288 as integer |
025 | |
026 | |
027 | set appFileManager to refMe's NSFileManager's defaultManager() |
028 | |
029 | ############################# |
030 | #ダイアログ |
031 | set strName to (name of current application) as text |
032 | if strName is "osascript" then |
033 | tell application "Finder" to activate |
034 | else |
035 | tell current application to activate |
036 | end if |
037 | # |
038 | tell application "Finder" |
039 | set aliasPathToMe to (path to me) as alias |
040 | set aliasContainerDirPath to (container of aliasPathToMe) as alias |
041 | set aliasDefaultLocation to (folder "サンプルデータ" of folder aliasContainerDirPath) as alias |
042 | end tell |
043 | |
044 | set listUTI to {"public.delimited-values-text", "public.tab-separated-values-text"} |
045 | set strMes to ("ファイルを選んでください") as text |
046 | set strPrompt to ("TSVタブ区切りテキストファイルを選んでください") as text |
047 | try |
048 | set aliasTSVFilePath 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 | #パスこのファイル |
055 | set strPathToMe to (POSIX path of aliasPathToMe) as text |
056 | set ocidPathToMeStr to refMe's NSString's stringWithString:(strPathToMe) |
057 | set ocidPathToMe to ocidPathToMeStr's stringByStandardizingPath() |
058 | set ocidPathToMeURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidPathToMe) isDirectory:(false) |
059 | #コンテナ |
060 | set ocidContainerPathURL to ocidPathToMeURL's URLByDeletingLastPathComponent() |
061 | ############# |
062 | #PDF保存先 |
063 | set ocidSavePDFDirPathURL to ocidContainerPathURL's URLByAppendingPathComponent:("SavePDF") isDirectory:(true) |
064 | #フォルダを作っておく |
065 | set appFileManager to refMe's NSFileManager's defaultManager() |
066 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
067 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
068 | set listDone to appFileManager's createDirectoryAtURL:(ocidSavePDFDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference) |
069 | if (item 1 of listDone) is true then |
070 | log "正常処理" |
071 | else if (item 2 of listDone) ≠ (missing value) then |
072 | set strErrorNO to (item 2 of listDone)'s code() as text |
073 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
074 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
075 | return "エラーしました" & strErrorNO & strErrorMes |
076 | end if |
077 | ############# |
078 | #QRコード保存先 |
079 | set ocidSaveQRDirPathURL to ocidContainerPathURL's URLByAppendingPathComponent:("SaveQR") isDirectory:(true) |
080 | #フォルダを作っておく |
081 | set listDone to appFileManager's createDirectoryAtURL:(ocidSaveQRDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference) |
082 | if (item 1 of listDone) is true then |
083 | log "正常処理" |
084 | else if (item 2 of listDone) ≠ (missing value) then |
085 | set strErrorNO to (item 2 of listDone)'s code() as text |
086 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
087 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
088 | return "エラーしました" & strErrorNO & strErrorMes |
089 | end if |
090 | |
091 | ################################ |
092 | #TSVファイル読み込み |
093 | set strTSVFilePath to (POSIX path of aliasTSVFilePath) as text |
094 | set ocidTSVFilePathStr to refMe's NSString's stringWithString:(strTSVFilePath) |
095 | set ocidTSVFilePath to ocidTSVFilePathStr's stringByStandardizingPath() |
096 | set ocidTSVFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidTSVFilePath) isDirectory:false) |
097 | #ファイル読み込み |
098 | set listResponse to (refMe's NSString's alloc()'s initWithContentsOfURL:(ocidTSVFilePathURL) usedEncoding:(refMe's NSUTF8StringEncoding) |error| :(reference)) |
099 | if (item 2 of listResponse) = (missing value) then |
100 | #log "正常処理" |
101 | set ocidReadString to (item 1 of listResponse) |
102 | else if (item 2 of listResponse) ≠ (missing value) then |
103 | set strErrorNO to (item 2 of listDone)'s code() as text |
104 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
105 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
106 | return "エラーしました" & strErrorNO & strErrorMes |
107 | end if |
108 | #改行コードをUNIXに強制 |
109 | set ocidCRChar to refMe's NSString's stringWithString:("\r") |
110 | set ocidLFChar to refMe's NSString's stringWithString:("\n") |
111 | set ocidCRLFChar to refMe's NSString's stringWithString:("\r\n") |
112 | set ocidReadStringsA to ocidReadString's stringByReplacingOccurrencesOfString:(ocidCRLFChar) withString:(ocidLFChar) |
113 | set ocidReadStrings to ocidReadStringsA's stringByReplacingOccurrencesOfString:(ocidCRChar) withString:(ocidLFChar) |
114 | |
115 | ############# |
116 | #改行毎でリストにする |
117 | set ocidCharSet to (refMe's NSCharacterSet's newlineCharacterSet) |
118 | set ocidLineArray to (ocidReadStrings's componentsSeparatedByCharactersInSet:(ocidCharSet)) |
119 | set numCntLineArray to ocidLineArray's |count|() as integer |
120 | |
121 | ############# |
122 | #行データの抽出用 |
123 | set ocidEditLineArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
124 | #行順に繰り返す |
125 | repeat with itemIntNo from 1 to (numCntLineArray - 1) by 1 |
126 | #1行分読み込んで |
127 | set ocidLineText to (ocidLineArray's objectAtIndex:(itemIntNo)) |
128 | #タブを区切りにしてリストにする |
129 | set ocidLineRowArray to (ocidLineText's componentsSeparatedByString:("\t")) |
130 | if (ocidLineRowArray's |count|() as integer) = 1 then |
131 | exit repeat |
132 | end if |
133 | #1項目目が氏名 |
134 | set strName to (ocidLineRowArray's objectAtIndex:(0)) as text |
135 | #5項目目がメールアドレス |
136 | set strEmail to (ocidLineRowArray's objectAtIndex:(4)) as text |
137 | #6項目目が電番 |
138 | set strTelNo to (ocidLineRowArray's objectAtIndex:(5)) as text |
139 | ############### |
140 | #項番 |
141 | set strItemNO to ("No: " & itemIntNo) as text |
142 | |
143 | ############### |
144 | #ファイル名は連番+氏名 |
145 | set strSaveFileName to (itemIntNo & "_" & strName) as text |
146 | |
147 | ############### |
148 | #QRイメージの保存先 |
149 | set ocidSaveQRBaseFilePathURL to (ocidSaveQRDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false)) |
150 | set ocidSaveQRFilePathURL to (ocidSaveQRBaseFilePathURL's URLByAppendingPathExtension:("tif")) |
151 | ### |
152 | log doMakeQR(strEmail, ocidSaveQRFilePathURL) |
153 | |
154 | ############### |
155 | #PDFの保存先 |
156 | set ocidSavePDFBaseFilePathURL to (ocidSavePDFDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false)) |
157 | set ocidSavePDFFilePathURL to (ocidSavePDFBaseFilePathURL's URLByAppendingPathExtension:("pdf")) |
158 | set strSavePDFFilePath to (ocidSavePDFFilePathURL's |path|()) as text |
159 | set strSaveQRFilePath to (ocidSaveQRFilePathURL's |path|()) as text |
160 | |
161 | # |
162 | set strCommandText to ("\"/usr/bin/python3\" -c \"from PIL import Image; Image.open('" & strSaveQRFilePath & "').save('" & strSavePDFFilePath & "', save_all=True, resolution=" & numResolution & ")\"") as text |
163 | log strCommandText |
164 | try |
165 | do shell script strCommandText |
166 | on error |
167 | return "コマンドでエラーしました" |
168 | end try |
169 | |
170 | end repeat |
171 | |
172 | return "終了" |
173 | |
174 | ################################ |
175 | #QR |
176 | ################################ |
177 | to doMakeQR(argText, argSaveFilePathURL) |
178 | ############## |
179 | #受け取りテキスト |
180 | set strText to argText as text |
181 | #NSString |
182 | set ocidText to refMe's NSString's alloc()'s initWithString:(strText) |
183 | #NSDATA |
184 | set ocidTextData to ocidText's dataUsingEncoding:(refMe's NSUTF8StringEncoding) |
185 | ############## |
186 | #mmをPTに換算 |
187 | set numPtSize to doConversionMM2PT(strMMsize) |
188 | #最終的に出力したいQRコードのサイズ |
189 | set numScaleMax to (numPtSize * (numResolution / 72)) as integer |
190 | |
191 | ############## |
192 | #CIFilter QRコード生成 |
193 | set appQRcodeImage to refMe's CIFilter's filterWithName:("CIQRCodeGenerator") |
194 | appQRcodeImage's setDefaults() |
195 | #テキスト指定 |
196 | appQRcodeImage's setValue:(ocidTextData) forKey:("inputMessage") |
197 | #読み取り誤差値設定L, M, Q, H |
198 | appQRcodeImage's setValue:("Q") forKey:("inputCorrectionLevel") |
199 | #QRコード本体のイメージ |
200 | set ocidCIImage to appQRcodeImage's outputImage() |
201 | |
202 | ############## |
203 | #色の置き換え色 QRコードの出力色になります |
204 | set ocidQRColor to refMe's CIColor's colorWithRed:(0.0) green:(0.0) blue:(0.0) alpha:(1.0) |
205 | #背景透過 |
206 | set ocidBlackColor to refMe's CIColor's colorWithRed:(1.0) green:(1.0) blue:(1.0) alpha:(0.0) |
207 | #QRコードの背景色 透過 |
208 | set ocidArtBoardColor to refMe's NSColor's colorWithDeviceCyan:(0.0) magenta:(0.0) yellow:(0.0) black:(0.0) alpha:(1.0) |
209 | |
210 | #色の置換 RGB |
211 | |
212 | set appCIFalseColor to refMe's CIFilter's filterWithName:"CIFalseColor" |
213 | appCIFalseColor's setDefaults() |
214 | appCIFalseColor's setValue:(ocidQRColor) forKey:("inputColor0") |
215 | appCIFalseColor's setValue:(ocidBlackColor) forKey:("inputColor1") |
216 | appCIFalseColor's setValue:(ocidCIImage) forKey:(refMe's kCIInputImageKey) |
217 | #色が変わった後のイメージの取り出し |
218 | #set ocidCIImage to ocidFilterColor's valueForKey:(refMe's kCIOutputImageKey) |
219 | set ocidCIImage to appCIFalseColor's outputImage() |
220 | |
221 | |
222 | ############## |
223 | #拡大 まずは作成したQRのピクセルサイズ取得 |
224 | set ocidQRRect to ocidCIImage's extent() |
225 | #必ず正方形なので幅しか取らない |
226 | set ocidCIImageWidth to refMe's NSRect's NSWidth(ocidQRRect) |
227 | #整数で拡大しないとアレなので↑の値のニアなサイズになります |
228 | #出力サイズ÷出来上がりQRコードサイズ÷1で整数に |
229 | set numScale to ((numScaleMax / ocidCIImageWidth) div 1) as integer |
230 | |
231 | ############## |
232 | #変換スケール作成-->拡大 |
233 | set recordScalse to refMe's CGAffineTransform's CGAffineTransformMakeScale(numScale, numScale) |
234 | #変換スケールを適応(元のサイズに元のサイズのスケール適応しても意味ないけど |
235 | set ocidCIImageScaled to ocidCIImage's imageByApplyingTransform:(recordScalse) |
236 | #QRイメージIMAGEREPに |
237 | set ocidCIImageRep to refMe's NSCIImageRep's imageRepWithCIImage:(ocidCIImageScaled) |
238 | #拡大したQRコードのサイズ |
239 | set ocidCIImageSize to ocidCIImageRep's |size|() |
240 | #出力用のNSIMAGEをQRコードのサイズで作成 |
241 | set ocidNSImageScaled to refMe's NSImage's alloc()'s initWithSize:(ocidCIImageSize) |
242 | #QRコードを挿入 |
243 | ocidNSImageScaled's addRepresentation:(ocidCIImageRep) |
244 | #TIFFRepresentation |
245 | set ocidOsDispatchData to ocidNSImageScaled's TIFFRepresentation() |
246 | #NSBitmapImageRep |
247 | set ocidBitmapImageRep to refMe's NSBitmapImageRep's imageRepWithData:(ocidOsDispatchData) |
248 | set numScaledSize to ocidBitmapImageRep's pixelsWide() |
249 | |
250 | ############## |
251 | #quiet zone用に画像をパディングする |
252 | set numArtBoardPtW to ((numScaledSize) + (numScale * 4)) as integer |
253 | #左右に2セル分づつ=縦横4セル分の余白 quiet zoneを足す |
254 | #まずは元のQRコードのサイズに4セルサイズ分足したサイズの画像=出力サイズイメージ |
255 | #アートボードになる出力サイズイメージ |
256 | #CIFilterのQRコードは1セルクワイエットゾーンがあるので2セル足して3セルにする |
257 | set ocidColorSpaceName to (refMe's NSDeviceCMYKColorSpace) |
258 | set ocidBitmapFormat to (refMe's NSBitmapFormatAlphaFirst) |
259 | set ocidArtBoardRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numArtBoardPtW) pixelsHigh:(numArtBoardPtW) bitsPerSample:8 samplesPerPixel:4 hasAlpha:false isPlanar:false colorSpaceName:(ocidColorSpaceName) bitmapFormat:(ocidBitmapFormat) bytesPerRow:0 bitsPerPixel:32) |
260 | |
261 | |
262 | ############## |
263 | #ColorSync |
264 | set strICCProfilesFilePath to ("/System/Library/ColorSync/Profiles/Generic CMYK Profile.icc") as text |
265 | set ocidICCProfilesFilePathStr to refMe's NSString's stringWithString:(strICCProfilesFilePath) |
266 | set ocidICCProfilesFilePath to ocidICCProfilesFilePathStr's stringByStandardizingPath() |
267 | set ocidICCProfilesFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidICCProfilesFilePath) isDirectory:false) |
268 | #NSData's |
269 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
270 | set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidICCProfilesFilePathURL) options:(ocidOption) |error| :(reference) |
271 | if (item 2 of listResponse) = (missing value) then |
272 | log "正常処理" |
273 | set ocidICCProfilesData to (item 1 of listResponse) |
274 | else if (item 2 of listResponse) ≠ (missing value) then |
275 | set strErrorNO to (item 2 of listDone)'s code() as text |
276 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
277 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
278 | return "エラーしました" & strErrorNO & strErrorMes |
279 | end if |
280 | ##プロファイルをセット |
281 | ocidArtBoardRep's setProperty:(refMe's NSImageColorSyncProfileData) withValue:(ocidICCProfilesData) |
282 | |
283 | ############## |
284 | #まずアートボードになる画像と背景色 |
285 | #初期化 |
286 | set appGraphicsContext to (refMe's NSGraphicsContext) |
287 | #編集開始 |
288 | appGraphicsContext's saveGraphicsState() |
289 | #イメージ読み込み |
290 | set ocidReadImageContext to appGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep) |
291 | #出力サイズイメージ読み込み |
292 | appGraphicsContext's setCurrentContext:(ocidReadImageContext) |
293 | #塗り色を設定値指定して |
294 | ocidArtBoardColor's |set|() |
295 | #画像として色を塗る |
296 | set ocidFillRect to refMe's NSRect's NSMakeRect(0, 0, numArtBoardPtW, numArtBoardPtW) |
297 | refMe's NSRectFill(ocidFillRect) |
298 | #編集終了 |
299 | appGraphicsContext's restoreGraphicsState() |
300 | |
301 | ############## |
302 | set appGraphicsContext to (refMe's NSGraphicsContext) |
303 | #編集開始 |
304 | appGraphicsContext's saveGraphicsState() |
305 | #イメージ読み込み |
306 | set ocidReadImageContext to appGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep) |
307 | #出力サイズイメージ読み込み |
308 | appGraphicsContext's setCurrentContext:(ocidReadImageContext) |
309 | #背景画像の上にQRバーコードの画像をNSCompositeSourceOver |
310 | #位置関係 ペーストする位置 |
311 | set ocidDrawRect to refMe's NSRect's NSMakeRect((numScale * 2), (numScale * 2), numScaledSize, numScaledSize) |
312 | #元画像をコピーしてくる位置 |
313 | set ocidFromRect to refMe's NSRect's NSMakeRect(0, 0, numScaledSize, numScaledSize) |
314 | #NSCompositeSourceOver |
315 | set appOperation to (refMe's NSCompositeSourceOver) |
316 | ocidBitmapImageRep's drawInRect:(ocidDrawRect) fromRect:(ocidFromRect) operation:(appOperation) fraction:1.0 respectFlipped:true hints:(missing value) |
317 | #画像作成終了 |
318 | appGraphicsContext's restoreGraphicsState() |
319 | |
320 | ############## |
321 | #解像度変更 |
322 | #出力画像サイズ |
323 | set ocidQRsize to ocidArtBoardRep's |size|() |
324 | #幅Pt=Pxだけ取得して |
325 | set numPxW to (ocidQRsize's width()) as number |
326 | #設定解像度÷72÷出来上がりサイズで |
327 | # 2なら144ppi 3なら216ppi 4なら288ppi 5なら360ppi |
328 | set numSetSize to (numPxW / (numResolution / 72)) as number |
329 | #サイズを生成して |
330 | set ocidOutPutSize to refMe's NSSize's NSMakeSize(numSetSize, numSetSize) |
331 | #サイズを設定(ここで設定するのはPTサイズ) |
332 | ocidArtBoardRep's setSize:(ocidOutPutSize) |
333 | |
334 | ############## |
335 | #TIFF保存のプロパティ |
336 | set ocidPropertiesDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
337 | ocidPropertiesDict's setValue:(0.0) forKey:(refMe's NSImageCompressionFactor) |
338 | ocidPropertiesDict's setValue:(missing value) forKey:(refMe's NSImageCompressionMethod) |
339 | ocidPropertiesDict's setValue:(missing value) forKey:(refMe's NSImageIPTCData) |
340 | ocidPropertiesDict's setValue:(missing value) forKey:(refMe's NSImageEXIFData) |
341 | ocidPropertiesDict's setValue:(ocidICCProfilesData) forKey:(refMe's NSImageColorSyncProfileData) |
342 | |
343 | #出力イメージへ変換 |
344 | set ocidType to (refMe's NSBitmapImageFileTypeTIFF) |
345 | set ocidSaveImageData to ocidArtBoardRep's representationUsingType:(ocidType) |properties|:(ocidPropertiesDict) |
346 | |
347 | |
348 | ############## |
349 | #保存 |
350 | set ocidOption to (refMe's NSDataWritingAtomic) |
351 | set listDone to ocidSaveImageData's writeToURL:(argSaveFilePathURL) options:(ocidOption) |error| :(reference) |
352 | if (item 1 of listDone) is true then |
353 | return true |
354 | else if (item 2 of listDone) ≠ (missing value) then |
355 | set strErrorNO to (item 2 of listDone)'s code() as text |
356 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
357 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
358 | log "エラーしました" & strErrorNO & strErrorMes |
359 | return false |
360 | end if |
361 | return true |
362 | end doMakeQR |
363 | |
364 | ################################ |
365 | #日付生成(今回は使わない) |
366 | set strDateText to doGetDateNo() |
367 | |
368 | ################################ |
369 | # 日付 doGetDateNo() |
370 | ################################ |
371 | to doGetDateNo() |
372 | #戻す日付テキスト |
373 | set ocidRetuenString to refMe's NSMutableString's alloc()'s initWithCapacity:(0) |
374 | ###今日 |
375 | set ocidDate to (current application's NSDate's |date|()) |
376 | ###フォーマット初期化 |
377 | set ocidFormatterJP to current application's NSDateFormatter's alloc()'s init() |
378 | ###日本のカレンダー |
379 | set ocidCalendarID to (current application's NSCalendarIdentifierJapanese) |
380 | set ocidCalendarJP to current application's NSCalendar's alloc()'s initWithCalendarIdentifier:(ocidCalendarID) |
381 | ###東京タイムゾーン |
382 | set ocidTimezoneJP to current application's NSTimeZone's alloc()'s initWithName:("Asia/Tokyo") |
383 | ###日本語 |
384 | set ocidLocaleJP to current application's NSLocale's alloc()'s initWithLocaleIdentifier:("ja_JP_POSIX") |
385 | ###フォーマットをセット(年号) |
386 | ocidFormatterJP's setTimeZone:(ocidTimezoneJP) |
387 | ocidFormatterJP's setLocale:(ocidLocaleJP) |
388 | ocidFormatterJP's setCalendar:(ocidCalendarJP) |
389 | ocidFormatterJP's setDateStyle:(current application's NSDateFormatterFullStyle) |
390 | set strDateFormat to (" ㋿y年") |
391 | ocidFormatterJP's setDateFormat:(strDateFormat) |
392 | set ocidDateStringEra to ocidFormatterJP's stringFromDate:(ocidDate) |
393 | ocidRetuenString's appendString:(ocidDateStringEra) |
394 | ###フォマットから月合字を取得(使わない) |
395 | set recordMonth to {|01月|:"㋀", |02月|:"㋁", |03月|:"㋂", |04月|:"㋃", |05月|:"㋄", |06月|:"㋅", |07月|:"㋆", |08月|:"㋇", |09月|:"㋈", |10月|:"㋉", |11月|:"㋊", |12月|:"㋋"} as record |
396 | set ocidMonthDict to refMe's NSMutableDictionary's alloc()'s initWithDictionary:(recordMonth) |
397 | set strDateFormat to ("MM月") |
398 | ocidFormatterJP's setDateFormat:(strDateFormat) |
399 | set ocidDateStringMonth to ocidFormatterJP's stringFromDate:(ocidDate) |
400 | #set ocidMonthValue to ocidMonthDict's valueForKey:(ocidDateStringMonth) |
401 | ocidRetuenString's appendString:(ocidDateStringMonth) |
402 | ##フォーマットから日付を取得(使わない) |
403 | set recordDate to {|01|:"㏠", |02|:"㏡", |03|:"㏢", |04|:"㏣", |05|:"㏤", |06|:"㏥", |07|:"㏦", |08|:"㏧", |09|:"㏨", |10|:"㏩", |11|:"㏪", |12|:"㏫", |13|:"㏬", |14|:"㏭", |15|:"㏮", |16|:"㏯", |17|:"㏰", |18|:"㏱", |19|:"㏲", |20|:"㏳", |21|:"㏴", |22|:"㏵", |23|:"㏶", |24|:"㏷", |25|:"㏸", |26|:"㏹", |27|:"㏺", |28|:"㏻", |29|:"㏼", |30|:"㏽", |31|:"㏾"} as record |
404 | set ocidDateDict to refMe's NSMutableDictionary's alloc()'s initWithDictionary:(recordDate) |
405 | set strDateFormat to ("dd日 ") |
406 | ocidFormatterJP's setDateFormat:(strDateFormat) |
407 | set ocidDateStringDate to ocidFormatterJP's stringFromDate:(ocidDate) |
408 | # set ocidDateValue to ocidDateDict's valueForKey:(ocidDateStringDate) |
409 | ocidRetuenString's appendString:(ocidDateStringDate) |
410 | |
411 | set strDateAndTime to ((ocidRetuenString as text)) as text |
412 | ###テキストで戻す |
413 | return strDateAndTime |
414 | end doGetDateNo |
415 | |
416 | |
417 | |
418 | |
419 | ############################ |
420 | # mmをPTに換算 |
421 | ############################ |
422 | to doConversionMM2PT(argNumMM) |
423 | set strArgNumMM to argNumMM as text |
424 | #指数 |
425 | set ocidCm2InchNo to refMe's NSDecimalNumber's alloc()'s initWithString:("25.4") |
426 | set ocidDivPt to refMe's NSDecimalNumber's alloc()'s initWithString:("72") |
427 | set ocidDivNo to (ocidCm2InchNo's decimalNumberByDividingBy:(ocidDivPt)) |
428 | #計算 |
429 | set ocidDecNo to refMe's NSDecimalNumber's alloc()'s initWithString:(strArgNumMM) |
430 | set ocidRawNo to (ocidDecNo's decimalNumberByDividingBy:(ocidDivNo)) |
431 | #小数点以下切り上げ |
432 | set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init() |
433 | ocidFormatter's setRoundingMode:(refMe's NSNumberFormatterRoundCeiling) |
434 | ocidFormatter's setNumberStyle:(refMe's NSNumberFormatterDecimalStyle) |
435 | ocidFormatter's setMaximumFractionDigits:(0) |
436 | set ocidRoundCeiling to ocidFormatter's stringFromNumber:(ocidRawNo) |
437 | #ポイントサイズ |
438 | set numPtSize to ocidRoundCeiling as integer |
439 | return numPtSize |
440 | |
441 | end doConversionMM2PT |
442 | |
443 | |
AppleScriptで生成しました |
| 固定リンク
行番号 | ソース |
---|---|
001 | #! /usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | # CMYKカラーのQRコードを生成する |
005 | # 黒はリッチブラックになるので留意 |
006 | # python3のPillowを利用します インストールが必要です |
007 | # com.cocolog-nifty.quicktimer.icefloe |
008 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
009 | use AppleScript version "2.8" |
010 | use framework "Foundation" |
011 | use framework "AppKit" |
012 | use framework "PDFKit" |
013 | use framework "CoreImage" |
014 | use scripting additions |
015 | |
016 | property refMe : a reference to current application |
017 | |
018 | ################ |
019 | #設定項目 |
020 | #出力サイズmm指定 |
021 | #(四捨五入するので近似値になる) |
022 | property strMMsize : ("30") as text |
023 | #出力解像度PPI |
024 | property numResolution : 288 as integer |
025 | |
026 | |
027 | set appFileManager to refMe's NSFileManager's defaultManager() |
028 | |
029 | ############################# |
030 | #ダイアログ |
031 | set strName to (name of current application) as text |
032 | if strName is "osascript" then |
033 | tell application "Finder" to activate |
034 | else |
035 | tell current application to activate |
036 | end if |
037 | # |
038 | tell application "Finder" |
039 | set aliasPathToMe to (path to me) as alias |
040 | set aliasContainerDirPath to (container of aliasPathToMe) as alias |
041 | set aliasDefaultLocation to (folder "サンプルデータ" of folder aliasContainerDirPath) as alias |
042 | end tell |
043 | |
044 | set listUTI to {"public.delimited-values-text", "public.tab-separated-values-text"} |
045 | set strMes to ("ファイルを選んでください") as text |
046 | set strPrompt to ("TSVタブ区切りテキストファイルを選んでください") as text |
047 | try |
048 | set aliasTSVFilePath 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 | #パスこのファイル |
055 | set strPathToMe to (POSIX path of aliasPathToMe) as text |
056 | set ocidPathToMeStr to refMe's NSString's stringWithString:(strPathToMe) |
057 | set ocidPathToMe to ocidPathToMeStr's stringByStandardizingPath() |
058 | set ocidPathToMeURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidPathToMe) isDirectory:(false) |
059 | #コンテナ |
060 | set ocidContainerPathURL to ocidPathToMeURL's URLByDeletingLastPathComponent() |
061 | ############# |
062 | #PDF保存先 |
063 | set ocidSavePDFDirPathURL to ocidContainerPathURL's URLByAppendingPathComponent:("SavePDF") isDirectory:(true) |
064 | #フォルダを作っておく |
065 | set appFileManager to refMe's NSFileManager's defaultManager() |
066 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
067 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
068 | set listDone to appFileManager's createDirectoryAtURL:(ocidSavePDFDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference) |
069 | if (item 1 of listDone) is true then |
070 | log "正常処理" |
071 | else if (item 2 of listDone) ≠ (missing value) then |
072 | set strErrorNO to (item 2 of listDone)'s code() as text |
073 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
074 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
075 | return "エラーしました" & strErrorNO & strErrorMes |
076 | end if |
077 | ############# |
078 | #QRコード保存先 |
079 | set ocidSaveQRDirPathURL to ocidContainerPathURL's URLByAppendingPathComponent:("SaveQR") isDirectory:(true) |
080 | #フォルダを作っておく |
081 | set listDone to appFileManager's createDirectoryAtURL:(ocidSaveQRDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference) |
082 | if (item 1 of listDone) is true then |
083 | log "正常処理" |
084 | else if (item 2 of listDone) ≠ (missing value) then |
085 | set strErrorNO to (item 2 of listDone)'s code() as text |
086 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
087 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
088 | return "エラーしました" & strErrorNO & strErrorMes |
089 | end if |
090 | |
091 | ################################ |
092 | #TSVファイル読み込み |
093 | set strTSVFilePath to (POSIX path of aliasTSVFilePath) as text |
094 | set ocidTSVFilePathStr to refMe's NSString's stringWithString:(strTSVFilePath) |
095 | set ocidTSVFilePath to ocidTSVFilePathStr's stringByStandardizingPath() |
096 | set ocidTSVFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidTSVFilePath) isDirectory:false) |
097 | #ファイル読み込み |
098 | set listResponse to (refMe's NSString's alloc()'s initWithContentsOfURL:(ocidTSVFilePathURL) usedEncoding:(refMe's NSUTF8StringEncoding) |error| :(reference)) |
099 | if (item 2 of listResponse) = (missing value) then |
100 | #log "正常処理" |
101 | set ocidReadString to (item 1 of listResponse) |
102 | else if (item 2 of listResponse) ≠ (missing value) then |
103 | set strErrorNO to (item 2 of listDone)'s code() as text |
104 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
105 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
106 | return "エラーしました" & strErrorNO & strErrorMes |
107 | end if |
108 | #改行コードをUNIXに強制 |
109 | set ocidCRChar to refMe's NSString's stringWithString:("\r") |
110 | set ocidLFChar to refMe's NSString's stringWithString:("\n") |
111 | set ocidCRLFChar to refMe's NSString's stringWithString:("\r\n") |
112 | set ocidReadStringsA to ocidReadString's stringByReplacingOccurrencesOfString:(ocidCRLFChar) withString:(ocidLFChar) |
113 | set ocidReadStrings to ocidReadStringsA's stringByReplacingOccurrencesOfString:(ocidCRChar) withString:(ocidLFChar) |
114 | |
115 | ############# |
116 | #改行毎でリストにする |
117 | set ocidCharSet to (refMe's NSCharacterSet's newlineCharacterSet) |
118 | set ocidLineArray to (ocidReadStrings's componentsSeparatedByCharactersInSet:(ocidCharSet)) |
119 | set numCntLineArray to ocidLineArray's |count|() as integer |
120 | |
121 | ############# |
122 | #行データの抽出用 |
123 | set ocidEditLineArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
124 | #行順に繰り返す |
125 | repeat with itemIntNo from 1 to (numCntLineArray - 1) by 1 |
126 | #1行分読み込んで |
127 | set ocidLineText to (ocidLineArray's objectAtIndex:(itemIntNo)) |
128 | #タブを区切りにしてリストにする |
129 | set ocidLineRowArray to (ocidLineText's componentsSeparatedByString:("\t")) |
130 | if (ocidLineRowArray's |count|() as integer) = 1 then |
131 | exit repeat |
132 | end if |
133 | #1項目目が氏名 |
134 | set strName to (ocidLineRowArray's objectAtIndex:(0)) as text |
135 | #5項目目がメールアドレス |
136 | set strEmail to (ocidLineRowArray's objectAtIndex:(4)) as text |
137 | #6項目目が電番 |
138 | set strTelNo to (ocidLineRowArray's objectAtIndex:(5)) as text |
139 | ############### |
140 | #項番 |
141 | set strItemNO to ("No: " & itemIntNo) as text |
142 | |
143 | ############### |
144 | #ファイル名は連番+氏名 |
145 | set strSaveFileName to (itemIntNo & "_" & strName) as text |
146 | |
147 | ############### |
148 | #QRイメージの保存先 |
149 | set ocidSaveQRBaseFilePathURL to (ocidSaveQRDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false)) |
150 | set ocidSaveQRFilePathURL to (ocidSaveQRBaseFilePathURL's URLByAppendingPathExtension:("tif")) |
151 | ### |
152 | log doMakeQR(strEmail, ocidSaveQRFilePathURL) |
153 | |
154 | ############### |
155 | #PDFの保存先 |
156 | set ocidSavePDFBaseFilePathURL to (ocidSavePDFDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false)) |
157 | set ocidSavePDFFilePathURL to (ocidSavePDFBaseFilePathURL's URLByAppendingPathExtension:("pdf")) |
158 | set strSavePDFFilePath to (ocidSavePDFFilePathURL's |path|()) as text |
159 | set strSaveQRFilePath to (ocidSaveQRFilePathURL's |path|()) as text |
160 | |
161 | # |
162 | set strCommandText to ("\"/usr/bin/python3\" -c \"from PIL import Image; Image.open('" & strSaveQRFilePath & "').save('" & strSavePDFFilePath & "', save_all=True, resolution=" & numResolution & ")\"") as text |
163 | log strCommandText |
164 | try |
165 | do shell script strCommandText |
166 | on error |
167 | return "コマンドでエラーしました" |
168 | end try |
169 | |
170 | end repeat |
171 | |
172 | return "終了" |
173 | |
174 | ################################ |
175 | #QR |
176 | ################################ |
177 | to doMakeQR(argText, argSaveFilePathURL) |
178 | ############## |
179 | #受け取りテキスト |
180 | set strText to argText as text |
181 | #NSString |
182 | set ocidText to refMe's NSString's alloc()'s initWithString:(strText) |
183 | #NSDATA |
184 | set ocidTextData to ocidText's dataUsingEncoding:(refMe's NSUTF8StringEncoding) |
185 | ############## |
186 | #mmをPTに換算 |
187 | set numPtSize to doConversionMM2PT(strMMsize) |
188 | #最終的に出力したいQRコードのサイズ |
189 | set numScaleMax to (numPtSize * (numResolution / 72)) as integer |
190 | |
191 | ############## |
192 | #CIFilter QRコード生成 |
193 | set appQRcodeImage to refMe's CIFilter's filterWithName:("CIQRCodeGenerator") |
194 | appQRcodeImage's setDefaults() |
195 | #テキスト指定 |
196 | appQRcodeImage's setValue:(ocidTextData) forKey:("inputMessage") |
197 | #読み取り誤差値設定L, M, Q, H |
198 | appQRcodeImage's setValue:("Q") forKey:("inputCorrectionLevel") |
199 | #QRコード本体のイメージ |
200 | set ocidCIImage to appQRcodeImage's outputImage() |
201 | |
202 | ############## |
203 | #色の置き換え色 QRコードの出力色になります |
204 | set ocidQRColor to refMe's CIColor's colorWithRed:(0.0) green:(0.0) blue:(0.0) alpha:(1.0) |
205 | #背景透過 |
206 | set ocidBlackColor to refMe's CIColor's colorWithRed:(1.0) green:(1.0) blue:(1.0) alpha:(0.0) |
207 | #色の置換 RGB |
208 | set appCIFalseColor to refMe's CIFilter's filterWithName:"CIFalseColor" |
209 | appCIFalseColor's setDefaults() |
210 | appCIFalseColor's setValue:(ocidQRColor) forKey:("inputColor0") |
211 | appCIFalseColor's setValue:(ocidBlackColor) forKey:("inputColor1") |
212 | appCIFalseColor's setValue:(ocidCIImage) forKey:(refMe's kCIInputImageKey) |
213 | #色が変わった後のイメージの取り出し |
214 | #set ocidCIImage to ocidFilterColor's valueForKey:(refMe's kCIOutputImageKey) |
215 | set ocidCIImage to appCIFalseColor's outputImage() |
216 | |
217 | ############## |
218 | #拡大 まずは作成したQRのピクセルサイズ取得 |
219 | set ocidQRRect to ocidCIImage's extent() |
220 | #必ず正方形なので幅しか取らない |
221 | set ocidCIImageWidth to refMe's NSRect's NSWidth(ocidQRRect) |
222 | #整数で拡大しないとアレなので↑の値のニアなサイズになります |
223 | #出力サイズ÷出来上がりQRコードサイズ÷1で整数に |
224 | set numScale to ((numScaleMax / ocidCIImageWidth) div 1) as integer |
225 | |
226 | ############## |
227 | #変換スケール作成-->拡大 |
228 | set recordScalse to refMe's CGAffineTransform's CGAffineTransformMakeScale(numScale, numScale) |
229 | #変換スケールを適応(元のサイズに元のサイズのスケール適応しても意味ないけど |
230 | set ocidCIImageScaled to ocidCIImage's imageByApplyingTransform:(recordScalse) |
231 | #QRイメージIMAGEREPに |
232 | set ocidCIImageRep to refMe's NSCIImageRep's imageRepWithCIImage:(ocidCIImageScaled) |
233 | #拡大したQRコードのサイズ |
234 | set ocidCIImageSize to ocidCIImageRep's |size|() |
235 | #出力用のNSIMAGEをQRコードのサイズで作成 |
236 | set ocidNSImageScaled to refMe's NSImage's alloc()'s initWithSize:(ocidCIImageSize) |
237 | #QRコードを挿入 |
238 | ocidNSImageScaled's addRepresentation:(ocidCIImageRep) |
239 | #TIFFRepresentation |
240 | set ocidOsDispatchData to ocidNSImageScaled's TIFFRepresentation() |
241 | #NSBitmapImageRep |
242 | set ocidBitmapImageRep to refMe's NSBitmapImageRep's imageRepWithData:(ocidOsDispatchData) |
243 | set numScaledSize to ocidBitmapImageRep's pixelsWide() |
244 | |
245 | ############## |
246 | #quiet zone用に画像をパディングする |
247 | set numArtBoardPtW to ((numScaledSize) + (numScale * 4)) as integer |
248 | #左右に2セル分づつ=縦横4セル分の余白 quiet zoneを足す |
249 | #まずは元のQRコードのサイズに4セルサイズ分足したサイズの画像=出力サイズイメージ |
250 | #アートボードになる出力サイズイメージ |
251 | #CIFilterのQRコードは1セルクワイエットゾーンがあるので2セル足して3セルにする |
252 | set ocidColorSpaceName to (refMe's NSDeviceWhiteColorSpace) |
253 | set ocidBitmapFormat to (refMe's NSBitmapFormatAlphaFirst) |
254 | set ocidArtBoardRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numArtBoardPtW) pixelsHigh:(numArtBoardPtW) bitsPerSample:8 samplesPerPixel:1 hasAlpha:false isPlanar:false colorSpaceName:(ocidColorSpaceName) bitmapFormat:(ocidBitmapFormat) bytesPerRow:0 bitsPerPixel:0) |
255 | |
256 | ############## |
257 | #ColorSync |
258 | set strICCProfilesFilePath to ("/System/Library/ColorSync/Profiles/Generic Gray Profile.icc") as text |
259 | set ocidICCProfilesFilePathStr to refMe's NSString's stringWithString:(strICCProfilesFilePath) |
260 | set ocidICCProfilesFilePath to ocidICCProfilesFilePathStr's stringByStandardizingPath() |
261 | set ocidICCProfilesFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidICCProfilesFilePath) isDirectory:false) |
262 | #NSData's |
263 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
264 | set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidICCProfilesFilePathURL) options:(ocidOption) |error| :(reference) |
265 | if (item 2 of listResponse) = (missing value) then |
266 | log "正常処理" |
267 | set ocidICCProfilesData to (item 1 of listResponse) |
268 | else if (item 2 of listResponse) ≠ (missing value) then |
269 | set strErrorNO to (item 2 of listDone)'s code() as text |
270 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
271 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
272 | return "エラーしました" & strErrorNO & strErrorMes |
273 | end if |
274 | ##プロファイルをセット |
275 | ocidArtBoardRep's setProperty:(refMe's NSImageColorSyncProfileData) withValue:(ocidICCProfilesData) |
276 | |
277 | ############## |
278 | #まずアートボードになる画像と背景色 |
279 | #QRコードの背景色 透過 |
280 | set ocidArtBoardColor to refMe's NSColor's colorWithWhite:(1.0) alpha:(1.0) |
281 | #初期化 |
282 | set appGraphicsContext to (refMe's NSGraphicsContext) |
283 | #編集開始 |
284 | appGraphicsContext's saveGraphicsState() |
285 | #イメージ読み込み |
286 | set ocidReadImageContext to appGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep) |
287 | #出力サイズイメージ読み込み |
288 | appGraphicsContext's setCurrentContext:(ocidReadImageContext) |
289 | #塗り色を設定値指定して |
290 | ocidArtBoardColor's |set|() |
291 | #画像として色を塗る |
292 | set ocidFillRect to refMe's NSRect's NSMakeRect(0, 0, numArtBoardPtW, numArtBoardPtW) |
293 | refMe's NSRectFill(ocidFillRect) |
294 | #編集終了 |
295 | appGraphicsContext's restoreGraphicsState() |
296 | |
297 | ############## |
298 | set appGraphicsContext to (refMe's NSGraphicsContext) |
299 | #編集開始 |
300 | appGraphicsContext's saveGraphicsState() |
301 | #イメージ読み込み |
302 | set ocidReadImageContext to appGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep) |
303 | #出力サイズイメージ読み込み |
304 | appGraphicsContext's setCurrentContext:(ocidReadImageContext) |
305 | #背景画像の上にQRバーコードの画像をNSCompositeSourceOver |
306 | #位置関係 ペーストする位置 |
307 | set ocidDrawRect to refMe's NSRect's NSMakeRect((numScale * 2), (numScale * 2), numScaledSize, numScaledSize) |
308 | #元画像をコピーしてくる位置 |
309 | set ocidFromRect to refMe's NSRect's NSMakeRect(0, 0, numScaledSize, numScaledSize) |
310 | #NSCompositeSourceOver |
311 | set appOperation to (refMe's NSCompositeSourceOver) |
312 | ocidBitmapImageRep's drawInRect:(ocidDrawRect) fromRect:(ocidFromRect) operation:(appOperation) fraction:1.0 respectFlipped:true hints:(missing value) |
313 | #画像作成終了 |
314 | appGraphicsContext's restoreGraphicsState() |
315 | |
316 | ############## |
317 | #解像度変更 |
318 | #出力画像サイズ |
319 | set ocidQRsize to ocidArtBoardRep's |size|() |
320 | #幅Pt=Pxだけ取得して |
321 | set numPxW to (ocidQRsize's width()) as number |
322 | #設定解像度÷72÷出来上がりサイズで |
323 | # 2なら144ppi 3なら216ppi 4なら288ppi 5なら360ppi |
324 | set numSetSize to (numPxW / (numResolution / 72)) as number |
325 | #サイズを生成して |
326 | set ocidOutPutSize to refMe's NSSize's NSMakeSize(numSetSize, numSetSize) |
327 | #サイズを設定(ここで設定するのはPTサイズ) |
328 | ocidArtBoardRep's setSize:(ocidOutPutSize) |
329 | |
330 | ############## |
331 | #TIFF保存のプロパティ |
332 | set ocidPropertiesDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
333 | ocidPropertiesDict's setValue:(0.0) forKey:(refMe's NSImageCompressionFactor) |
334 | ocidPropertiesDict's setValue:(missing value) forKey:(refMe's NSImageCompressionMethod) |
335 | ocidPropertiesDict's setValue:(missing value) forKey:(refMe's NSImageIPTCData) |
336 | ocidPropertiesDict's setValue:(missing value) forKey:(refMe's NSImageEXIFData) |
337 | ocidPropertiesDict's setValue:(ocidICCProfilesData) forKey:(refMe's NSImageColorSyncProfileData) |
338 | |
339 | #出力イメージへ変換 |
340 | set ocidType to (refMe's NSBitmapImageFileTypeTIFF) |
341 | set ocidSaveImageData to ocidArtBoardRep's representationUsingType:(ocidType) |properties|:(ocidPropertiesDict) |
342 | |
343 | ############## |
344 | #保存 |
345 | set ocidOption to (refMe's NSDataWritingAtomic) |
346 | set listDone to ocidSaveImageData's writeToURL:(argSaveFilePathURL) options:(ocidOption) |error| :(reference) |
347 | if (item 1 of listDone) is true then |
348 | return true |
349 | else if (item 2 of listDone) ≠ (missing value) then |
350 | set strErrorNO to (item 2 of listDone)'s code() as text |
351 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
352 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
353 | log "エラーしました" & strErrorNO & strErrorMes |
354 | return false |
355 | end if |
356 | return true |
357 | end doMakeQR |
358 | |
359 | ################################ |
360 | #日付生成(今回は使わない) |
361 | set strDateText to doGetDateNo() |
362 | |
363 | ################################ |
364 | # 日付 doGetDateNo() |
365 | ################################ |
366 | to doGetDateNo() |
367 | #戻す日付テキスト |
368 | set ocidRetuenString to refMe's NSMutableString's alloc()'s initWithCapacity:(0) |
369 | ###今日 |
370 | set ocidDate to (current application's NSDate's |date|()) |
371 | ###フォーマット初期化 |
372 | set ocidFormatterJP to current application's NSDateFormatter's alloc()'s init() |
373 | ###日本のカレンダー |
374 | set ocidCalendarID to (current application's NSCalendarIdentifierJapanese) |
375 | set ocidCalendarJP to current application's NSCalendar's alloc()'s initWithCalendarIdentifier:(ocidCalendarID) |
376 | ###東京タイムゾーン |
377 | set ocidTimezoneJP to current application's NSTimeZone's alloc()'s initWithName:("Asia/Tokyo") |
378 | ###日本語 |
379 | set ocidLocaleJP to current application's NSLocale's alloc()'s initWithLocaleIdentifier:("ja_JP_POSIX") |
380 | ###フォーマットをセット(年号) |
381 | ocidFormatterJP's setTimeZone:(ocidTimezoneJP) |
382 | ocidFormatterJP's setLocale:(ocidLocaleJP) |
383 | ocidFormatterJP's setCalendar:(ocidCalendarJP) |
384 | ocidFormatterJP's setDateStyle:(current application's NSDateFormatterFullStyle) |
385 | set strDateFormat to (" ㋿y年") |
386 | ocidFormatterJP's setDateFormat:(strDateFormat) |
387 | set ocidDateStringEra to ocidFormatterJP's stringFromDate:(ocidDate) |
388 | ocidRetuenString's appendString:(ocidDateStringEra) |
389 | ###フォマットから月合字を取得(使わない) |
390 | set recordMonth to {|01月|:"㋀", |02月|:"㋁", |03月|:"㋂", |04月|:"㋃", |05月|:"㋄", |06月|:"㋅", |07月|:"㋆", |08月|:"㋇", |09月|:"㋈", |10月|:"㋉", |11月|:"㋊", |12月|:"㋋"} as record |
391 | set ocidMonthDict to refMe's NSMutableDictionary's alloc()'s initWithDictionary:(recordMonth) |
392 | set strDateFormat to ("MM月") |
393 | ocidFormatterJP's setDateFormat:(strDateFormat) |
394 | set ocidDateStringMonth to ocidFormatterJP's stringFromDate:(ocidDate) |
395 | #set ocidMonthValue to ocidMonthDict's valueForKey:(ocidDateStringMonth) |
396 | ocidRetuenString's appendString:(ocidDateStringMonth) |
397 | ##フォーマットから日付を取得(使わない) |
398 | set recordDate to {|01|:"㏠", |02|:"㏡", |03|:"㏢", |04|:"㏣", |05|:"㏤", |06|:"㏥", |07|:"㏦", |08|:"㏧", |09|:"㏨", |10|:"㏩", |11|:"㏪", |12|:"㏫", |13|:"㏬", |14|:"㏭", |15|:"㏮", |16|:"㏯", |17|:"㏰", |18|:"㏱", |19|:"㏲", |20|:"㏳", |21|:"㏴", |22|:"㏵", |23|:"㏶", |24|:"㏷", |25|:"㏸", |26|:"㏹", |27|:"㏺", |28|:"㏻", |29|:"㏼", |30|:"㏽", |31|:"㏾"} as record |
399 | set ocidDateDict to refMe's NSMutableDictionary's alloc()'s initWithDictionary:(recordDate) |
400 | set strDateFormat to ("dd日 ") |
401 | ocidFormatterJP's setDateFormat:(strDateFormat) |
402 | set ocidDateStringDate to ocidFormatterJP's stringFromDate:(ocidDate) |
403 | # set ocidDateValue to ocidDateDict's valueForKey:(ocidDateStringDate) |
404 | ocidRetuenString's appendString:(ocidDateStringDate) |
405 | |
406 | set strDateAndTime to ((ocidRetuenString as text)) as text |
407 | ###テキストで戻す |
408 | return strDateAndTime |
409 | end doGetDateNo |
410 | |
411 | |
412 | |
413 | |
414 | ############################ |
415 | # mmをPTに換算 |
416 | ############################ |
417 | to doConversionMM2PT(argNumMM) |
418 | set strArgNumMM to argNumMM as text |
419 | #指数 |
420 | set ocidCm2InchNo to refMe's NSDecimalNumber's alloc()'s initWithString:("25.4") |
421 | set ocidDivPt to refMe's NSDecimalNumber's alloc()'s initWithString:("72") |
422 | set ocidDivNo to (ocidCm2InchNo's decimalNumberByDividingBy:(ocidDivPt)) |
423 | #計算 |
424 | set ocidDecNo to refMe's NSDecimalNumber's alloc()'s initWithString:(strArgNumMM) |
425 | set ocidRawNo to (ocidDecNo's decimalNumberByDividingBy:(ocidDivNo)) |
426 | #小数点以下切り上げ |
427 | set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init() |
428 | ocidFormatter's setRoundingMode:(refMe's NSNumberFormatterRoundCeiling) |
429 | ocidFormatter's setNumberStyle:(refMe's NSNumberFormatterDecimalStyle) |
430 | ocidFormatter's setMaximumFractionDigits:(0) |
431 | set ocidRoundCeiling to ocidFormatter's stringFromNumber:(ocidRawNo) |
432 | #ポイントサイズ |
433 | set numPtSize to ocidRoundCeiling as integer |
434 | return numPtSize |
435 | |
436 | end doConversionMM2PT |
437 | |
438 | |
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 "CoreImage" |
010 | use scripting additions |
011 | |
012 | property refMe : a reference to current application |
013 | |
014 | ################ |
015 | #設定項目 |
016 | #出力サイズmm指定 |
017 | #(四捨五入するので近似値になる) |
018 | property strMMsize : ("30") as text |
019 | #出力解像度PPI |
020 | property numResolution : 288 as integer |
021 | |
022 | set boolDone to doMakeQR(strEmail, ocidSaveQRFilePathURL) |
023 | |
024 | |
025 | ################################ |
026 | #QR |
027 | ################################ |
028 | to doMakeQR(argText, argSaveFilePathURL) |
029 | ############## |
030 | #受け取りテキスト |
031 | set strText to argText as text |
032 | #NSString |
033 | set ocidText to refMe's NSString's alloc()'s initWithString:(strText) |
034 | #NSDATA |
035 | set ocidTextData to ocidText's dataUsingEncoding:(refMe's NSUTF8StringEncoding) |
036 | ############## |
037 | #mmをPTに換算 |
038 | set numPtSize to doConversionMM2PT(strMMsize) |
039 | #最終的に出力したいQRコードのサイズ |
040 | set numScaleMax to (numPtSize * (numResolution / 72)) as integer |
041 | |
042 | ############## |
043 | #CIFilter QRコード生成 |
044 | set appQRcodeImage to refMe's CIFilter's filterWithName:("CIQRCodeGenerator") |
045 | appQRcodeImage's setDefaults() |
046 | #テキスト指定 |
047 | appQRcodeImage's setValue:(ocidTextData) forKey:("inputMessage") |
048 | #読み取り誤差値設定L, M, Q, H |
049 | appQRcodeImage's setValue:("Q") forKey:("inputCorrectionLevel") |
050 | #QRコード本体のイメージ |
051 | set ocidCIImage to appQRcodeImage's outputImage() |
052 | |
053 | ############## |
054 | #色の置き換え色 QRコードの出力色になります |
055 | set ocidQRColor to refMe's CIColor's colorWithRed:(0.0) green:(0.0) blue:(0.0) alpha:(1.0) |
056 | #背景透過 |
057 | set ocidBlackColor to refMe's CIColor's colorWithRed:(1.0) green:(1.0) blue:(1.0) alpha:(0.0) |
058 | #QRコードの背景色 透過 |
059 | set ocidArtBoardColor to refMe's NSColor's colorWithRed:(1.0) green:(1.0) blue:(1.0) alpha:(0.0) |
060 | |
061 | #色の置換 |
062 | set appCIFalseColor to refMe's CIFilter's filterWithName:"CIFalseColor" |
063 | appCIFalseColor's setDefaults() |
064 | appCIFalseColor's setValue:(ocidQRColor) forKey:("inputColor0") |
065 | appCIFalseColor's setValue:(ocidBlackColor) forKey:("inputColor1") |
066 | appCIFalseColor's setValue:(ocidCIImage) forKey:("inputImage") |
067 | #色が変わった後のイメージの取り出し |
068 | #set ocidCIImage to ocidFilterColor's valueForKey:"outputImage" |
069 | set ocidCIImage to appCIFalseColor's outputImage() |
070 | |
071 | ############## |
072 | #拡大 まずは作成したQRのピクセルサイズ取得 |
073 | set ocidQRRect to ocidCIImage's extent() |
074 | #必ず正方形なので幅しか取らない |
075 | set ocidCIImageWidth to refMe's NSRect's NSWidth(ocidQRRect) |
076 | #整数で拡大しないとアレなので↑の値のニアなサイズになります |
077 | #出力サイズ÷出来上がりQRコードサイズ÷1で整数に |
078 | set numScale to ((numScaleMax / ocidCIImageWidth) div 1) as integer |
079 | |
080 | ############## |
081 | #変換スケール作成-->拡大 |
082 | set recordScalse to refMe's CGAffineTransform's CGAffineTransformMakeScale(numScale, numScale) |
083 | #変換スケールを適応(元のサイズに元のサイズのスケール適応しても意味ないけど |
084 | set ocidCIImageScaled to ocidCIImage's imageByApplyingTransform:(recordScalse) |
085 | #QRイメージIMAGEREPに |
086 | set ocidCIImageRep to refMe's NSCIImageRep's imageRepWithCIImage:(ocidCIImageScaled) |
087 | #拡大したQRコードのサイズ |
088 | set ocidCIImageSize to ocidCIImageRep's |size|() |
089 | #出力用のNSIMAGEをQRコードのサイズで作成 |
090 | set ocidNSImageScaled to refMe's NSImage's alloc()'s initWithSize:(ocidCIImageSize) |
091 | #QRコードを挿入 |
092 | ocidNSImageScaled's addRepresentation:(ocidCIImageRep) |
093 | #TIFFRepresentation |
094 | set ocidOsDispatchData to ocidNSImageScaled's TIFFRepresentation() |
095 | #NSBitmapImageRep |
096 | set ocidBitmapImageRep to refMe's NSBitmapImageRep's imageRepWithData:(ocidOsDispatchData) |
097 | set numScaledSize to ocidBitmapImageRep's pixelsWide() |
098 | |
099 | ############## |
100 | #QRイメージIMAGEREPに |
101 | set ocidCIImageRep to refMe's NSCIImageRep's imageRepWithCIImage:(ocidCIImageScaled) |
102 | #出力用のNSIMAGEをQRコードのサイズで作成 |
103 | set ocidNSImageScaled to refMe's NSImage's alloc()'s initWithSize:(ocidCIImageRep's |size|()) |
104 | #QRコードを挿入 |
105 | ocidNSImageScaled's addRepresentation:(ocidCIImageRep) |
106 | #TIFFRepresentation |
107 | set ocidOsDispatchData to ocidNSImageScaled's TIFFRepresentation() |
108 | #NSBitmapImageRep |
109 | set ocidBitmapImageRep to refMe's NSBitmapImageRep's imageRepWithData:(ocidOsDispatchData) |
110 | |
111 | ############## |
112 | #quiet zone用に画像をパディングする |
113 | set numArtBoardPtW to ((numScaledSize) + (numScale * 4)) as integer |
114 | #左右に2セル分づつ=縦横4セル分の余白 quiet zoneを足す |
115 | #まずは元のQRコードのサイズに4セルサイズ分足したサイズの画像=出力サイズイメージ |
116 | #アートボードになる出力サイズイメージ |
117 | #CIFilterのQRコードは1セルクワイエットゾーンがあるので2セル足して3セルにする |
118 | set ocidArtBoardRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numArtBoardPtW) pixelsHigh:(numArtBoardPtW) bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:false colorSpaceName:(refMe's NSCalibratedRGBColorSpace) bitmapFormat:(refMe's NSAlphaFirstBitmapFormat) bytesPerRow:0 bitsPerPixel:32) |
119 | |
120 | ############## |
121 | #まずアートボードになる画像と背景色 |
122 | #初期化 |
123 | set appGraphicsContext to (refMe's NSGraphicsContext) |
124 | #編集開始 |
125 | appGraphicsContext's saveGraphicsState() |
126 | #イメージ読み込み |
127 | set ocidReadImageContext to appGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep) |
128 | #出力サイズイメージ読み込み |
129 | appGraphicsContext's setCurrentContext:(ocidReadImageContext) |
130 | #塗り色を『白』に指定して |
131 | ocidArtBoardColor's |set|() |
132 | #画像として色を塗る |
133 | set ocidFillRect to refMe's NSRect's NSMakeRect(0, 0, numArtBoardPtW, numArtBoardPtW) |
134 | refMe's NSRectFill(ocidFillRect) |
135 | #編集終了 |
136 | appGraphicsContext's restoreGraphicsState() |
137 | |
138 | ############## |
139 | set appGraphicsContext to (refMe's NSGraphicsContext) |
140 | #編集開始 |
141 | appGraphicsContext's saveGraphicsState() |
142 | #イメージ読み込み |
143 | set ocidReadImageContext to appGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep) |
144 | #出力サイズイメージ読み込み |
145 | appGraphicsContext's setCurrentContext:(ocidReadImageContext) |
146 | #背景画像の上にQRバーコードの画像をNSCompositeSourceOver |
147 | #位置関係 ペーストする位置 |
148 | set ocidDrawRect to refMe's NSRect's NSMakeRect((numScale * 2), (numScale * 2), numScaledSize, numScaledSize) |
149 | #元画像をコピーしてくる位置 |
150 | set ocidFromRect to refMe's NSRect's NSMakeRect(0, 0, numScaledSize, numScaledSize) |
151 | #NSCompositeSourceOver |
152 | set appOperation to (refMe's NSCompositeSourceOver) |
153 | ocidBitmapImageRep's drawInRect:(ocidDrawRect) fromRect:(ocidFromRect) operation:(appOperation) fraction:1.0 respectFlipped:true hints:(missing value) |
154 | #画像作成終了 |
155 | appGraphicsContext's restoreGraphicsState() |
156 | |
157 | ############## |
158 | #解像度変更 |
159 | #出力画像サイズ |
160 | set ocidQRsize to ocidArtBoardRep's |size|() |
161 | #幅Pt=Pxだけ取得して |
162 | set numPxW to (ocidQRsize's width()) as number |
163 | #設定解像度÷72÷出来上がりサイズで |
164 | # 2なら144ppi 3なら216ppi 4なら288ppi 5なら360ppi |
165 | set numSetSize to (numPxW / (numResolution / 72)) as number |
166 | #サイズを生成して |
167 | set ocidOutPutSize to refMe's NSSize's NSMakeSize(numSetSize, numSetSize) |
168 | #サイズを設定(ここで設定するのはPTサイズ) |
169 | ocidArtBoardRep's setSize:(ocidOutPutSize) |
170 | |
171 | ############## |
172 | #PNG保存のプロパティ |
173 | set ocidPropertiesDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
174 | ocidPropertiesDict's setValue:(false) forKey:(refMe's NSImageInterlaced) |
175 | ocidPropertiesDict's setValue:(1.0 / 2.2) forKey:(refMe's NSImageGamma) |
176 | ocidPropertiesDict's setValue:(missing value) forKey:(refMe's NSImageIPTCData) |
177 | ocidPropertiesDict's setValue:(missing value) forKey:(refMe's NSImageEXIFData) |
178 | #出力イメージへ変換 |
179 | set ocidType to (refMe's NSBitmapImageFileTypePNG) |
180 | set ocidSaveImageData to ocidArtBoardRep's representationUsingType:(ocidType) |properties|:(ocidPropertiesDict) |
181 | |
182 | ############## |
183 | #保存 |
184 | set ocidOption to (refMe's NSDataWritingAtomic) |
185 | set listDone to ocidSaveImageData's writeToURL:(argSaveFilePathURL) options:(ocidOption) |error| :(reference) |
186 | if (item 1 of listDone) is true then |
187 | return true |
188 | else if (item 2 of listDone) ≠ (missing value) then |
189 | set strErrorNO to (item 2 of listDone)'s code() as text |
190 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
191 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
192 | log "エラーしました" & strErrorNO & strErrorMes |
193 | return false |
194 | end if |
195 | return true |
196 | end doMakeQR |
AppleScriptで生成しました |
| 固定リンク
行番号 | ソース |
---|---|
001 | #! /usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | # |
005 | # |
006 | # com.cocolog-nifty.quicktimer.icefloe |
007 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
008 | use AppleScript version "2.8" |
009 | use framework "Foundation" |
010 | use framework "AppKit" |
011 | use framework "PDFKit" |
012 | use framework "CoreImage" |
013 | use scripting additions |
014 | |
015 | property refMe : a reference to current application |
016 | |
017 | ################ |
018 | #設定項目 |
019 | #出力サイズmm指定 |
020 | #(四捨五入するので近似値になる) |
021 | set strMMsize to ("30") as text |
022 | |
023 | #出力解像度PPI |
024 | set numResolution to 288 as integer |
025 | |
026 | #バーコードの中身 |
027 | set strText to ("http://news.yahoo.co.jp/") as text |
028 | |
029 | ################ |
030 | #出力先パス |
031 | set strFilePath to "~/Desktop/test.png" as text |
032 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
033 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
034 | set argSaveFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false) |
035 | |
036 | ############## |
037 | #テキスト |
038 | set ocidText to |