20260527

[AppleScript]見開き画像を左右に分割する RTL右から左順 左右CROP指定可

[AppleScript]見開き画像を左右に分割する RTL右から左順 左右CROP指定可

NOTE記事一覧ですnote.com

【スクリプトエディタで開く】 |

画像の右左に分割別ファイルR2L.applescript.scpt
ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(*
004見開き画像を画像の左右分割
005左右方向にCROPする
006左右の余白を入力してCROPする
007
008右→左の順
009
010v1 ファイルの選択順に処理+連番
011v1.1 パス順にソート そのソート後のファイル順に処理
012
013com.cocolog-nifty.quicktimer.icefloe *)
014----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
015use AppleScript version "2.8"
016use framework "Foundation"
017use framework "AppKit"
018use scripting additions
019
020property refMe : a reference to current application
021
022############################
023### 入力ダイアログ
024############################
025## クリップボードの中身取り出し
026###初期化
027set appPasteboard to refMe's NSPasteboard's generalPasteboard()
028set ocidPastBoardTypeArray to appPasteboard's types
029###テキストがあれば
030set boolContain to ocidPastBoardTypeArray's containsObject:"public.utf8-plain-text"
031if boolContain = true then
032   ###値を格納する
033   tell application "Finder"
034      set strReadString to (the clipboard as text) as text
035   end tell
036   ###Finderでエラーしたら
037else
038   set boolContain to ocidPastBoardTypeArray's containsObject:"NSStringPboardType"
039   if boolContain = true then
040      set ocidReadString to ocidPasteboard's readObjectsForClasses:({refMe's NSString}) options:(missing value)
041      set strReadString to ocidReadString as text
042   else
043      log "テキストなし"
044      set strReadString to ("横方向左右CROP『px』数値入力") as text
045   end if
046end if
047
048###########################
049#ダイアログを全面に
050set strName to (name of current application) as text
051if strName is "osascript" then
052   tell application "System Events" to activate
053else
054   tell current application to activate
055end if
056set aliasIconPath to (POSIX file "/System/Applications/Calculator.app/Contents/Resources/AppIcon.icns") as alias
057set strTitle to ("入力してください") as text
058set strMes to ("横方向左右CROP『px』数値入力\n例:100だと\n左100px右100pxCropします") as text
059tell application "System Events"
060   activate
061   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)
062end tell
063if (gave up of recordResult) is true then
064   return "時間切れです"
065else if (button returned of recordResult) is "キャンセル" then
066   return "キャンセルです"
067else
068   set strReturnedText to (text returned of recordResult) as text
069end if
070#戻り値の整形
071set ocidResponseText to (refMe's NSString's stringWithString:(strReturnedText))
072###タブと改行を除去しておく
073set ocidTextM to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
074ocidTextM's appendString:(ocidResponseText)
075##改行除去
076set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\n") withString:("")
077set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\r") withString:("")
078##タブ除去
079set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\t") withString:("")
080####戻り値を半角にする
081set ocidNSStringTransform to (refMe's NSStringTransformFullwidthToHalfwidth)
082set ocidTextM to (ocidTextM's stringByApplyingTransform:ocidNSStringTransform |reverse|:false)
083##カンマ置換
084set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:(",") withString:(".")
085###数字以外の値を取る
086set ocidDecSet to refMe's NSCharacterSet's decimalDigitCharacterSet
087set ocidCharSet to ocidDecSet's invertedSet()
088set ocidCharArray to ocidTextM's componentsSeparatedByCharactersInSet:ocidCharSet
089set ocidInteger to ocidCharArray's componentsJoinedByString:""
090#
091set numCropW to ocidInteger as integer
092###########################
093#ファイル選択
094set strName to (name of current application) as text
095if strName is "osascript" then
096   tell application "SystemUIServer" to activate
097else
098   tell current application to activate
099end if
100set appFileManager to refMe's NSFileManager's defaultManager()
101set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
102set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
103set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias
104set listUTI to {"public.image"} as list
105set strMes to ("ファイルを選んでください") as text
106set strPrompt to ("ファイルを選んでください") as text
107try
108   tell application "SystemUIServer"
109      activate
110      set listAliasFilePath to (choose file strMes with prompt strPrompt default location aliasDefaultLocation of type listUTI with invisibles, multiple selections allowed and showing package contents) as list
111   end tell
112on error
113   tell application "SystemUIServer" to quit
114   log "エラーしました"
115   return "エラーしました"
116end try
117if listAliasFilePath is {} then
118   return "選んでください"
119end if
120###########################
121#保存先選択
122set strName to (name of current application) as text
123if strName is "osascript" then
124   tell application "SystemUIServer" to activate
125else
126   tell current application to activate
127end if
128set strMes to "フォルダを選んでください" as text
129set strPrompt to "フォルダを選択してください" as text
130try
131   tell application "SystemUIServer"
132      activate
133      set aliasResponse to (choose folder strMes with prompt strPrompt default location aliasDefaultLocation with invisibles and showing package contents without multiple selections allowed) as alias
134   end tell
135on error
136   log "エラーしました"
137   return "エラーしました"
138end try
139#保存先フォルダ
140set strSaveDirPath to (POSIX path of aliasResponse) as text
141set ocidSaveDirPathStr to refMe's NSString's stringWithString:(strSaveDirPath)
142set ocidSaveDirPath to ocidSaveDirPathStr's stringByStandardizingPath()
143set ocidSaveDirPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveDirPath) isDirectory:(false)
144#ページ番号用の連番
145set numPageNo to 0 as integer
146
147###########################
148#入力ファイルをソートする
149set ocidFileURLArray to refMe's NSMutableArray's alloc()'s init()
150#
151repeat with itemAliasFilePath in listAliasFilePath
152   set aliasFilePath to itemAliasFilePath as alias
153   set strFilePath to (POSIX path of aliasFilePath) as text
154   set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
155   set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
156   set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(false))
157   (ocidFileURLArray's addObject:(ocidFilePathURL))
158   
159end repeat
160
161
162
163set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("path") ascending:(yes) selector:("localizedStandardCompare:")
164set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
165set ocidFileURLArray to ocidFileURLArray's sortedArrayUsingDescriptors:(ocidDescriptorArray)
166
167
168(*
169#
170#repeat with itemAliasFilePath in listAliasFilePath
171   ###########################
172   #入力
173   set aliasFilePath to itemAliasFilePath as alias
174   set strFilePath to (POSIX path of aliasFilePath) as text
175   set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
176   set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
177   set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(false))
178*)
179
180repeat with itemFilePathURL in ocidFileURLArray
181   
182   set ocidFilePathURL to itemFilePathURL
183   
184   set ocidFileName to ocidFilePathURL's lastPathComponent()
185   set strBaseFileName to ocidFileName's stringByDeletingPathExtension() as text
186   #NSDATA
187   set ocidOption to (refMe's NSDataReadingMappedIfSafe)
188   set listResponse to (refMe's NSData's alloc()'s initWithContentsOfURL:(ocidFilePathURL) options:(ocidOption) |error|:(reference))
189   if (item 2 of listResponse) = (missing value) then
190      set ocidReadData to (item 1 of listResponse)
191   else if (item 2 of listResponse) ≠ (missing value) then
192      set strErrorNO to (item 2 of listResponse)'s code() as text
193      set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text
194      refMe's NSLog("■:" & strErrorNO & strErrorMes)
195      return "initWithContentsOfURL エラーしました" & strErrorNO & strErrorMes
196   end if
197   #NSIMAGE
198   set ocidReadImage to (refMe's NSImage's alloc()'s initWithData:(ocidReadData))
199   set sizeImageInPt to ocidReadImage's |size|()
200   set numInWpt to sizeImageInPt's width()
201   set numInHpt to sizeImageInPt's height()
202   #BitMapRepに変換
203   set ocidmageRepArray to ocidReadImage's representations()
204   set ocidReadImageRep to ocidmageRepArray's firstObject()
205   set numInWpx to ocidReadImageRep's pixelsWide()
206   set numInHpx to ocidReadImageRep's pixelsHigh()
207   #72ppiに変換する
208   set sizeImageInPx to refMe's NSSize's NSMakeSize(numInWpx, numInHpx)
209   (ocidReadImageRep's setSize:(sizeImageInPx))
210   
211   ###########################
212   #まずは、画像の左右をCROPする
213   set numSetCropWpx to (numInWpx - (numCropW * 2)) as integer
214   set numSetCropHpx to numInHpx as integer
215   #出力用の画像ピクセルサイズ
216   set sizeCropImage to refMe's NSSize's NSMakeSize(numSetCropWpx, numSetCropHpx)
217   #出力用イメージ NSBitmapImageRep
218   #   samplesPerPixel
219   set intSPP to 4 as integer
220   #   bitsPerSample
221   set intBPS to 8 as integer
222   # bytesPerRow
223   set intBPR to 0 as integer
224   #   bitsPerPixel
225   set intBPP to 32 as integer
226   #   RGB系のカラースペース
227   set ocidColorSpaceName to refMe's NSCalibratedRGBColorSpace
228   #   アルファあり
229   set ocidBitmapFormat to refMe's NSAlphaFirstBitmapFormat
230   ##出力ピクセルサイズのブランクイメージ 
231   set ocidArtBoardRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numSetCropWpx) pixelsHigh:(numSetCropHpx) bitsPerSample:(intBPS) samplesPerPixel:(intSPP) hasAlpha:(true) isPlanar:(false) colorSpaceName:(ocidColorSpaceName) bitmapFormat:(ocidBitmapFormat) bytesPerRow:(intBPR) bitsPerPixel:(intBPP))
232   ###########################
233   #画像合成開始
234   #背景白の画像(アートボードになります)
235   #▼▼▼▼▼ saveGraphicsState
236   refMe's NSGraphicsContext's saveGraphicsState()
237   #Context 
238   set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep))
239   ###生成された画像でNSGraphicsContext初期化
240   (refMe's NSGraphicsContext's setCurrentContext:(ocidContext))
241   #色を個別に指定する場合 値は0が暗 1が明
242   set ocidSetColor to (refMe's NSColor's colorWithSRGBRed:(1.0) green:(1.0) blue:(1.0) alpha:(0.0))
243   ocidSetColor's |set|()
244   #画像生成
245   set ocidSaveImageRect to refMe's NSRect's NSMakeRect(0, 0, numSetCropWpx, numSetCropHpx)
246   #refMe's NSRectFill({origin:{x:0, y:0}, |size|:{width:(numSetPxWidth), height:(numSetPxHeight)}})
247   refMe's NSRectFill(ocidSaveImageRect)
248   #画像作成終了
249   refMe's NSGraphicsContext's restoreGraphicsState()
250   #▲▲▲▲▲▲ restoreGraphicsState
251   
252   ###########################
253   #画像合成開始
254   #▼▼▼▼▼ saveGraphicsState
255   refMe's NSGraphicsContext's saveGraphicsState()
256   #Context 
257   set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep))
258   #生成された画像でNSGraphicsContext初期化
259   (refMe's NSGraphicsContext's setCurrentContext:(ocidContext))
260   #左に指定サイズ分ずらしたRECT
261   # {origin:{x:(numCropW ), y:(0)}, |size|:{width:(numSetPxWidth), Hight:(numSetPxHeight)}}
262   set ocidFromRect to refMe's NSRect's NSMakeRect(numCropW, 0, numSetCropWpx, numSetCropHpx)
263   #NSCompositeSourceOver
264   set ocidOption to (refMe's NSCompositingOperationSourceOver)
265   #drawInRect いわゆるペースト
266   (ocidReadImageRep's drawInRect:(ocidSaveImageRect) fromRect:(ocidFromRect) operation:(ocidOption) fraction:1.0 respectFlipped:true hints:(missing value))
267   #画像作成終了
268   refMe's NSGraphicsContext's restoreGraphicsState()
269   #▲▲▲▲▲▲ restoreGraphicsState
270   
271   
272   
273   ###########################
274   #ページ処理 右ページ
275   ###########################
276   #まずは、画像の左右をCROPする
277   set numSetPageWpx to (numSetCropWpx / 2) as integer
278   set numSetPageHpx to numInHpx as integer
279   #出力用の画像ピクセルサイズ
280   set sizeCropImage to refMe's NSSize's NSMakeSize(numSetPageWpx, numSetPageHpx)
281   ##出力ピクセルサイズのブランクイメージ 
282   set ocidPageArtBoardRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numSetPageWpx) pixelsHigh:(numSetPageHpx) bitsPerSample:(intBPS) samplesPerPixel:(intSPP) hasAlpha:(true) isPlanar:(false) colorSpaceName:(ocidColorSpaceName) bitmapFormat:(ocidBitmapFormat) bytesPerRow:(intBPR) bitsPerPixel:(intBPP))
283   set ocidSaveImageRect to refMe's NSRect's NSMakeRect(0, 0, numSetPageWpx, numSetPageHpx)
284   ###########################
285   #画像合成開始
286   #▼▼▼▼▼ saveGraphicsState
287   refMe's NSGraphicsContext's saveGraphicsState()
288   #Context 
289   set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidPageArtBoardRep))
290   #生成された画像でNSGraphicsContext初期化
291   (refMe's NSGraphicsContext's setCurrentContext:(ocidContext))
292   #左に指定サイズ分ずらしたRECT
293   # {origin:{x:(numCropW ), y:(0)}, |size|:{width:(numSetPxWidth), Hight:(numSetPxHeight)}}
294   set ocidFromRect to refMe's NSRect's NSMakeRect(numSetPageWpx, 0, numSetPageWpx, numSetPageHpx)
295   #NSCompositeSourceOver
296   set ocidOption to (refMe's NSCompositingOperationSourceOver)
297   #drawInRect いわゆるペースト
298   (ocidArtBoardRep's drawInRect:(ocidSaveImageRect) fromRect:(ocidFromRect) operation:(ocidOption) fraction:1.0 respectFlipped:true hints:(missing value))
299   #画像作成終了
300   refMe's NSGraphicsContext's restoreGraphicsState()
301   #▲▲▲▲▲▲ restoreGraphicsState
302   
303   
304   ###########################
305   #保存用にNSDATAに変換
306   set ocidProperty to refMe's NSMutableDictionary's alloc()'s init()
307   (ocidProperty's setObject:(refMe's NSNumber's numberWithBool:false) forKey:(refMe's NSImageInterlaced))
308   (ocidProperty's setObject:(refMe's NSNumber's numberWithDouble:(1 / 2.2)) forKey:(refMe's NSImageGamma))
309   set ocidSaveData to (ocidPageArtBoardRep's representationUsingType:(refMe's NSBitmapImageFileTypePNG) |properties|:(ocidProperty))
310   #
311   set numPageNo to (numPageNo + 1) as integer
312   set strPageNo to numPageNo as text
313   set strZero to "0000" as text
314   set strPageNo to ("" & strZero & strPageNo & "") as text
315   set strPageNo to (text 2 through -1 of strPageNo) as text
316   #
317   set strSaveFileName to ("" & strPageNo & "R_" & strBaseFileName & "@" & numSetCropWpx & "x" & numSetCropHpx & "")
318   set ocidBaseSaveFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false))
319   set ocidSaveFilePathURL to (ocidBaseSaveFilePathURL's URLByAppendingPathExtension:("png"))
320   ##NSDATA
321   set ocidOption to (refMe's NSDataWritingAtomic)
322   set listDone to (ocidSaveData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error|:(reference))
323   if (item 1 of listDone) is true then
324      #   log "writeToURL 正常処理"
325   else if (item 2 of listDone) ≠ (missing value) then
326      set strErrorNO to (item 2 of listDone)'s code() as text
327      set strErrorMes to (item 2 of listDone)'s localizedDescription() as text
328      refMe's NSLog("■:" & strErrorNO & strErrorMes)
329      return "writeToURL エラーしました" & strErrorNO & strErrorMes
330   end if
331   ###########################
332   #ページ処理 左ページ
333   ###########################
334   #まずは、画像の左右をCROPする
335   set numSetPageWpx to (numSetCropWpx / 2) as integer
336   set numSetPageHpx to numInHpx as integer
337   #出力用の画像ピクセルサイズ
338   set sizeCropImage to refMe's NSSize's NSMakeSize(numSetPageWpx, numSetPageHpx)
339   ##出力ピクセルサイズのブランクイメージ 
340   set ocidPageArtBoardRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numSetPageWpx) pixelsHigh:(numSetPageHpx) bitsPerSample:(intBPS) samplesPerPixel:(intSPP) hasAlpha:(true) isPlanar:(false) colorSpaceName:(ocidColorSpaceName) bitmapFormat:(ocidBitmapFormat) bytesPerRow:(intBPR) bitsPerPixel:(intBPP))
341   set ocidSaveImageRect to refMe's NSRect's NSMakeRect(0, 0, numSetPageWpx, numSetPageHpx)
342   ###########################
343   #画像合成開始
344   #▼▼▼▼▼ saveGraphicsState
345   refMe's NSGraphicsContext's saveGraphicsState()
346   #Context 
347   set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidPageArtBoardRep))
348   #生成された画像でNSGraphicsContext初期化
349   (refMe's NSGraphicsContext's setCurrentContext:(ocidContext))
350   #左に指定サイズ分ずらしたRECT
351   # {origin:{x:(numCropW ), y:(0)}, |size|:{width:(numSetPxWidth), Hight:(numSetPxHeight)}}
352   set ocidFromRect to refMe's NSRect's NSMakeRect(0, 0, numSetPageWpx, numSetPageHpx)
353   #NSCompositeSourceOver
354   set ocidOption to (refMe's NSCompositingOperationSourceOver)
355   #drawInRect いわゆるペースト
356   (ocidArtBoardRep's drawInRect:(ocidSaveImageRect) fromRect:(ocidFromRect) operation:(ocidOption) fraction:1.0 respectFlipped:true hints:(missing value))
357   #画像作成終了
358   refMe's NSGraphicsContext's restoreGraphicsState()
359   #▲▲▲▲▲▲ restoreGraphicsState
360   
361   
362   ###########################
363   #保存用にNSDATAに変換
364   set ocidProperty to refMe's NSMutableDictionary's alloc()'s init()
365   (ocidProperty's setObject:(refMe's NSNumber's numberWithBool:false) forKey:(refMe's NSImageInterlaced))
366   (ocidProperty's setObject:(refMe's NSNumber's numberWithDouble:(1 / 2.2)) forKey:(refMe's NSImageGamma))
367   set ocidSaveData to (ocidPageArtBoardRep's representationUsingType:(refMe's NSBitmapImageFileTypePNG) |properties|:(ocidProperty))
368   #
369   set numPageNo to (numPageNo + 1) as integer
370   set strPageNo to numPageNo as text
371   set strZero to "0000" as text
372   set strPageNo to ("" & strZero & strPageNo & "") as text
373   set strPageNo to (text 2 through -1 of strPageNo) as text
374   #
375   set strSaveFileName to ("" & strPageNo & "L_" & strBaseFileName & "@" & numSetCropWpx & "x" & numSetCropHpx & "")
376   set ocidBaseSaveFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false))
377   set ocidSaveFilePathURL to (ocidBaseSaveFilePathURL's URLByAppendingPathExtension:("png"))
378   ##NSDATA
379   set ocidOption to (refMe's NSDataWritingAtomic)
380   set listDone to (ocidSaveData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error|:(reference))
381   if (item 1 of listDone) is true then
382      #   log "writeToURL 正常処理"
383   else if (item 2 of listDone) ≠ (missing value) then
384      set strErrorNO to (item 2 of listDone)'s code() as text
385      set strErrorMes to (item 2 of listDone)'s localizedDescription() as text
386      refMe's NSLog("■:" & strErrorNO & strErrorMes)
387      return "writeToURL エラーしました" & strErrorNO & strErrorMes
388   end if
389   
390   
391   
392end repeat
393
394
395
AppleScriptで生成しました