20260530

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

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

NOTE記事一覧ですnote.com
 

【Safari・FireFox用Script Editorで開く】 |

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