« PDFのしおり(BookMarkまたはoutline)の一覧取得 | トップページ | Spotlight 自分メモ 少し更新 »

画像の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
005use AppleScript version "2.8"
006use framework "Foundation"
007use framework "AppKit"
008use scripting additions
009
010property refMe : a reference to current application
011
012############################
013### 入力ダイアログ
014############################
015## クリップボードの中身取り出し
016###初期化
017set appPasteboard to refMe's NSPasteboard's generalPasteboard()
018set ocidPastBoardTypeArray to appPasteboard's types
019###テキストがあれば
020set boolContain to ocidPastBoardTypeArray's containsObject:"public.utf8-plain-text"
021if boolContain = true then
022  ###値を格納する
023  tell application "Finder"
024    set strReadString to (the clipboard as text) as text
025  end tell
026  ###Finderでエラーしたら
027else
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
036end if
037
038###ダイアログを全面に
039set strName to (name of current application) as text
040if strName is "osascript" then
041  tell application "Finder" to activate
042else
043  tell current application to activate
044end if
045set aliasIconPath to (POSIX file "/System/Applications/Calculator.app/Contents/Resources/AppIcon.icns") as alias
046set strTitle to ("入力してください") as text
047set strMes to ("横方向左右CROP『px』数値入力\n例:100だと\n左100px右100pxCropします") as text
048set 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
050if (gave up of recordResult) is true then
051  return "時間切れです"
052else if (button returned of recordResult) is "キャンセル" then
053  return "キャンセルです"
054else
055  set strReturnedText to (text returned of recordResult) as text
056end if
057#戻り値の整形
058set ocidResponseText to (refMe's NSString's stringWithString:(strReturnedText))
059###タブと改行を除去しておく
060set ocidTextM to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
061ocidTextM's appendString:(ocidResponseText)
062##改行除去
063set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\n") withString:("")
064set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\r") withString:("")
065##タブ除去
066set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\t") withString:("")
067####戻り値を半角にする
068set ocidNSStringTransform to (refMe's NSStringTransformFullwidthToHalfwidth)
069set ocidTextM to (ocidTextM's stringByApplyingTransform:ocidNSStringTransform |reverse|:false)
070##カンマ置換
071set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:(",") withString:(".")
072###数字以外の値を取る
073set ocidDecSet to refMe's NSCharacterSet's decimalDigitCharacterSet
074set ocidCharSet to ocidDecSet's invertedSet()
075set ocidCharArray to ocidTextM's componentsSeparatedByCharactersInSet:ocidCharSet
076set ocidInteger to ocidCharArray's componentsJoinedByString:""
077#
078set numCropW to ocidInteger as integer
079
080############################
081### ダイアログ
082############################
083set appFileManager to refMe's NSFileManager's defaultManager()
084###デフォルトロケーション
085set ocidUserDocumentPathArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
086set ocidDesktopPathURL to ocidUserDocumentPathArray's firstObject()
087set aliasDefaultLocation to (ocidDesktopPathURL's absoluteURL()) as alias
088#####ダイアログを前面に
089tell current application
090  set strName to name as text
091end tell
092####スクリプトメニューから実行したら
093if strName is "osascript" then
094  tell application "Finder" to activate
095else
096  tell current application to activate
097end if
098####ダイアログを出す
099set listUTI to {"public.image"} as list
100set 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に
103set ocidFilePathURLArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
104repeat 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))
109end repeat
110set numCntURL to (count of ocidFilePathURLArray) as integer
111############
112#並び替え
113set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("absoluteString") ascending:(yes) selector:("localizedStandardCompare:")
114set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
115set ocidSortedArray to ocidFilePathURLArray's sortedArrayUsingDescriptors:(ocidDescriptorArray)
116#ダイアログ用のURL
117set ocidFirstObjectURL to ocidSortedArray's firstObject()
118set ocidContainerDirPathURL to ocidFirstObjectURL's URLByDeletingLastPathComponent()
119set aliasSaveDirPath to (ocidContainerDirPathURL's absoluteURL()) as alias
120############
121#保存先
122set strMes to "保存先フォルダを選んでください" as text
123set strPrompt to "保存先フォルダを選択してください" as text
124try
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
126on error
127  log "エラーしました"
128  return "エラーしました"
129end try
130set strSaveDirPath to (POSIX path of aliasSaveDirPath) as text
131set ocidSaveDirPath to refMe's NSString's stringWithString:(strSaveDirPath)
132set ocidSaveDirPathURL to refMe's NSURL's fileURLWithPath:(ocidSaveDirPath)
133
134
135
136############################
137####選んだファイルパスの数だけ繰り返し
138############################
139repeat 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 ""
243end repeat
AppleScriptで生成しました

|

« PDFのしおり(BookMarkまたはoutline)の一覧取得 | トップページ | Spotlight 自分メモ 少し更新 »

AppleScript NSBitmapImageRep」カテゴリの記事