画像のCROP w指定 (少し修正)
あくまでも参考にしてください
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | #com.cocolog-nifty.quicktimer.icefloe |
004 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
005 | use AppleScript version "2.8" |
006 | use framework "Foundation" |
007 | use framework "AppKit" |
008 | use scripting additions |
009 | |
010 | property refMe : a reference to current application |
011 | |
012 | ############################ |
013 | ### 入力ダイアログ |
014 | ############################ |
015 | ## クリップボードの中身取り出し |
016 | ###初期化 |
017 | set appPasteboard to refMe's NSPasteboard's generalPasteboard() |
018 | set ocidPastBoardTypeArray to appPasteboard's types |
019 | ###テキストがあれば |
020 | set boolContain to ocidPastBoardTypeArray's containsObject:"public.utf8-plain-text" |
021 | if boolContain = true then |
022 | ###値を格納する |
023 | tell application "Finder" |
024 | set strReadString to (the clipboard as text) as text |
025 | end tell |
026 | ###Finderでエラーしたら |
027 | else |
028 | set boolContain to ocidPastBoardTypeArray's containsObject:"NSStringPboardType" |
029 | if boolContain = true then |
030 | set ocidReadString to ocidPasteboard's readObjectsForClasses:({refMe's NSString}) options:(missing value) |
031 | set strReadString to ocidReadString as text |
032 | else |
033 | log "テキストなし" |
034 | set strReadString to ("横方向左右CROP『px』数値入力") as text |
035 | end if |
036 | end if |
037 | |
038 | ###ダイアログを全面に |
039 | set strName to (name of current application) as text |
040 | if strName is "osascript" then |
041 | tell application "Finder" to activate |
042 | else |
043 | tell current application to activate |
044 | end if |
045 | set aliasIconPath to (POSIX file "/System/Applications/Calculator.app/Contents/Resources/AppIcon.icns") as alias |
046 | set strTitle to ("入力してください") as text |
047 | set strMes to ("横方向左右CROP『px』数値入力\n例:100だと\n左100px右100pxCropします") as text |
048 | set recordResult to (display dialog strMes with title strTitle default answer strReadString buttons {"キャンセル", "OK"} default button "OK" cancel button "キャンセル" giving up after 30 with icon aliasIconPath without hidden answer) |
049 | |
050 | if (gave up of recordResult) is true then |
051 | return "時間切れです" |
052 | else if (button returned of recordResult) is "キャンセル" then |
053 | return "キャンセルです" |
054 | else |
055 | set strReturnedText to (text returned of recordResult) as text |
056 | end if |
057 | #戻り値の整形 |
058 | set ocidResponseText to (refMe's NSString's stringWithString:(strReturnedText)) |
059 | ###タブと改行を除去しておく |
060 | set ocidTextM to refMe's NSMutableString's alloc()'s initWithCapacity:(0) |
061 | ocidTextM's appendString:(ocidResponseText) |
062 | ##改行除去 |
063 | set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\n") withString:("") |
064 | set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\r") withString:("") |
065 | ##タブ除去 |
066 | set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\t") withString:("") |
067 | ####戻り値を半角にする |
068 | set ocidNSStringTransform to (refMe's NSStringTransformFullwidthToHalfwidth) |
069 | set ocidTextM to (ocidTextM's stringByApplyingTransform:ocidNSStringTransform |reverse|:false) |
070 | ##カンマ置換 |
071 | set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:(",") withString:(".") |
072 | ###数字以外の値を取る |
073 | set ocidDecSet to refMe's NSCharacterSet's decimalDigitCharacterSet |
074 | set ocidCharSet to ocidDecSet's invertedSet() |
075 | set ocidCharArray to ocidTextM's componentsSeparatedByCharactersInSet:ocidCharSet |
076 | set ocidInteger to ocidCharArray's componentsJoinedByString:"" |
077 | # |
078 | set numCropW to ocidInteger as integer |
079 | |
080 | ############################ |
081 | ### ダイアログ |
082 | ############################ |
083 | set appFileManager to refMe's NSFileManager's defaultManager() |
084 | ###デフォルトロケーション |
085 | set ocidUserDocumentPathArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
086 | set ocidDesktopPathURL to ocidUserDocumentPathArray's firstObject() |
087 | set aliasDefaultLocation to (ocidDesktopPathURL's absoluteURL()) as alias |
088 | #####ダイアログを前面に |
089 | tell current application |
090 | set strName to name as text |
091 | end tell |
092 | ####スクリプトメニューから実行したら |
093 | if strName is "osascript" then |
094 | tell application "Finder" to activate |
095 | else |
096 | tell current application to activate |
097 | end if |
098 | ####ダイアログを出す |
099 | set listUTI to {"public.image"} as list |
100 | set listAliasFilePath to (choose file with prompt "ファイルを選んでください" default location (aliasDefaultLocation) of type listUTI with invisibles, showing package contents and multiple selections allowed) as list |
101 | ############ |
102 | #URLのARRAYに |
103 | set ocidFilePathURLArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
104 | repeat with itemAliasFilePath in listAliasFilePath |
105 | set strFilePath to (POSIX path of itemAliasFilePath) as text |
106 | set ocidFilePath to (refMe's NSString's stringWithString:(strFilePath)) |
107 | set ocidFilePathURL to (refMe's NSURL's fileURLWithPath:(ocidFilePath)) |
108 | (ocidFilePathURLArray's addObject:(ocidFilePathURL)) |
109 | end repeat |
110 | set numCntURL to (count of ocidFilePathURLArray) as integer |
111 | ############ |
112 | #並び替え |
113 | set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("absoluteString") ascending:(yes) selector:("localizedStandardCompare:") |
114 | set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor) |
115 | set ocidSortedArray to ocidFilePathURLArray's sortedArrayUsingDescriptors:(ocidDescriptorArray) |
116 | #ダイアログ用のURL |
117 | set ocidFirstObjectURL to ocidSortedArray's firstObject() |
118 | set ocidContainerDirPathURL to ocidFirstObjectURL's URLByDeletingLastPathComponent() |
119 | set aliasSaveDirPath to (ocidContainerDirPathURL's absoluteURL()) as alias |
120 | ############ |
121 | #保存先 |
122 | set strMes to "保存先フォルダを選んでください" as text |
123 | set strPrompt to "保存先フォルダを選択してください" as text |
124 | try |
125 | set aliasSaveDirPath to (choose folder strMes with prompt strPrompt default location aliasSaveDirPath with invisibles and showing package contents without multiple selections allowed) as alias |
126 | on error |
127 | log "エラーしました" |
128 | return "エラーしました" |
129 | end try |
130 | set strSaveDirPath to (POSIX path of aliasSaveDirPath) as text |
131 | set ocidSaveDirPath to refMe's NSString's stringWithString:(strSaveDirPath) |
132 | set ocidSaveDirPathURL to refMe's NSURL's fileURLWithPath:(ocidSaveDirPath) |
133 | |
134 | |
135 | |
136 | ############################ |
137 | ####選んだファイルパスの数だけ繰り返し |
138 | ############################ |
139 | repeat with itemAliasFilePath in listAliasFilePath |
140 | ##入力パス |
141 | set strFilePath to (POSIX path of itemAliasFilePath) as text |
142 | set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath)) |
143 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
144 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false) |
145 | set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent() |
146 | set ocidFileName to ocidFilePathURL's lastPathComponent() |
147 | #保存先パス |
148 | set ocidSaveFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(ocidFileName)) |
149 | ########################### |
150 | ###【1】元イメージ |
151 | ##NSIMAGEに読み込む |
152 | set ocidReadImage to (refMe's NSImage's alloc()'s initWithContentsOfURL:(ocidFilePathURL)) |
153 | set ocidNsImageSize to ocidReadImage's |size|() |
154 | set numPtWidth to ocidNsImageSize's width |
155 | set numPtHeigh to ocidNsImageSize's height |
156 | ##BitMapRepに変換 |
157 | set ocidReadImageRepArray to ocidReadImage's representations() |
158 | set ocidReadImageRep to (ocidReadImageRepArray's objectAtIndex:0) |
159 | ##ピクセルサイズ取得 |
160 | set numPixelsWidth to ocidReadImageRep's pixelsWide() |
161 | set numPixelsHeight to ocidReadImageRep's pixelsHigh() |
162 | ##まずは72ppiにする |
163 | set ocidPixelsSize to refMe's NSMakeSize(numPixelsWidth, numPixelsHeight) |
164 | (ocidReadImageRep's setSize:(ocidPixelsSize)) |
165 | log numPtWidth as integer |
166 | log numPixelsHeight as integer |
167 | |
168 | ########################### |
169 | ###【2】出力イメージ numCropW |
170 | ###サイズ計算 |
171 | # |
172 | set numSetPxWidth to (numPixelsWidth - (numCropW * 2)) as integer |
173 | # |
174 | set numSetPxHeight to numPixelsHeight as integer |
175 | #出力用の画像ピクセルサイズ |
176 | set ocidNewImagePixelsSize to refMe's NSMakeSize(numSetPxWidth, numSetPxHeight) |
177 | ###出力用イメージ NSBitmapImageRep |
178 | # samplesPerPixel |
179 | set intSPP to 4 as integer |
180 | # bitsPerSample |
181 | set intBPS to 8 as integer |
182 | # bytesPerRow |
183 | set intBPR to 0 as integer |
184 | # bitsPerPixel |
185 | set intBPP to 32 as integer |
186 | # RGB系のカラースペース |
187 | set ocidColorSpaceName to refMe's NSCalibratedRGBColorSpace |
188 | # アルファあり |
189 | set ocidBitmapFormat to refMe's NSAlphaFirstBitmapFormat |
190 | ##出力ピクセルサイズのブランクイメージ |
191 | set ocidArtBoardRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numSetPxWidth) pixelsHigh:(numSetPxHeight) bitsPerSample:(intBPS) samplesPerPixel:(intSPP) hasAlpha:(true) isPlanar:(false) colorSpaceName:(ocidColorSpaceName) bitmapFormat:(ocidBitmapFormat) bytesPerRow:(intBPR) bitsPerPixel:(intBPP)) |
192 | |
193 | ########################### |
194 | ###【3】ArtBord |
195 | ### 初期化 CodeBase |
196 | refMe's NSGraphicsContext's saveGraphicsState() |
197 | ###Context |
198 | set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep)) |
199 | ###生成された画像でNSGraphicsContext初期化 |
200 | (refMe's NSGraphicsContext's setCurrentContext:(ocidContext)) |
201 | ##色を個別に指定する場合 値は0が暗 1が明 |
202 | set ocidSetColor to (refMe's NSColor's colorWithSRGBRed:(1.0) green:(1.0) blue:(1.0) alpha:(0.0)) |
203 | ocidSetColor's |set|() |
204 | ###画像生成 |
205 | set ocidSaveImageRect to refMe's NSMakeRect(0, 0, numSetPxWidth, (numSetPxHeight)) |
206 | #refMe's NSRectFill({origin:{x:0, y:0}, |size|:{width:(numSetPxWidth), height:(numSetPxHeight)}}) |
207 | refMe's NSRectFill(ocidSaveImageRect) |
208 | ####画像作成終了 |
209 | refMe's NSGraphicsContext's restoreGraphicsState() |
210 | |
211 | ########################### |
212 | ###【3】合成 リザイスでNSCompositeSourceOver |
213 | ### 初期化 |
214 | refMe's NSGraphicsContext's saveGraphicsState() |
215 | ###Context |
216 | set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep)) |
217 | ###NSGraphicsContext初期化 |
218 | (refMe's NSGraphicsContext's setCurrentContext:(ocidContext)) |
219 | ####NSCompositeSourceOver |
220 | set ocidFromRect to refMe's NSMakeRect((numCropW), 0, numSetPxWidth, numSetPxHeight) |
221 | # {origin:{x:(numCropW / 2), y:(0)}, |size|:{width:(numSetPxWidth), Hight:(numSetPxHeight)}} |
222 | |
223 | # set ocidOption to (refMe's NSCompositeSourceOver) |
224 | set ocidOption to (refMe's NSCompositingOperationSourceOver) |
225 | |
226 | ## set ocidOption to (refMe's NSCompositeCopy) |
227 | (ocidReadImageRep's drawInRect:(ocidSaveImageRect) fromRect:(ocidFromRect) operation:(ocidOption) fraction:1.0 respectFlipped:true hints:(missing value)) |
228 | ####画像作成終了 |
229 | refMe's NSGraphicsContext's restoreGraphicsState() |
230 | |
231 | ########################### |
232 | ###【5】保存 |
233 | ####保存オプション |
234 | set ocidProperty to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0) |
235 | (ocidProperty's setObject:(refMe's NSNumber's numberWithBool:false) forKey:(refMe's NSImageInterlaced)) |
236 | (ocidProperty's setObject:(refMe's NSNumber's numberWithDouble:(1 / 2.2)) forKey:(refMe's NSImageGamma)) |
237 | ##保存 |
238 | set ocidNSInlineData to (ocidArtBoardRep's representationUsingType:(refMe's NSBitmapImageFileTypePNG) |properties|:(ocidProperty)) |
239 | set boolDone to (ocidNSInlineData's writeToURL:(ocidSaveFilePathURL) atomically:true) |
240 | |
241 | set ocidReadImageRep to "" |
242 | set ocidArtBoardRep to "" |
243 | end repeat |
AppleScriptで生成しました |
| 固定リンク