« 解像度変更 修正 | トップページ | [Automator]script2Applescript スクリプトをテキスト形式にする(修正 パスのエスケープ処理を追加) »

画像ファイルを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





|

« 解像度変更 修正 | トップページ | [Automator]script2Applescript スクリプトをテキスト形式にする(修正 パスのエスケープ処理を追加) »

AppleScript NSBitmapImageRep」カテゴリの記事