NSGraphicsContext

画像ファイルを2in1 2upにマージする(読み方向;上から下) 修正


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
(*
NSAlphaFirstBitmapFormatが廃止になるので
NSBitmapFormatAlphaFirstに変更した
com.cocolog-nifty.quicktimer.icefloe
NSCompositeSourceOverが廃止になるので
NSCompositingOperationSourceOverに変更
*)
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application

set appFileManager to refMe's NSFileManager's defaultManager()
set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias
#############################
#ファイル選択
set strName to (name of current application) as text
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
set listUTI to {"public.image"}
set strMes to ("ファイルを選んでください") as text
set strPrompt to ("ファイルを選んでください") as text
try
  ### ファイル選択時
  set listAliasFilePath to (choose file strMes with prompt strPrompt default location (aliasDefaultLocation) of type listUTI with invisibles, showing package contents and multiple selections allowed) as list
on error
log "エラーしました"
return "エラーしました"
end try
############
#URLのARRAYに
set ocidFilePathURLArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
repeat with itemAliasFilePath in listAliasFilePath
  set strFilePath to (POSIX path of itemAliasFilePath) as text
  set ocidFilePath to (refMe's NSString's stringWithString:(strFilePath))
  set ocidFilePathURL to (refMe's NSURL's fileURLWithPath:(ocidFilePath))
(ocidFilePathURLArray's addObject:(ocidFilePathURL))
end repeat
set numCntURL to (count of ocidFilePathURLArray) as integer
############
#並び替え
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("absoluteString") ascending:(yes) selector:("localizedStandardCompare:")
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidFilePathURLArray's sortedArrayUsingDescriptors:(ocidDescriptorArray)
#ダイアログ用のURL
set ocidFirstObjectURL to ocidSortedArray's firstObject()
set ocidContainerDirPathURL to ocidFirstObjectURL's URLByDeletingLastPathComponent()
set aliasSaveDirPath to (ocidContainerDirPathURL's absoluteURL()) as alias
############
#保存先
set strMes to "保存先フォルダを選んでください" as text
set strPrompt to "保存先フォルダを選択してください" as text
try
  set aliasSaveDirPath to (choose folder strMes with prompt strPrompt default location aliasSaveDirPath with invisibles and showing package contents without multiple selections allowed) as alias
on error
log "エラーしました"
return "エラーしました"
end try
set strSaveDirPath to (POSIX path of aliasSaveDirPath) as text
set ocidSaveDirPath to refMe's NSString's stringWithString:(strSaveDirPath)
set ocidSaveDirPathURL to refMe's NSURL's fileURLWithPath:(ocidSaveDirPath)

############
set numOutPutPageNo to 1 as integer
repeat with itemIntNo from 0 to (numCntURL - 1) by 2
  #上ページURL取り出し
  set ocidFilePathURLR to (ocidSortedArray's objectAtIndex:(itemIntNo))
  if numCntURL > (itemIntNo + 1) then
    #下ページURL取り出し
    set ocidFilePathURLL to (ocidSortedArray's objectAtIndex:(itemIntNo + 1))
  end if
  ############DATA
  #上ページ
  set ocidOption to (refMe's NSDataReadingMappedIfSafe)
  set listDataR to (refMe's NSData's alloc()'s initWithContentsOfURL:(ocidFilePathURLR) options:(ocidOption) |error|:(reference))
  if (item 2 of listDataR) ≠ (missing value) then
log (item 2 of listDataR)'s localizedFailureReason() as text
return "データ読み込みでエラーになりました"
  else
    set ocidDataR to (item 1 of listDataR)
  end if
  #下ページ
  if numCntURL > (itemIntNo + 1) then
    set listDataL to (refMe's NSData's alloc()'s initWithContentsOfURL:(ocidFilePathURLL) options:(ocidOption) |error|:(reference))
    if (item 2 of listDataL) ≠ (missing value) then
log (item 2 of listDataL)'s localizedFailureReason() as text
return "データ読み込みでエラーになりました"
    else
      set ocidDataL to (item 1 of listDataL)
    end if
  end if
  
  ############IMAGE 上
  #イメージに変換 上ページ
  set ocidImageR to (refMe's NSImage's alloc()'s initWithData:(ocidDataR))
  #BmpRepに変換
  set ocidImageRepArrayR to ocidImageR's representations()
  set ocidImageRepR to ocidImageRepArrayR's firstObject()
  ############IMAGEのサイズ
  #ptサイズ 上
  set ocidSizeR to ocidImageRepR's |size|()
  set numWptR to ocidSizeR's width
  set numHptR to ocidSizeR's height
  #pxサイズ 上
  set numWpxR to ocidImageRepR's pixelsWide()
  set numHpxR to ocidImageRepR's pixelsHigh()
  #BmpRepを72ppiにしておく
  set ocidSetSizeR to refMe's NSMakeSize(numWpxR, numHpxR)
(ocidImageRepR's setSize:(ocidSetSizeR))
  ############IMAGE 下
  if numCntURL > (itemIntNo + 1) then
    #イメージに変換 下
    set ocidImageL to (refMe's NSImage's alloc()'s initWithData:(ocidDataL))
    #BmpRepに変換
    set ocidImageRepArrayL to ocidImageL's representations()
    set ocidImageRepL to ocidImageRepArrayL's firstObject()
    #奇数ページ対応で白紙画像を入れる
  else
    ##白紙
    set ocidImageRepL to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numWpxR) pixelsHigh:(numHpxR) bitsPerSample:8 samplesPerPixel:3 hasAlpha:false isPlanar:false colorSpaceName:(refMe's NSCalibratedRGBColorSpace) bitmapFormat:(refMe's NSBitmapFormatThirtyTwoBitLittleEndian) bytesPerRow:0 bitsPerPixel:32)
    #初期化
refMe's NSGraphicsContext's saveGraphicsState()
    #読み込み
(refMe's NSGraphicsContext's setCurrentContext:(refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidImageRepL)))
    #塗り色を『白』に指定して
refMe's NSColor's whiteColor()'s |set|()
refMe's NSRectFill({origin:{x:0, y:0}, |size|:{width:(numWpxR), height:(numHpxR)}})
    #画像作成終了
refMe's NSGraphicsContext's restoreGraphicsState()
  end if
  ##
  #ptサイズ 下
  set ocidSizeL to ocidImageRepL's |size|()
  set numWptL to ocidSizeL's width
  set numHptL to ocidSizeL's height
  #pxサイズ 下
  set numWpxL to ocidImageRepL's pixelsWide()
  set numHpxL to ocidImageRepL's pixelsHigh()
  #解像度 下
  set numResolutionL to (numWpxL / numWptL) as number
  #BmpRepを72ppiにしておく
  set ocidSetSizeL to refMe's NSMakeSize(numWpxL, numHpxL)
(ocidImageRepL's setSize:(ocidSetSizeL))
  ############出力イメージ サイズPX
  if numWpxR ≥ numWpxL then
    set numOutPutW to (numWpxR) as number
  else
    set numOutPutW to (numWpxL) as number
  end if
  set numOutPutH to (numHpxR + numHpxL) as number
  
  ############合成処理開始
  ##出力用REP
  set ocidAardboardRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numOutPutW) pixelsHigh:(numOutPutH) bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:false colorSpaceName:(refMe's NSCalibratedRGBColorSpace) bitmapFormat:(refMe's NSBitmapFormatAlphaFirst) bytesPerRow:0 bitsPerPixel:32)
  ##ペースト位置RECT 上
  set ocidPasteRectR to {origin:{x:((numOutPutW - numWpxR) / 2), y:(numHpxL)}, |size|:{width:(numWpxR), height:(numHpxR)}}
  ##ペースト位置RECT 下
  set ocidPasteRectL to {origin:{x:((numOutPutW - numWpxL) / 2), y:(0)}, |size|:{width:(numWpxL), height:(numHpxL)}}
  
  ##元データのRECT(コピー位置) 右
  set ocidCopyRectR to {origin:{x:(0), y:(0)}, |size|:{width:(numWpxR), height:(numHpxR)}}
  ##元データのRECT(コピー位置) 左
  set ocidCopyRectL to {origin:{x:(0), y:(0)}, |size|:{width:(numWpxL), height:(numHpxL)}}
  
  ##################
  ##処理開始
  set ocidGraphicsContext to refMe's NSGraphicsContext
ocidGraphicsContext's saveGraphicsState()
  ##アートボード(出力イメージ)を処理内に読み込んで
(ocidGraphicsContext's setCurrentContext:(ocidGraphicsContext's graphicsContextWithBitmapImageRep:(ocidAardboardRep)))
  ##合成 上
(ocidImageRepR's drawInRect:(ocidPasteRectR) fromRect:(ocidCopyRectR) operation:(refMe's NSCompositingOperationSourceOver) fraction:1.0 respectFlipped:false hints:(missing value))
  ##合成 下
(ocidImageRepL's drawInRect:(ocidPasteRectL) fromRect:(ocidCopyRectL) operation:(refMe's NSCompositingOperationSourceOver) fraction:1.0 respectFlipped:false hints:(missing value))
  ##処理終了
ocidGraphicsContext's restoreGraphicsState()
  
  ##################
  ###変換オプション
  set ocidPropertiesDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
(ocidPropertiesDict's setValue:(false) forKey:(refMe's NSImageInterlaced))
(ocidPropertiesDict's setValue:(2.2) forKey:(refMe's NSImageGamma))
  #PNG変換でデータにする
  set ocidSaveData to (ocidAardboardRep's representationUsingType:(refMe's NSPNGFileType) |properties|:(ocidPropertiesDict))
  #保存パス
  #ページ番号0パディング
  set strOutPutPageNo to text -3 through -1 of ("000" & (numOutPutPageNo as text)) as text
  set strSaveFileName to (strOutPutPageNo & "-" & numOutPutW & "x" & numOutPutH) as text
  set ocidBaseSaveFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false))
  set ocidSaveFilePathURL to (ocidBaseSaveFilePathURL's URLByAppendingPathExtension:("png"))
  #保存
  set listDone to (ocidSaveData's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error|:(reference))
  if (item 2 of listDone) ≠ (missing value) then
log (item 2 of listDone)'s localizedFailureReason() as text
return "データ読み込みでエラーになりました"
  end if
  set numOutPutPageNo to numOutPutPageNo + 1 as integer
  
end repeat


return





|

画像ファイルを2in1 2upにマージする(読み方向;右から左) 修正


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
(*
NSAlphaFirstBitmapFormatが廃止になるので
NSBitmapFormatAlphaFirstに変更した
NSCompositeSourceOverが廃止になるので
NSCompositingOperationSourceOverに変更

com.cocolog-nifty.quicktimer.icefloe
*)
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application

set appFileManager to refMe's NSFileManager's defaultManager()
set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias
#############################
#ファイル選択
set strName to (name of current application) as text
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
set listUTI to {"public.image"}
set strMes to ("ファイルを選んでください") as text
set strPrompt to ("ファイルを選んでください") as text
try
  ### ファイル選択時
  set listAliasFilePath to (choose file strMes with prompt strPrompt default location (aliasDefaultLocation) of type listUTI with invisibles, showing package contents and multiple selections allowed) as list
on error
log "エラーしました"
return "エラーしました"
end try
############
#URLのARRAYに
set ocidFilePathURLArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
repeat with itemAliasFilePath in listAliasFilePath
  set strFilePath to (POSIX path of itemAliasFilePath) as text
  set ocidFilePath to (refMe's NSString's stringWithString:(strFilePath))
  set ocidFilePathURL to (refMe's NSURL's fileURLWithPath:(ocidFilePath))
(ocidFilePathURLArray's addObject:(ocidFilePathURL))
end repeat
set numCntURL to (count of ocidFilePathURLArray) as integer
############
#並び替え
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("absoluteString") ascending:(yes) selector:("localizedStandardCompare:")
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidFilePathURLArray's sortedArrayUsingDescriptors:(ocidDescriptorArray)
#ダイアログ用のURL
set ocidFirstObjectURL to ocidSortedArray's firstObject()
set ocidContainerDirPathURL to ocidFirstObjectURL's URLByDeletingLastPathComponent()
set aliasSaveDirPath to (ocidContainerDirPathURL's absoluteURL()) as alias
############
#保存先
set strMes to "保存先フォルダを選んでください" as text
set strPrompt to "保存先フォルダを選択してください" as text
try
  set aliasSaveDirPath to (choose folder strMes with prompt strPrompt default location aliasSaveDirPath with invisibles and showing package contents without multiple selections allowed) as alias
on error
log "エラーしました"
return "エラーしました"
end try
set strSaveDirPath to (POSIX path of aliasSaveDirPath) as text
set ocidSaveDirPath to refMe's NSString's stringWithString:(strSaveDirPath)
set ocidSaveDirPathURL to refMe's NSURL's fileURLWithPath:(ocidSaveDirPath)

############
set numOutPutPageNo to 1 as integer
repeat with itemIntNo from 0 to (numCntURL - 1) by 2
  #左右ページ取り出し
  set ocidFilePathURLR to (ocidSortedArray's objectAtIndex:(itemIntNo))
  if numCntURL > (itemIntNo + 1) then
    set ocidFilePathURLL to (ocidSortedArray's objectAtIndex:(itemIntNo + 1))
  end if
  ############DATA
  #右ページ
  set ocidOption to (refMe's NSDataReadingMappedIfSafe)
  set listDataR to (refMe's NSData's alloc()'s initWithContentsOfURL:(ocidFilePathURLR) options:(ocidOption) |error|:(reference))
  if (item 2 of listDataR) ≠ (missing value) then
log (item 2 of listDataR)'s localizedFailureReason() as text
return "データ読み込みでエラーになりました"
  else
    set ocidDataR to (item 1 of listDataR)
  end if
  #左ページ
  if numCntURL > (itemIntNo + 1) then
    set listDataL to (refMe's NSData's alloc()'s initWithContentsOfURL:(ocidFilePathURLL) options:(ocidOption) |error|:(reference))
    if (item 2 of listDataL) ≠ (missing value) then
log (item 2 of listDataL)'s localizedFailureReason() as text
return "データ読み込みでエラーになりました"
    else
      set ocidDataL to (item 1 of listDataL)
    end if
  end if
  
  ############IMAGE 右
  #イメージに変換 右
  set ocidImageR to (refMe's NSImage's alloc()'s initWithData:(ocidDataR))
  #BmpRepに変換
  set ocidImageRepArrayR to ocidImageR's representations()
  set ocidImageRepR to ocidImageRepArrayR's firstObject()
  ############IMAGEのサイズ
  #ptサイズ 右
  set ocidSizeR to ocidImageRepR's |size|()
  set numWptR to ocidSizeR's width
  set numHptR to ocidSizeR's height
  #pxサイズ 右
  set numWpxR to ocidImageRepR's pixelsWide()
  set numHpxR to ocidImageRepR's pixelsHigh()
  #BmpRepを72ppiにしておく
  set ocidSetSizeR to refMe's NSMakeSize(numWpxR, numHpxR)
(ocidImageRepR's setSize:(ocidSetSizeR))
  ############IMAGE 左
  if numCntURL > (itemIntNo + 1) then
    #イメージに変換 左
    set ocidImageL to (refMe's NSImage's alloc()'s initWithData:(ocidDataL))
    #BmpRepに変換
    set ocidImageRepArrayL to ocidImageL's representations()
    set ocidImageRepL to ocidImageRepArrayL's firstObject()
    #奇数ページ対応で白紙画像を入れる
  else
    ##白紙
    set ocidImageRepL to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numWpxR) pixelsHigh:(numHpxR) bitsPerSample:8 samplesPerPixel:3 hasAlpha:false isPlanar:false colorSpaceName:(refMe's NSCalibratedRGBColorSpace) bitmapFormat:(refMe's NSBitmapFormatThirtyTwoBitLittleEndian) bytesPerRow:0 bitsPerPixel:32)
    #初期化
refMe's NSGraphicsContext's saveGraphicsState()
    #読み込み
(refMe's NSGraphicsContext's setCurrentContext:(refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidImageRepL)))
    #塗り色を『白』に指定して
refMe's NSColor's whiteColor()'s |set|()
refMe's NSRectFill({origin:{x:0, y:0}, |size|:{width:(numWpxR), height:(numHpxR)}})
    #画像作成終了
refMe's NSGraphicsContext's restoreGraphicsState()
  end if
  ##
  #ptサイズ 左
  set ocidSizeL to ocidImageRepL's |size|()
  set numWptL to ocidSizeL's width
  set numHptL to ocidSizeL's height
  #pxサイズ 左
  set numWpxL to ocidImageRepL's pixelsWide()
  set numHpxL to ocidImageRepL's pixelsHigh()
  #解像度 左
  set numResolutionL to (numWpxL / numWptL) as number
  #BmpRepを72ppiにしておく
  set ocidSetSizeL to refMe's NSMakeSize(numWpxL, numHpxL)
(ocidImageRepL's setSize:(ocidSetSizeL))
  ############出力イメージ
  set numOutPutW to (numWpxR + numWpxL) as number
  if numHpxR ≥ numHpxL then
    set numOutPutH to numHpxR as number
  else if numHpxR < numHpxL then
    set numOutPutH to numHpxL as number
  end if
  ############合成処理開始
  ##出力用REP
  set ocidAardboardRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numOutPutW) pixelsHigh:(numOutPutH) bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:false colorSpaceName:(refMe's NSCalibratedRGBColorSpace) bitmapFormat:(refMe's NSBitmapFormatAlphaFirst) bytesPerRow:0 bitsPerPixel:32)
  ##ペースト位置RECT 右
  set ocidPasteRectR to {origin:{x:(numWpxL), y:(numOutPutH - numHpxR)}, |size|:{width:(numWpxR), height:(numHpxR)}}
  ##ペースト位置RECT 左
  set ocidPasteRectL to {origin:{x:0, y:(numOutPutH - numHpxL)}, |size|:{width:(numWpxL), height:(numHpxL)}}
  ##元データのRECT(コピー位置) 右
  set ocidCopyRectR to {origin:{x:(0), y:(0)}, |size|:{width:(numWpxR), height:(numHpxR)}}
  ##元データのRECT(コピー位置) 左
  set ocidCopyRectL to {origin:{x:(0), y:(0)}, |size|:{width:(numWpxL), height:(numHpxL)}}
  
  ##################
  ##処理開始
  set ocidGraphicsContext to refMe's NSGraphicsContext
ocidGraphicsContext's saveGraphicsState()
  ##アートボード(出力イメージ)を処理内に読み込んで
(ocidGraphicsContext's setCurrentContext:(ocidGraphicsContext's graphicsContextWithBitmapImageRep:(ocidAardboardRep)))
  ##合成 右
(ocidImageRepR's drawInRect:(ocidPasteRectR) fromRect:(ocidCopyRectR) operation:(refMe's NSCompositingOperationSourceOver) fraction:1.0 respectFlipped:false hints:(missing value))
  ##合成 左
(ocidImageRepL's drawInRect:(ocidPasteRectL) fromRect:(ocidCopyRectL) operation:(refMe's NSCompositingOperationSourceOver) fraction:1.0 respectFlipped:false hints:(missing value))
  ##処理終了
ocidGraphicsContext's restoreGraphicsState()
  
  ##################
  ###変換オプション
  set ocidPropertiesDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
(ocidPropertiesDict's setValue:(false) forKey:(refMe's NSImageInterlaced))
(ocidPropertiesDict's setValue:(2.2) forKey:(refMe's NSImageGamma))
  #PNG変換でデータにする
  set ocidSaveData to (ocidAardboardRep's representationUsingType:(refMe's NSPNGFileType) |properties|:(ocidPropertiesDict))
  #保存パス
  #ページ番号0パディング
  set strOutPutPageNo to text -3 through -1 of ("000" & (numOutPutPageNo as text)) as text
  set strSaveFileName to (strOutPutPageNo & "-" & numOutPutW & "x" & numOutPutH) as text
  set ocidBaseSaveFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false))
  set ocidSaveFilePathURL to (ocidBaseSaveFilePathURL's URLByAppendingPathExtension:("png"))
  #保存
  set listDone to (ocidSaveData's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error|:(reference))
  if (item 2 of listDone) ≠ (missing value) then
log (item 2 of listDone)'s localizedFailureReason() as text
return "データ読み込みでエラーになりました"
  end if
  set numOutPutPageNo to numOutPutPageNo + 1 as integer
  
end repeat


return





|

画像のリサイズ(解像度72ppi換算)後にPNG保存する


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application

############################
### 入力ダイアログ
############################
## クリップボードの中身取り出し
###初期化
set appPasteboard to refMe's NSPasteboard's generalPasteboard()
set ocidPastBoardTypeArray to appPasteboard's types
###テキストがあれば
set boolContain to ocidPastBoardTypeArray's containsObject:"public.utf8-plain-text"
if boolContain = true then
  ###値を格納する
  tell application "Finder"
    set strReadString to (the clipboard as text) as text
  end tell
  ###Finderでエラーしたら
else
  set boolContain to ocidPastBoardTypeArray's containsObject:"NSStringPboardType"
  if boolContain = true then
    set ocidReadString to ocidPasteboard's readObjectsForClasses:({refMe's NSString}) options:(missing value)
    set strReadString to ocidReadString as text
  else
log "テキストなし"
    set strReadString to ("リサイズ後の『幅px』数値入力") as text
  end if
end if

###ダイアログを全面に
set strName to (name of current application) as text
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
set aliasIconPath to (POSIX file "/System/Applications/Calculator.app/Contents/Resources/AppIcon.icns") as alias
set strTitle to ("入力してください") as text
set strMes to ("リサイズ後の『幅px』数値入力") as text
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)

if (gave up of recordResult) is true then
return "時間切れです"
else if (button returned of recordResult) is "キャンセル" then
return "キャンセルです"
else
  set strReturnedText to (text returned of recordResult) as text
end if
#戻り値の整形
set ocidResponseText to (refMe's NSString's stringWithString:(strReturnedText))
###タブと改行を除去しておく
set ocidTextM to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
ocidTextM's appendString:(ocidResponseText)
##改行除去
set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\n") withString:("")
set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\r") withString:("")
##タブ除去
set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\t") withString:("")
####戻り値を半角にする
set ocidNSStringTransform to (refMe's NSStringTransformFullwidthToHalfwidth)
set ocidTextM to (ocidTextM's stringByApplyingTransform:ocidNSStringTransform |reverse|:false)
##カンマ置換
set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:(",") withString:(".")
###数字以外の値を取る
set ocidDecSet to refMe's NSCharacterSet's decimalDigitCharacterSet
set ocidCharSet to ocidDecSet's invertedSet()
set ocidCharArray to ocidTextM's componentsSeparatedByCharactersInSet:ocidCharSet
set ocidInteger to ocidCharArray's componentsJoinedByString:""
#
set numNewPixelsWidth to ocidInteger as integer

############################
### ダイアログ
############################
set appFileManager to refMe's NSFileManager's defaultManager()
###デフォルトロケーション
set ocidUserDocumentPathArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDesktopPathURL to ocidUserDocumentPathArray's firstObject()
set aliasDefaultLocation to (ocidDesktopPathURL's absoluteURL()) as alias
#####ダイアログを前面に
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
####ダイアログを出す
set listUTI to {"public.image"} as list
set listAliasFilePath to (choose file with prompt "ファイルを選んでください" default location (aliasDefaultLocation) of type listUTI with invisibles, showing package contents and multiple selections allowed) as list

############################
####選んだファイルパスの数だけ繰り返し
############################
repeat with itemAliasFilePath in listAliasFilePath
  ##入力パス
  set strFilePath to (POSIX path of itemAliasFilePath) as text
  set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
  set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
  ###########################
  ###【1】元イメージ
  ###読み込むイメージURL
  set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
  ##NSIMAGEに読み込む
  set ocidReadImage to (refMe's NSImage's alloc()'s initWithContentsOfURL:(ocidFilePathURL))
  set ocidNsImageSize to ocidReadImage's |size|()
  set numPtWidth to ocidNsImageSize's width
  set numPtHeigh to ocidNsImageSize's height
log numPtWidth as integer
log numPtHeigh as integer
  ##BitMapRepに変換
  set ocidReadImageRepArray to ocidReadImage's representations()
  set ocidReadImageRep to (ocidReadImageRepArray's objectAtIndex:0)
  ##ピクセルサイズ取得
  set numPixelsWidth to ocidReadImageRep's pixelsWide()
  set numPixelsHeight to ocidReadImageRep's pixelsHigh()
  ##まずは72ppiにする
  set ocidPixelsSize to refMe's NSMakeSize(numPixelsWidth, numPixelsHeight)
(ocidReadImageRep's setSize:(ocidPixelsSize))
  ###########################
  ###【2】出力イメージ
  ###サイズ計算
  ### 画像サイズ変更比率=出力ピクセルサイズ縦➗読み込み画像ピクセルサイズ縦
  set numImageVW to numNewPixelsWidth / numPixelsWidth as number
  ###同じ比率で横幅を計算 読み込み画像ピクセルサイズ横×画像サイズ変更比率
  set numNewPixelsHeight to numPixelsHeight * numImageVW as integer
  ###↑で計算した出力用の画像ピクセルサイズ
  set ocidNewImagePixelsSize to refMe's NSMakeSize(numNewPixelsWidth, numNewPixelsHeight)
  ###
  ###Retinaモニター時 ポイントサイズ=pixの半分
  set intResPer to (144 / 72) as number
  set intNewWidth to (numNewPixelsWidth / intResPer) as integer
  set intNewHeight to (numNewPixelsHeight / intResPer) as integer
  set ocidNewImageSize to refMe's NSMakeSize(intNewWidth, intNewHeight)
  set ocidNewRect to refMe's NSMakeRect(0, 0, intNewWidth, intNewHeight)
  
  ###出力用イメージ NSBitmapImageRep
  # samplesPerPixel
  set intSPP to 4 as integer
  # bitsPerSample
  set intBPS to 8 as integer
  # bytesPerRow
  set intBPR to (numNewPixelsWidth * intSPP) as integer
  # bitsPerPixel
  set intBPP to 32 as integer
  # RGB系のカラースペース
  set ocidColorSpaceName to refMe's NSCalibratedRGBColorSpace
  # アルファあり
  set ocidBitmapFormat to refMe's NSAlphaFirstBitmapFormat
  ##出力ピクセルサイズのブランクイメージ
  set ocidArtBoardRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numNewPixelsWidth) pixelsHigh:(numNewPixelsHeight) bitsPerSample:(intBPS) samplesPerPixel:(intSPP) hasAlpha:(true) isPlanar:(false) colorSpaceName:(ocidColorSpaceName) bitmapFormat:(ocidBitmapFormat) bytesPerRow:(intBPR) bitsPerPixel:(intBPP))
  
  ###########################
  ###【3】ArtBord
  ### 初期化 CodeBase
refMe's NSGraphicsContext's saveGraphicsState()
  ###Context
  set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep))
  ###生成された画像でNSGraphicsContext初期化
(refMe's NSGraphicsContext's setCurrentContext:(ocidContext))
  ##色を個別に指定する場合 値は0が暗 1が明
  set ocidSetColor to (refMe's NSColor's colorWithSRGBRed:(1.0) green:(1.0) blue:(1.0) alpha:(0.0))
ocidSetColor's |set|()
  ###画像生成
refMe's NSRectFill({origin:{x:0, y:0}, |size|:{width:(numNewPixelsWidth), height:(numNewPixelsHeight)}})
  ####画像作成終了
refMe's NSGraphicsContext's restoreGraphicsState()
  
  ###########################
  ###【3】合成 リザイスでNSCompositeSourceOver
  ### 初期化
refMe's NSGraphicsContext's saveGraphicsState()
  ###Context
  set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep))
  ###NSGraphicsContext初期化
(refMe's NSGraphicsContext's setCurrentContext:(ocidContext))
  ####NSCompositeSourceOver
  set ocidDrawInRect to {origin:{x:(0), y:(0)}, |size|:{width:(numNewPixelsWidth), Hight:(numNewPixelsHeight)}}
  set ocidFromRect to (refMe's NSZeroRect)
  set ocidOption to (refMe's NSCompositeSourceOver)
  ## set ocidOption to (refMe's NSCompositeCopy)
(ocidReadImageRep's drawInRect:(ocidDrawInRect) fromRect:(ocidFromRect) operation:(ocidOption) fraction:1.0 respectFlipped:true hints:(missing value))
  ####画像作成終了
refMe's NSGraphicsContext's restoreGraphicsState()
  
  ###########################
  ###【5】保存
  ####保存オプション
  set ocidProperty to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
(ocidProperty's setObject:(refMe's NSNumber's numberWithBool:false) forKey:(refMe's NSImageInterlaced))
(ocidProperty's setObject:(refMe's NSNumber's numberWithDouble:(1 / 2.2)) forKey:(refMe's NSImageGamma))
  
  ###保存は別ディレクトリに
  set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
  set ocidFileName to ocidFilePathURL's lastPathComponent()
  #フォルダを作って
  set strSaveDirName to ("_ResizedImage") as text
  set ocidSaveDirPathURL to (ocidContainerDirPathURL's URLByAppendingPathComponent:(strSaveDirName))
  set ocidAttrDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
  # 777-->511 755-->493 700-->448 766-->502
(ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions))
  set listBoolMakeDir to (appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference))
  #保存先パス
  set ocidSaveFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(ocidFileName))
  ##保存
  set ocidNSInlineData to (ocidArtBoardRep's representationUsingType:(refMe's NSBitmapImageFileTypePNG) |properties|:(ocidProperty))
  set boolDone to (ocidNSInlineData's writeToURL:(ocidSaveFilePathURL) atomically:true)
  
  set ocidReadImageRep to ""
  set ocidArtBoardRep to ""
end repeat


|

生成した画像に文字を描画する


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use framework "CoreImage"
use scripting additions

property refMe : a reference to current application

##偶数値限定
set numPixelsWidth to 720 as integer
set numPixelsHeight to 360 as integer
set numResolution to 144 as integer
#
set numHarfPixelsWidth to (numPixelsWidth / 2) as integer
set numHarfPixelsHeight to (numPixelsHeight / 2) as integer
#ファイル名
set strFileName to (numHarfPixelsWidth & "x" & numHarfPixelsHeight & "@2x.png") as text
#パス
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
set ocidSaveFilePathURL to ocidDesktopDirPathURL's URLByAppendingPathComponent:(strFileName)

#画像を生成
set ocidBitmapFormat to refMe's NSBitmapFormatAlphaFirst
set ocidColorSpaceName to refMe's NSCalibratedRGBColorSpace
set ocidArtBoardRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(720) pixelsHigh:(360) bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:false colorSpaceName:(ocidColorSpaceName) bitmapFormat:(ocidBitmapFormat) bytesPerRow:0 bitsPerPixel:32)
########画像編集開始
refMe's NSGraphicsContext's saveGraphicsState()
set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep))
(refMe's NSGraphicsContext's setCurrentContext:(ocidContext))
#色指定
set ocidSetColor to refMe's NSColor's colorWithSRGBRed:(0.1) green:(0.75) blue:(0.26) alpha:(1.0)
ocidSetColor's |set|()
###画像生成 原点は画像左下
refMe's NSRectFill({origin:{x:0, y:0}, |size|:{width:(numPixelsWidth), height:(numPixelsHeight)}})
####画像作成終了
refMe's NSGraphicsContext's restoreGraphicsState()
########画像編集終わり
########文字入力開始
###フォント初期化
# set appFontManager to refMe's NSFontManager
# set appSharedFontManager to appFontManager's sharedFontManager()
###設定用のレコード
set ocidTextAttr to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
###共通項目
set ocidStyle to refMe's NSParagraphStyle's defaultParagraphStyle
(ocidTextAttr's setObject:(ocidStyle) forKey:(refMe's NSParagraphStyleAttributeName))
#色指定
set ocidTextColor to (refMe's NSColor's colorWithSRGBRed:(0) green:(0) blue:(0) alpha:(1.0))
(ocidTextAttr's setObject:(ocidTextColor) forKey:(refMe's NSForegroundColorAttributeName))
########画像編集開始(テキスト描画)
refMe's NSGraphicsContext's saveGraphicsState()
#画像指定
set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep))
#コンテキスト化
(refMe's NSGraphicsContext's setCurrentContext:(ocidContext))
#印字内容
set ocidText to (refMe's NSString's stringWithString:("Hello AppleScript"))
#フォントとサイズ指定
set ocidFont to (refMe's NSFont's fontWithName:("Helvetica-Bold") |size|:(52))
#フォント情報を付与
(ocidTextAttr's setObject:(ocidFont) forKey:(refMe's NSFontAttributeName))
#カーニング
(ocidTextAttr's setObject:(-1.8) forKey:(refMe's NSKernAttributeName))
#描画位置 左下基準
set ocidTextOrigin to refMe's NSMakePoint((160), (150))
#描画内容の確定↓ NSGraphicsContext'sに渡す
(ocidText's drawAtPoint:(ocidTextOrigin) withAttributes:(ocidTextAttr))
########画像編集終了(テキスト描画)
refMe's NSGraphicsContext's restoreGraphicsState()

#保存用オプション
set ocidPropertyDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
(ocidPropertyDict's setObject:(refMe's NSNumber's numberWithBool:(false)) forKey:(refMe's NSImageInterlaced))
(ocidPropertyDict's setObject:(refMe's NSNumber's numberWithFloat:(1 / 2.2)) forKey:(refMe's NSImageGamma))
set ocidImageType to (refMe's NSBitmapImageFileTypePNG)
#イメージサイズを指定(ここで解像度が144ppiになる)
set ocidPixelsSize to refMe's NSMakeSize(numHarfPixelsWidth, numHarfPixelsHeight)
(ocidArtBoardRep's setSize:(ocidPixelsSize))
#インラインデータに変換して
set ocidImageData to (ocidArtBoardRep's representationUsingType:(ocidImageType) |properties|:(ocidPropertyDict))
#保存
set listDone to (ocidImageData's writeToURL:ocidSaveFilePathURL options:0 |error|:(reference))





|

生成した画像に色を塗る


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use framework "CoreImage"
use scripting additions

property refMe : a reference to current application

##偶数値限定
set numPixelsWidth to 720 as integer
set numPixelsHeight to 360 as integer
set numResolution to 144 as integer
#
set numHarfPixelsWidth to (numPixelsWidth / 2) as integer
set numHarfPixelsHeight to (numPixelsHeight / 2) as integer
#ファイル名
set strFileName to (numHarfPixelsWidth & "x" & numHarfPixelsHeight & "@2x.png") as text
#パス
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
set ocidSaveFilePathURL to ocidDesktopDirPathURL's URLByAppendingPathComponent:(strFileName)

#画像を生成
set ocidBitmapFormat to refMe's NSBitmapFormatAlphaFirst
set ocidColorSpaceName to refMe's NSCalibratedRGBColorSpace
set ocidArtBoardRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(720) pixelsHigh:(360) bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:false colorSpaceName:(ocidColorSpaceName) bitmapFormat:(ocidBitmapFormat) bytesPerRow:0 bitsPerPixel:32)
########画像編集開始
refMe's NSGraphicsContext's saveGraphicsState()
set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep))
(refMe's NSGraphicsContext's setCurrentContext:(ocidContext))
#色指定
set ocidSetColor to refMe's NSColor's colorWithSRGBRed:(0.1) green:(0.75) blue:(0.26) alpha:(1.0)
ocidSetColor's |set|()
###画像生成 原点は画像左下
refMe's NSRectFill({origin:{x:0, y:0}, |size|:{width:(numPixelsWidth), height:(numPixelsHeight)}})
####画像作成終了
refMe's NSGraphicsContext's restoreGraphicsState()
########画像編集終わり
#保存用オプション
set ocidPropertyDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
(ocidPropertyDict's setObject:(refMe's NSNumber's numberWithBool:(false)) forKey:(refMe's NSImageInterlaced))
(ocidPropertyDict's setObject:(refMe's NSNumber's numberWithFloat:(1 / 2.2)) forKey:(refMe's NSImageGamma))
set ocidImageType to (refMe's NSBitmapImageFileTypePNG)
#イメージサイズを指定(ここで解像度が144ppiになる)
set ocidPixelsSize to refMe's NSMakeSize(numHarfPixelsWidth, numHarfPixelsHeight)
(ocidArtBoardRep's setSize:(ocidPixelsSize))
#インラインデータに変換して
set ocidImageData to (ocidArtBoardRep's representationUsingType:(ocidImageType) |properties|:(ocidPropertyDict))
#保存
set listDone to (ocidImageData's writeToURL:ocidSaveFilePathURL options:0 |error|:(reference))





|

複数画像のリサイズ(高さ揃え)saveGraphicsState使用


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application

############################
### 設定項目 出力高さpx
############################
property numNewPixelsHeight : 1080 as integer

############################
### ダイアログ
############################
set appFileManager to refMe's NSFileManager's defaultManager()
###デフォルトロケーション
set ocidUserDocumentPathArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDesktopPathURL to ocidUserDocumentPathArray's firstObject()
set aliasDefaultLocation to (ocidDesktopPathURL's absoluteURL()) as alias
#####ダイアログを前面に
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
####ダイアログを出す
set listUTI to {"public.image"} as list
set listAliasFilePath to (choose file with prompt "ファイルを選んでください" default location (aliasDefaultLocation) of type listUTI with invisibles, showing package contents and multiple selections allowed) as list

############################
####選んだファイルパスの数だけ繰り返し
############################
repeat with itemAliasFilePath in listAliasFilePath
  ##入力パス
  set strFilePath to (POSIX path of itemAliasFilePath) as text
  set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
  set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
  ###########################
  ###【1】元イメージ
  ###読み込むイメージURL
  set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
  ##NSIMAGEに読み込む
  set ocidReadImage to (refMe's NSImage's alloc()'s initWithContentsOfURL:(ocidFilePathURL))
  ##BitMapRepに変換
  set ocidReadImageRepArray to ocidReadImage's representations()
  set ocidReadImageRep to (ocidReadImageRepArray's objectAtIndex:0)
  ##ピクセルサイズ取得
  set numPixelsWidth to ocidReadImageRep's pixelsWide()
  set numPixelsHeight to ocidReadImageRep's pixelsHigh()
  ##まずは72ppiにする
  set ocidPixelsSize to refMe's NSMakeSize(numPixelsWidth, numPixelsHeight)
(ocidReadImageRep's setSize:(ocidPixelsSize))
  ###########################
  ###【2】出力イメージ
  ###サイズ計算
  ### 画像サイズ変更比率=出力ピクセルサイズ縦➗読み込み画像ピクセルサイズ縦
  set numImageVH to numNewPixelsHeight / numPixelsHeight as number
  ###同じ比率で横幅を計算 読み込み画像ピクセルサイズ横×画像サイズ変更比率
  set numNewPixelsWidth to numPixelsWidth * numImageVH as integer
  ###↑で計算した出力用の画像ピクセルサイズ
  set ocidNewImagePixelsSize to refMe's NSMakeSize(numNewPixelsWidth, numNewPixelsHeight)
  ###
  ###Retinaモニター時 ポイントサイズ=pixの半分
  set intResPer to (144 / 72) as number
  set intNewWidth to (numNewPixelsWidth / intResPer) as integer
  set intNewHeight to (numNewPixelsHeight / intResPer) as integer
  set ocidNewImageSize to refMe's NSMakeSize(intNewWidth, intNewHeight)
  set ocidNewRect to refMe's NSMakeRect(0, 0, intNewWidth, intNewHeight)
  
  ###出力用イメージ NSBitmapImageRep
  # samplesPerPixel
  set intSPP to 4 as integer
  # bitsPerSample
  set intBPS to 8 as integer
  # bytesPerRow
  set intBPR to (numNewPixelsWidth * intSPP) as integer
  # bitsPerPixel
  set intBPP to 32 as integer
  # RGB系のカラースペース
  set ocidColorSpaceName to refMe's NSCalibratedRGBColorSpace
  # アルファあり
  set ocidBitmapFormat to refMe's NSAlphaFirstBitmapFormat
  ##出力ピクセルサイズのブランクイメージ
  set ocidArtBoardRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numNewPixelsWidth) pixelsHigh:(numNewPixelsHeight) bitsPerSample:(intBPS) samplesPerPixel:(intSPP) hasAlpha:(true) isPlanar:(false) colorSpaceName:(ocidColorSpaceName) bitmapFormat:(ocidBitmapFormat) bytesPerRow:(intBPR) bitsPerPixel:(intBPP))
  
  ###########################
  ###【3】ArtBord
  ### 初期化 CodeBase
refMe's NSGraphicsContext's saveGraphicsState()
  ###Context
  set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep))
  ###生成された画像でNSGraphicsContext初期化
(refMe's NSGraphicsContext's setCurrentContext:(ocidContext))
  ##色を個別に指定する場合 値は0が暗 1が明
  set ocidSetColor to (refMe's NSColor's colorWithSRGBRed:(1.0) green:(1.0) blue:(1.0) alpha:(0.0))
ocidSetColor's |set|()
  ###画像生成
refMe's NSRectFill({origin:{x:0, y:0}, |size|:{width:(numNewPixelsWidth), height:(numNewPixelsHeight)}})
  ####画像作成終了
refMe's NSGraphicsContext's restoreGraphicsState()
  
  ###########################
  ###【3】合成 リザイスでNSCompositeSourceOver
  ### 初期化
refMe's NSGraphicsContext's saveGraphicsState()
  ###Context
  set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep))
  ###NSGraphicsContext初期化
(refMe's NSGraphicsContext's setCurrentContext:(ocidContext))
  ####NSCompositeSourceOver
  set ocidDrawInRect to {origin:{x:(0), y:(0)}, |size|:{width:(numNewPixelsWidth), Hight:(numNewPixelsHeight)}}
  set ocidFromRect to (refMe's NSZeroRect)
  set ocidOption to (refMe's NSCompositeSourceOver)
  ## set ocidOption to (refMe's NSCompositeCopy)
(ocidReadImageRep's drawInRect:(ocidDrawInRect) fromRect:(ocidFromRect) operation:(ocidOption) fraction:1.0 respectFlipped:true hints:(missing value))
  ####画像作成終了
refMe's NSGraphicsContext's restoreGraphicsState()
  
  ###########################
  ###【5】保存
  ####保存オプション
  set ocidProperty to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
(ocidProperty's setObject:(refMe's NSNumber's numberWithBool:false) forKey:(refMe's NSImageInterlaced))
(ocidProperty's setObject:(refMe's NSNumber's numberWithDouble:(1 / 2.2)) forKey:(refMe's NSImageGamma))
  
  ##保存
  set ocidNSInlineData to (ocidArtBoardRep's representationUsingType:(refMe's NSBitmapImageFileTypePNG) |properties|:(ocidProperty))
  set boolDone to (ocidNSInlineData's writeToURL:(ocidFilePathURL) atomically:true)
  
  set ocidReadImageRep to ""
  set ocidArtBoardRep to ""
end repeat


|

画像生成(背景色指定)


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use framework "CoreImage"
use scripting additions

property refMe : a reference to current application


#####設定項目
set intWidth to 1280 as integer
set intHight to 720 as integer

###################################################
#####ダイアログを前面に
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
#####RGB16bitColorに16bitRGB値を格納
tell application "Finder"
  set the listRGB16bitColor to (choose color default color {65535, 55535, 0, 0}) as list
  log class of listRGB16bitColor
end tell
##########Color Picker Value 16Bit
set numRcolor16Bit to item 1 of listRGB16bitColor as number
set numGcolor16Bit to item 2 of listRGB16bitColor as number
set numBcolor16Bit to item 3 of listRGB16bitColor as number

##########Standard RGB Value 8Bit
set numRcolor8Bit to numRcolor16Bit / 256 div 1 as number
set numGcolor8Bit to numGcolor16Bit / 256 div 1 as number
set numBcolor8Bit to numBcolor16Bit / 256 div 1 as number

##########NSColorValue Float
set numRcolorFloat to numRcolor8Bit / 255 as number
set numGcolorFloat to numGcolor8Bit / 255 as number
set numBcolorFloat to numBcolor8Bit / 255 as number

###################################################
##画像生成開始
set ocidBitmapFormat to refMe's NSBitmapFormatAlphaFirst
set ocidColorSpaceName to refMe's NSCalibratedRGBColorSpace
set ocidNSBitmapImageFileType to refMe's NSBitmapImageFileTypePNG

set ocidBitmapImageRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:intWidth pixelsHigh:intHight bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:false colorSpaceName:(ocidColorSpaceName) bitmapFormat:(ocidBitmapFormat) bytesPerRow:0 bitsPerPixel:32)

###################################################
###初期化
refMe's NSGraphicsContext's saveGraphicsState()
###生成された画像でNSGraphicsContext初期化
(refMe's NSGraphicsContext's setCurrentContext:(refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:ocidBitmapImageRep))
###塗り色
set ocidSetColor to refMe's NSColor's colorWithDeviceRed:numRcolorFloat green:numGcolorFloat blue:numBcolorFloat alpha:1
ocidSetColor's |set|()
##画像にする
refMe's NSRectFill({origin:{x:0, y:0}, |size|:{width:intWidth, height:intHight}})
####画像作成終了
refMe's NSGraphicsContext's restoreGraphicsState()

###################################################
set ocidNSBitmapImageRepPropertyKey to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
####TIFF用の圧縮プロパティ(NSImageCompressionFactor)
ocidNSBitmapImageRepPropertyKey's setObject:0 forKey:(refMe's NSImageCompressionFactor)
####DATAに
set ocidNSInlineData to (ocidBitmapImageRep's representationUsingType:(ocidNSBitmapImageFileType) |properties|:ocidNSBitmapImageRepPropertyKey)

###################################################
###保存

set strFilePath to "~/Desktop/出来上がり画像.png"
set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)


set boolMakeQrCode to (ocidNSInlineData's writeToURL:(ocidFilePathURL) atomically:true)




|

画像にテキストを描画する


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "Appkit"
use scripting additions

property refMe : a reference to current application

set appFileManager to refMe's NSFileManager's defaultManager()
###################################
#####設定項目
###################################
###画像に印字するテキスト
set strInputText to "美しい日本語" as text
###余白
set numPadding to 10 as integer
###フォントサイズ
set numFontSize to 24 as integer
###カーニング値
set numKern to -1.8 as number
### テキスト色の透過
set numAlpha to 0.8 as number

###################################
#####入力ダイアログ
###################################
set ocidUserDesktopPathURLArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidUserDesktopPathURL to ocidUserDesktopPathURLArray's firstObject()
set aliasDefaultLocation to (ocidUserDesktopPathURL's absoluteURL()) as alias
set listUTI to {"public.png", "public.jpeg", "public.tiff"}
set strPromptText to "ファイルを選んでください" as text
set strMesText to "ファイルを選んでください" as text
set listAliasFilePath to (choose file strMesText with prompt strPromptText default location (aliasDefaultLocation) of type listUTI with invisibles and multiple selections allowed without showing package contents) as list

###################################
#####出力先フォルダ
###################################
set aliasFilePath to (item 1 of listAliasFilePath) as alias
set strFilePath to (POSIX path of aliasFilePath) as text
set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidContainerDirPath to (ocidFilePath's stringByDeletingLastPathComponent)
set strContainerDirPath to (ocidContainerDirPath's stringByDeletingLastPathComponent) as text
set aliasDefaultLocation to (POSIX file strContainerDirPath) as alias
set strPromptText to "保存先フォルダを選んでください" as text
set strMesText to "保存先フォルダを選んでください" as text
set aliasDistDirPath to (choose folder strMesText with prompt strPromptText default location aliasDefaultLocation with invisibles without multiple selections allowed and showing package contents) as alias

###################################
#####テキスト印字位置
###################################
set listTextPosition to {"上左", "上中", "上右", "中央", "下左", "下中", "下右"} as list
try
  set listResponse to (choose from list listTextPosition with title "選んでください" with prompt "テキスト位置を選んでください" default items (item 7 of listTextPosition) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) as list
on error
  log "エラーしました"
return "エラーしました"
end try
if (item 1 of listResponse) is false then
return "キャンセルしました"
end if
set strResponse to (item 1 of listResponse)
set recordTextPosition to {|上左|:1, |上中|:2, |上右|:3, |中央|:4, |下左|:5, |下中|:6, |下右|:7} as record
set ocidTextPositionDict to refMe's NSMutableDictionary's dictionaryWithDictionary:recordTextPosition
set numPosition to (ocidTextPositionDict's valueForKey:strResponse) as integer

###################################
##### フォント選択
###################################
set strDefailtsFont to "Osaka-Mono" as text
set appFontManager to refMe's NSFontManager
set appSharedMaanager to appFontManager's sharedFontManager()
set ocidFontList to appSharedMaanager's availableFonts()
set numDefaultsFont to (ocidFontList's indexOfObject:(strDefailtsFont)) as integer
set listFontList to ocidFontList as list
try
  set listResponse to (choose from list listFontList with title "選んでください" with prompt "フォントを選んでください" default items (item numDefaultsFont of listFontList) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) as list
on error
  log "エラーしました"
return "エラーしました"
end try
if listResponse is false then
return "キャンセルしました"
end if
set strFontName to (item 1 of listResponse) as text
###################################
#####テキストの色
###################################
#####RGB16bitColorに16bitRGB値を格納
tell application "Finder"
  set the listRGB16bitColor to (choose color default color {65535, 55535, 0, 0}) as list
  log class of listRGB16bitColor
end tell
##########Color Picker Value 16Bit
set numRcolor16Bit to item 1 of listRGB16bitColor as number
set numGcolor16Bit to item 2 of listRGB16bitColor as number
set numBcolor16Bit to item 3 of listRGB16bitColor as number
##########Standard RGB Value 8Bit
set numRcolor8Bit to numRcolor16Bit / 256 div 1 as number
set numGcolor8Bit to numGcolor16Bit / 256 div 1 as number
set numBcolor8Bit to numBcolor16Bit / 256 div 1 as number
##########NSColorValue Float
set numRcolorFloat to numRcolor8Bit / 255 as number
set numGcolorFloat to numGcolor8Bit / 255 as number
set numBcolorFloat to numBcolor8Bit / 255 as number

###################################
#####テキスト印字位置
###################################
repeat with itemAliasFilePath in listAliasFilePath
  set aliasFilePath to itemAliasFilePath as alias
  set strFilePath to (POSIX path of aliasFilePath) as text
  set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
  set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
  set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
  ###
  set ocidFileName to ocidFilePathURL's lastPathComponent()
  set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension
  set ocidExtensionName to ocidFilePathURL's pathExtension()
  
  ###################################
  #####画像読み込み
  ###################################
  set ocidImageRep to (refMe's NSBitmapImageRep's imageRepWithContentsOfURL:(ocidFilePathURL))
  ###サイズ取得
  set recordImageRepSize to ocidImageRep's |size|()
  set numPointWidth to width of recordImageRepSize
  set numPointHeight to height of recordImageRepSize
  ##ピクセルサイズ
  set numPixelsWide to ocidImageRep's pixelsWide() as integer
  set numPixelsHigh to ocidImageRep's pixelsHigh() as integer
  
  ###サイズをピクセルサイズに変更しておく (解像度を72にしておく)
  set recordTempImageSize to {width:numPixelsWide, height:numPixelsHigh} as record
(ocidImageRep's setSize:recordTempImageSize)
  
  ###################################
  #####テキスト生成
  ###################################
  ###設定用のレコード
  set ocidTextAttr to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
  ###################
  ##描画する文字列
  set ocidText to (refMe's NSString's stringWithString:(strInputText))
  ###################
  ###色指定
  ## set ocidTextColor to refMe's NSColor's blackColor
  ###RGBA 透過指定
  set ocidTextColor to (refMe's NSColor's colorWithSRGBRed:(numRcolorFloat) green:(numGcolorFloat) blue:(numBcolorFloat) alpha:(numAlpha))
(ocidTextAttr's setObject:(ocidTextColor) forKey:(refMe's NSForegroundColorAttributeName))
  ###################
  ##システムフォント
  # set ocidFont to (refMe's NSFont's systemFontOfSize:numFontSize)
  ###PS名指定でもいける?
  set ocidFont to (refMe's NSFont's fontWithName:(strFontName) |size|:numFontSize)
(ocidTextAttr's setObject:(ocidFont) forKey:(refMe's NSFontAttributeName))
  ###################
  ##行間
  set ocidStyle to refMe's NSParagraphStyle's defaultParagraphStyle
(ocidTextAttr's setObject:(ocidStyle) forKey:(refMe's NSParagraphStyleAttributeName))
  ###################
  ##カーニング
  # set ocidKern to refMe's NSTextScalingStandard ##デフォルト
  # set ocidKern to refMe's NSTextScalingiOS ###iOS最適化
  set ocidKern to numKern
(ocidTextAttr's setObject:(ocidKern) forKey:(refMe's NSKernAttributeName))
  ###################
  ##ドロップシャドウ
  set ocidShadow to refMe's NSShadow's alloc()'s init()
  ###テキスト色と同色にする場合
  set ocidShadowColor to (ocidTextColor's colorWithAlphaComponent:0.8)
  ###色指定する場合
  set ocidShadowColor to (refMe's NSColor's colorWithDeviceRed:0 green:0 blue:0 alpha:0.5)
(ocidShadow's setShadowColor:(ocidShadowColor))
(ocidShadow's setShadowOffset:(refMe's NSMakeSize(1, -1)))
(ocidShadow's setShadowBlurRadius:4)
(ocidTextAttr's setObject:(ocidShadow) forKey:(refMe's NSShadowAttributeName))
  ###################
  ####描画されるテキストボックスのサイズ
  set ocidTextSize to (ocidText's sizeWithAttributes:(ocidTextAttr))
  set ocidTextWide to ocidTextSize's width as integer
  set ocidTextHigh to ocidTextSize's height as integer
  ###################################
  #####描画位置
  ###################################
  ###ここは、お好みで色々できるか?
  if numPosition = 1 then ###上左
    set ocidTextOrigin to refMe's NSMakePoint(numPadding, (numPixelsHigh - ocidTextHigh) - numPadding)
  else if numPosition = 2 then ###上中央
    set ocidTextOrigin to refMe's NSMakePoint((numPixelsWide - ocidTextWide) / 2, (numPixelsHigh - ocidTextHigh) - numPadding)
  else if numPosition = 3 then ###上右
    set ocidTextOrigin to refMe's NSMakePoint((numPixelsWide - ocidTextWide - numPadding), (numPixelsHigh - ocidTextHigh) - numPadding)
  else if numPosition = 4 then ###真ん中
    set ocidTextOrigin to refMe's NSMakePoint((numPixelsWide - ocidTextWide) / 2, (numPixelsHigh - ocidTextHigh) / 2)
  else if numPosition = 5 then ###左下
    set ocidTextOrigin to refMe's NSMakePoint(numPadding, numPadding)
  else if numPosition = 6 then ###下中央
    set ocidTextOrigin to refMe's NSMakePoint((numPixelsWide - ocidTextWide) / 2, numPadding)
  else if numPosition = 7 then ###右下
    set ocidTextOrigin to refMe's NSMakePoint((numPixelsWide - ocidTextWide - numPadding), numPadding)
  end if
  ###################################
  #####NSGraphicsContext用のアートボード
  ###################################
  ##透過指定
  set ocidSetColor to refMe's NSColor's clearColor
  set ocidFormat to refMe's NSBitmapFormatAlphaFirst
  set ocidColorSpace to refMe's NSCalibratedRGBColorSpace
  ####生成本処理
  set ocidArtBoardRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numPixelsWide) pixelsHigh:(numPixelsHigh) bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:false colorSpaceName:(ocidColorSpace) bitmapFormat:(ocidFormat) bytesPerRow:0 bitsPerPixel:32)
  ####Rectを生成(画像ピクセルサイズ)
  set ocidArtBordRect to refMe's NSMakeRect(0, 0, numPixelsWide, numPixelsHigh)
  
  ###################################
  #####テキスト描画 本処理
  ###################################
  ####NSGraphicsContextは透明アートボード
  set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep))
  ###初期化
refMe's NSGraphicsContext's saveGraphicsState()
  ###透明アートボードでNSGraphicsContext初期化
(refMe's NSGraphicsContext's setCurrentContext:(ocidContext))
  ####読み込んだ画像をレンダリングする
(ocidImageRep's drawInRect:(ocidArtBordRect) fromRect:(ocidArtBordRect) operation:(refMe's NSCompositeSourceOver) fraction:1.0 respectFlipped:true hints:(missing value))
  ###テキスト描画する
(ocidText's drawAtPoint:(ocidTextOrigin) withAttributes:(ocidTextAttr))
  ####画像作成終了
refMe's NSGraphicsContext's restoreGraphicsState()
  ###解像度を戻す
  set recordPointImageSize to {width:numPointWidth, height:numPointHeight} as record
(ocidArtBoardRep's setSize:recordPointImageSize)
  ###################################
  #####保存オプション PNG
  ###################################
  set ocidFileType to refMe's NSBitmapImageFileTypePNG
  set ocidProperties to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
  ###インターレース
(ocidProperties's setObject:false forKey:(refMe's NSImageInterlaced))
  ###ガンマ
(ocidProperties's setObject:(1 / 2.2 as number) forKey:(refMe's NSImageGamma))
  ####NSImageColorSyncProfileData カラースペース
  set ocidColorSpace to refMe's NSColorSpace's displayP3ColorSpace()
  set ocidColorSpaceData to ocidColorSpace's colorSyncProfile()
  ###プロファイルを付ける
(ocidProperties's setObject:(ocidColorSpaceData) forKey:(refMe's NSImageColorSyncProfileData))
  ####出力用データに変換
  set ocidNSInlineData to (ocidArtBoardRep's representationUsingType:(ocidFileType) |properties|:(ocidProperties))
  
  ###################################
  #####保存ファイル パス処理
  ###################################
  set strDistDirPath to (POSIX path of aliasDistDirPath) as text
  set ocidDistDirPathStr to (refMe's NSString's stringWithString:(strDistDirPath))
  set ocidDistDirPath to ocidDistDirPathStr's stringByStandardizingPath()
  set ocidDistDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidDistDirPath) isDirectory:false)
  set ocidSaveFileName to (ocidBaseFileName's stringByAppendingPathExtension:"png")
  set ocidSaveFilePathURL to (ocidDistDirPathURL's URLByAppendingPathComponent:(ocidSaveFileName))
  
  ###################################
  #####保存
  ###################################
  set boolDone to (ocidNSInlineData's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error|:(reference))
  
end repeat

tell application "Finder"
  open aliasDistDirPath
end tell

return "処理終了"



|

[NSGraphicsContext]サムネイル画像を作成する(途中)

25ファイルの画像から、いわゆるビデオ用のサムネイル画像を作成します
いわゆるこれ
Photo_20230429095401



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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application

###########################################
###フォルダ選択ダイアログ
###########################################

set aliasDefaultLocation to (path to desktop from user domain) as alias
set strPromptText to "フォルダをえらんでください"
set strMesText to "フォルダをえらんでください"
try
  set listFolderPath to (choose folder strMesText with prompt strPromptText default location aliasDefaultLocation without multiple selections allowed, invisibles and showing package contents) as list
on error
  log "エラーしました"
  return
end try

###########################################
###最初にファイルパスを名前順に並び替える
###########################################
###enumeratorAtURL用のBoolean
set ocidFalse to (refMe's NSNumber's numberWithBool:false)
set ocidTrue to (refMe's NSNumber's numberWithBool:true)
###ファイルマネジャー初期化
set appFileManager to refMe's NSFileManager's defaultManager()
####フォルダの数だけ繰り返し
repeat with itemFolderPath in listFolderPath
  ######パス
  set aliasDirPath to itemFolderPath as alias
  set strDirPath to POSIX path of aliasDirPath as text
  set ocidDirPath to (refMe's NSString's stringWithString:strDirPath)
  set ocidDirPath to ocidDirPath's stringByStandardizingPath
  set ocidDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidDirPath isDirectory:true)
  set strDirName to ocidDirPathURL's lastPathComponent() as text
  
  
  ##プロパティ
  set ocidPropertieKey to {refMe's NSURLPathKey, refMe's NSURLIsRegularFileKey}
  ##オプション
  set ocidOption to refMe's NSDirectoryEnumerationSkipsHiddenFiles
  ####ディレクトリのコンテツを収集
  set ocidEmuDict to (appFileManager's enumeratorAtURL:ocidDirPathURL includingPropertiesForKeys:ocidPropertieKey options:ocidOption errorHandler:(reference))
  ###戻り値
  set ocidEmuFileURLArray to ocidEmuDict's allObjects()
  ##############################################
  ##URLリストなのでPathArrayにしてから
  ##############################################
  ###ファイルURLのみを格納するリスト
  set ocidFilePathArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:0)
  
  repeat with itemEmuFileURL in ocidEmuFileURLArray
    ####拡張子取って
    set ocidExtension to itemEmuFileURL's pathExtension()
    if (ocidExtension as text) is "url" then
      set listResult to (appFileManager's trashItemAtURL:itemEmuFileURL resultingItemURL:(missing value) |error|:(reference))
    else if (ocidExtension as text) is "db" then
      set listResult to (appFileManager's trashItemAtURL:itemEmuFileURL resultingItemURL:(missing value) |error|:(reference))
    else
      ####URLforKeyで取り出し
      set listResult to (itemEmuFileURL's getResourceValue:(reference) forKey:(refMe's NSURLIsRegularFileKey) |error|:(reference))
      ###リストからNSURLIsRegularFileKeyBOOLを取り出し
      set boolIsRegularFileKey to item 2 of listResult
      ####ファイルのみを(ディレクトリやリンボリックリンクは含まない)
      if boolIsRegularFileKey is ocidTrue then
        ###パスにして
        set ocidFilePath to itemEmuFileURL's |path|()
        ####リストにする
        (ocidFilePathArray's addObject:ocidFilePath)
      end if
    end if
  end repeat
  ##############################
  ####並び替え
  ##############################
  set ocidSortDescriptor to (refMe's NSSortDescriptor's sortDescriptorWithKey:"self" ascending:(true) selector:"localizedStandardCompare:")
  (ocidFilePathArray's sortUsingDescriptors:{ocidSortDescriptor})
end repeat
###########################################
###出力画像サイズ計算用
###########################################
###生成する画像の幅
set numWidth to 2560 as integer
###1コマの画像の幅
set numScaleMaxWidth to 510 as integer
###ファイルリストの最初の1つを取って
set ocidChkFilePath to ocidFilePathArray's objectAtIndex:0
set ocidChkFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidChkFilePath isDirectory:false)
set ocidChkImage to refMe's NSImage's alloc()'s initWithContentsOfURL:ocidChkFilePathURL
###描画して
set ocidImageRepArray to ocidChkImage's representations()
set ocidImageRep to (ocidImageRepArray's objectAtIndex:0)
###読み込む画像の幅と高さを取得
set numpixelsWidth to ocidImageRep's pixelsWide() as integer
set numpixelsHeight to ocidImageRep's pixelsHigh() as integer
###縮小率
set numImagePer to (numScaleMaxWidth / numpixelsWidth) as number
###生成する画像の高さ(1コマの高さX5列)を計算
set numImageFullHeight to (numpixelsHeight * numImagePer) * 5 as integer

###########################################
###設定項目
###########################################
###コマとコマの間隔用に10pix追加
set numHight to numImageFullHeight + 10 as integer
####リサイズする指数
set numScaleMaxHight to (numpixelsHeight / numImagePer) as integer
####画像の間隔
set numWsp to (numWidth - (numScaleMaxWidth * 5)) / 5 as integer
set numHsp to (numHight - (numScaleMaxHight * 5)) / 5 as integer

###########################################
###背景となる画像を作る
###########################################
set ocidSetColor to refMe's NSColor's colorWithDeviceRed:0 green:0 blue:0 alpha:1
set ocidBitmapFormat to refMe's NSBitmapFormatAlphaFirst
set ocidColorSpaceName to refMe's NSCalibratedRGBColorSpace
set ocidNSBitmapImageFileType to refMe's NSBitmapImageFileTypeTIFF
set ocidBitmapImageRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:numWidth pixelsHigh:numHight bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:false colorSpaceName:(ocidColorSpaceName) bitmapFormat:(ocidBitmapFormat) bytesPerRow:0 bitsPerPixel:32)
###初期化
refMe's NSGraphicsContext's saveGraphicsState()
###生成された画像でNSGraphicsContext初期化
(refMe's NSGraphicsContext's setCurrentContext:(refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:ocidBitmapImageRep))
###塗り色
ocidSetColor's |set|()
##画像にする
refMe's NSRectFill({origin:{x:0, y:0}, |size|:{width:numWidth, height:numHight}})
####画像作成終了
refMe's NSGraphicsContext's restoreGraphicsState()
####################################
########画像合成の始まり
refMe's NSGraphicsContext's saveGraphicsState()
(refMe's NSGraphicsContext's setCurrentContext:(refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:ocidBitmapImageRep))
###背景画像を描画
refMe's NSRectFill({origin:{x:0, y:0}, |size|:{width:numWidth, height:numHight}})

###########################################
###ファイルパスの数だけ繰り返す
###########################################
set numCntLine to 0 as integer
###コマの位置初期値
set numOverImageX to numWsp as integer
set numOverImageY to numHight - numScaleMaxHight - numHsp as integer
####ファイルの数だけ繰り返し
repeat with itemFilePathArray in ocidFilePathArray
  ###################################################
  ##コマ用の画像を縮小する
  ####リストがパスなのでURLにしてから
  set ocidImagePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:itemFilePathArray isDirectory:false)
  ####ファイルをCIイメージで読み込み
  set ocidCiImageInput to (refMe's CIImage's imageWithContentsOfURL:ocidImagePathURL options:(missing value))
  ###ファイルのサイズを調べる
  set ocidCGRect to ocidCiImageInput's extent()
  ####幅の値を取得
  set numImageWidth to refMe's CGRectGetWidth(ocidCGRect) as number
  ####リサイズする指数
  set numResizeScale to (numScaleMaxWidth / numImageWidth)
  ####Transform指定
  set recordCiImageData to (ocidCiImageInput's imageTransformForOrientation:(refMe's kCGImagePropertyOrientationUp))
  ####Transform適応
  set ocidCiImageData to (ocidCiImageInput's imageByApplyingTransform:recordCiImageData)
  ####Parameters
  set listParameters to {inputImage:ocidCiImageData, inputScale:numResizeScale, inputAspectRatio:1.0}
  ####リサイズする指数でリサイズ
  set ocidCiFilterData to (refMe's CIFilter's filterWithName:"CILanczosScaleTransform" withInputParameters:listParameters)
  #####リサイズ済みイメージ
  set ocidResizedCiImage to ocidCiFilterData's outputImage()
  #####NSBitmapImageRepに変換して
  set ocidResizedImagePep to (refMe's NSBitmapImageRep's alloc()'s initWithCIImage:ocidResizedCiImage)
  ###################################################
  ####背景用画像に配置する位置
  set ocidDrawRect to {origin:{x:numOverImageX, y:numOverImageY}, |size|:{width:numWidth, Hight:numHight}}
  ####背景画像の位置
  set ocidFromRect to {origin:{x:0, y:0}, |size|:{width:numWidth, Hight:numHight}}
  ####コマ画像を背景画像上に配置する
  (ocidResizedImagePep's drawInRect:ocidDrawRect fromRect:ocidFromRect operation:(refMe's NSCompositingOperationSourceOver) fraction:1.0 respectFlipped:true hints:(missing value))
  ####行列カウントアップ
  set numCntLine to numCntLine + 1 as integer
  ###5の倍数になったら
  if (numCntLine mod 5) = 0 then
    set numOverImageX to 1 as integer
    set numOverImageY to numOverImageY - numScaleMaxHight - numHsp as integer
  else
    ###5の倍数以外の場合
    set numOverImageX to numOverImageX + numScaleMaxWidth + numWsp as integer
  end if
end repeat

####################################
########画像合成の終わり
refMe's NSGraphicsContext's restoreGraphicsState()
###################################################
set ocidNSBitmapImageRepPropertyKey to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
####TIFF用の圧縮プロパティ(NSImageCompressionFactor)
ocidNSBitmapImageRepPropertyKey's setObject:0 forKey:(refMe's NSImageCompressionFactor)
####インラインデータに変換
set ocidNSInlineData to (ocidBitmapImageRep's representationUsingType:(ocidNSBitmapImageFileType) |properties|:ocidNSBitmapImageRepPropertyKey)
####保存先パス
set strPosixPath to "~/Desktop/" & strDirName & "x25サムネイル.tif"
set ocidPosixPath to refMe's NSString's stringWithString:strPosixPath
set ocidPosixFullPath to ocidPosixPath's stringByStandardizingPath
###保存
set boolMakeQrCode to (ocidNSInlineData's writeToFile:ocidPosixFullPath atomically:true)



|

画像ファイルをファイル名順に見開き画像に変更

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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application
set appFileManager to refMe's NSFileManager's defaultManager()


###ダイアログを前面に出す
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder"
    activate
  end tell
else
  tell current application
    activate
  end tell
end if
###################################
#####ファイル選択ダイアログ
###################################
###ダイアログのデフォルト
set ocidUserDesktopPath to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set aliasDefaultLocation to ocidUserDesktopPath as alias
tell application "Finder"
  ##  set aliasDefaultLocation to container of (path to me) as alias
end tell
set listChooseFileUTI to {"public.image"}
set strPromptText to "画像ファイルを選んでください" as text
set listAliasFilePath to (choose file with prompt strPromptText default location (aliasDefaultLocation) of type listChooseFileUTI with multiple selections allowed without invisibles and showing package contents) as list

#############################
##まずはリストをファイル名順にソート
#############################
###初期化
set ocidFilePathArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
repeat with itemAliasFilePath in listAliasFilePath
  set strFilePath to POSIX path of itemAliasFilePath as text
  (ocidFilePathArray's addObject:strFilePath)
end repeat
###ファイルリストをソート
set ocidSortedArray to (ocidFilePathArray's sortedArrayUsingSelector:"compare:")
###################################
#####保存先 ダイアログ
###################################
try
  set aliasResponse to (choose folder "画像を書き出すフォルダを選択してください" with prompt "画像を書き出すフォルダを選択してください" default location aliasDefaultLocation without multiple selections allowed, invisibles and showing package contents)
on error
  log "エラーしました"
  return
end try
set strFilePath to POSIX path of aliasResponse as text
set ocidFilePathStr to refMe's NSString's stringWithString:strFilePath
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidSaveDirPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath isDirectory:false
#############################################
####本処理
#############################################
set numCntAllFileNo to (ocidFilePathArray count) as integer
set numFileCnt to 0 as integer
set numSaveFileNo to 1 as integer
repeat (numCntAllFileNo / 2 as integer) times
  ###########右ファイルから
  set strFilePath to ocidSortedArray's objectAtIndex:numFileCnt
  set ocidFilePathStr to refMe's NSString's stringWithString:strFilePath
  set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
  set ocidRFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath isDirectory:false
  ###########次のファイルへのカウントアップ
  set numFileCnt to numFileCnt + 1 as integer
  ###########左ファイル
  set strFilePath to ocidSortedArray's objectAtIndex:numFileCnt
  set ocidFilePathStr to refMe's NSString's stringWithString:strFilePath
  set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
  set ocidLFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath isDirectory:false
  ###########次のファイルへのカウントアップ
  set numFileCnt to numFileCnt + 1 as integer
  ###########保存ファイル
  set strZeroAdd to ("000" & numSaveFileNo) as text
  set strSaveFileNo to text -3 through -1 of strZeroAdd as text
  ###出力用のファイル名
  set strSaveFileName to strSaveFileNo & ".jpg"
  set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:strSaveFileName
  ###カウントアップ
  set numSaveFileNo to numSaveFileNo + 1 as integer
  ##########################################
  ####入力イメージ読み込み
  set ocidImageL to refMe's NSImage's alloc()'s initWithContentsOfURL:ocidLFilePathURL
  set ocidImageR to refMe's NSImage's alloc()'s initWithContentsOfURL:ocidRFilePathURL
  ###中間ファイルとしてのTIFFに変換
  set ocidDistImageL to ocidImageL's TIFFRepresentation()
  set ocidDistImageR to ocidImageR's TIFFRepresentation()
  ####元データとしてのBmpImageRep
  set ocidBmpImageRepL to (refMe's NSBitmapImageRep's imageRepWithData:ocidDistImageL)
  set ocidBmpImageRepR to (refMe's NSBitmapImageRep's imageRepWithData:ocidDistImageR)
  ###解像度を72に変更
  set numpixelsWidthR to ocidBmpImageRepR's pixelsWide()
  set numpixelsHeightR to ocidBmpImageRepR's pixelsHigh()
  (ocidBmpImageRepR's setSize:{width:numpixelsWidthR, height:numpixelsHeightR})
  ###解像度を72に変更
  set numpixelsWidthL to ocidBmpImageRepL's pixelsWide()
  set numpixelsHeightL to ocidBmpImageRepL's pixelsHigh()
  (ocidBmpImageRepL's setSize:{width:numpixelsWidthL, height:numpixelsHeightL})
  ###横幅2倍 出力用のイメージサイズ
  set numNewW to numpixelsWidthR + numpixelsWidthL as integer
  set numNewH to numpixelsHeightR as integer
  ##########################################
  ####出力用のイメージ
  set ocidSaveSize to refMe's NSMakeSize(numNewW, numNewH)
  set ocidBmpImageRepSave to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:numNewW pixelsHigh:numNewH bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:false colorSpaceName:(refMe's NSCalibratedRGBColorSpace) bitmapFormat:(refMe's NSAlphaFirstBitmapFormat) bytesPerRow:0 bitsPerPixel:32)
  ##########################################
  ####
  ###初期化
  refMe's NSGraphicsContext's saveGraphicsState()
  (refMe's NSGraphicsContext's setCurrentContext:(refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:ocidBmpImageRepSave))
  refMe's NSColor's whiteColor()'s |set|()
  refMe's NSRectFill({origin:{x:0, y:0}, |size|:{width:numNewW, height:numNewH}})
  #####左側
  ocidBmpImageRepL's drawInRect:{origin:{x:0, y:0}, |size|:{width:numNewW, Hight:numNewH}} fromRect:{origin:{x:0, y:0}, |size|:{width:numNewW, height:numNewH}} operation:(refMe's NSCompositeSourceOver) fraction:1.0 respectFlipped:true hints:(missing value)
  ####右側
  ocidBmpImageRepR's drawInRect:{origin:{x:numpixelsWidthR, y:0}, |size|:{width:numNewW, Hight:numNewH}} fromRect:{origin:{x:0, y:0}, |size|:{width:numNewW, height:numNewH}} operation:(refMe's NSCompositeSourceOver) fraction:1.0 respectFlipped:true hints:(missing value)
  ####画像作成終了
  refMe's NSGraphicsContext's restoreGraphicsState()
  ###################################
  #####保存
  ###################################
  ####JPEG用の圧縮プロパティ
  set ocidJpegOption to refMe's NSDictionary's dictionaryWithObject:1 forKey:(refMe's NSImageCompressionFactor)
  #####出力イメージへ変換
  set ocidNSInlineData to (ocidBmpImageRepSave's representationUsingType:(refMe's NSBitmapImageFileTypePNG) |properties|:ocidJpegOption)
  ####保存
  set boolResults to (ocidNSInlineData's writeToURL:ocidSaveFilePathURL options:(refMe's NSDataWritingAtomic) |error|:(reference))
  
  ###各種解放
  set ocidNSInlineData to ""
  set ocidBmpImageRepSave to ""
  set ocidBmpImageRepL to ""
  set ocidBmpImageRepR to ""
end repeat



|

その他のカテゴリー

Accessibility Acrobat Acrobat 2020 Acrobat AddOn Acrobat Annotation Acrobat ARMDC Acrobat AV2 Acrobat BookMark Acrobat Classic Acrobat DC Acrobat Dialog Acrobat Distiller Acrobat Form Acrobat JS Acrobat Manifest Acrobat Menu Acrobat Open Acrobat Plugin Acrobat Preferences Acrobat Preflight Acrobat python Acrobat Reader Acrobat SCA Acrobat SCA Updater Acrobat Sequ Acrobat Sign Acrobat Stamps Acrobat Watermark Acrobat Windows Acrobat Windows Reader Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Mobileconfig Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin Support Admin System Information Admin Tools Admin Users Admin Volumes Adobe Adobe FDKO Adobe RemoteUpdateManager AppKit Apple AppleScript AppleScript do shell script AppleScript List AppleScript ObjC AppleScript Osax AppleScript PDF AppleScript Pictures AppleScript record AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript System Events AppleScript System Events Plist AppleScript Video Applications AppStore Archive Attributes Automator BackUp Barcode Barcode QR Barcode QR Decode Bash Basic Basic Path Bluetooth BOX Browser Calendar CD/DVD Choose Chrome CIImage CityCode CloudStorage Color com.apple.LaunchServices.OpenWith Console Contacts CotEditor CURL current application Date&Time delimiters Desktop Device Diff Disk Dock DropBox Droplet eMail Encode % Encode Decode Encode UTF8 Error EXIFData ffmpeg File Finder Firefox Folder FolderAction Fonts GIF github Guide HTML HTML Entity Icon Illustrator Image Events Image2PDF ImageOptim iPhone iWork Javascript Jedit Json Label Leading Zero List locationd LRC lsappinfo LSSharedFileList m3u8 Mail MakePDF Map Math Media Media AVAsset Media AVconvert Media AVFoundation Media AVURLAsset Media Movie Media Music Memo Messages Microsoft Microsoft Edge Microsoft Excel Mouse Music NetWork Notes NSArray NSArray Sort NSBezierPath NSBitmapImageRep NSBundle NSCFBoolean NSCharacterSet NSColor NSColorList NSData NSDecimalNumber NSDictionary NSError NSEvent NSFileAttributes NSFileManager NSFileManager enumeratorAtURL NSFont NSFontManager NSGraphicsContext NSImage NSIndex NSKeyedArchiver NSKeyedUnarchiver NSLocale NSMutableArray NSMutableDictionary NSMutableString NSNotFound NSNumber NSOpenPanel NSPasteboard NSpoint NSPredicate NSPrintOperation NSRange NSRect NSRegularExpression NSRunningApplication NSScreen NSSize NSString NSString stringByApplyingTransform NSStringCompareOptions NSTask NSTimeZone NSURL NSURL File NSURLBookmark NSURLComponents NSURLResourceKey NSURLSession NSUserDefaults NSUUID NSView NSWorkspace Numbers OAuth OneDrive PDF PDFAnnotation PDFAnnotationWidget PDFContext PDFDisplayBox PDFDocumentPermissions PDFImageRep PDFKit PDFnUP PDFOutline perl Photoshop PlistBuddy pluginkit postalcode PostScript prefPane Preview Python QuickLook QuickTime ReadMe Regular Expression Reminders ReName Repeat RTF Safari SaveFile ScreenCapture ScreenSaver SF Symbols character id SF Symbols Entity sips Skype Slack Sound Spotlight sqlite SRT StandardAdditions Swift System Settings TCC TemporaryItems Terminal Text Text CSV Text MD Text TSV TextEdit Tools Translate Trash Twitter Typography UI Unit Conversion UTType valueForKeyPath Video VisionKit Visual Studio Code Wacom webarchive webp Wifi Windows XML XML EPUB XML OPML XML Plist XML RSS XML savedSearch XML SVG XML TTML XML webloc XML XMP YouTube zoom