–> MagicKit.framework

— Created 2017-11-21 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "QuartzCore"
use framework "AppKit"
use framework "MagicKit" –

property |NSURL| : a reference to current application’s |NSURL|
property NSUUID : a reference to current application’s NSUUID
property NSArray : a reference to current application’s NSArray
property NSString : a reference to current application’s NSString
property NSImage : a reference to current application’s NSImage
property GEMagicKit : a reference to current application’s GEMagicKit
property NSPNGFileType : a reference to current application’s NSPNGFileType
property NSMutableArray : a reference to current application’s NSMutableArray
property NSBitmapImageRep : a reference to current application’s NSBitmapImageRep

property xGap : 10 –連結時の画像間のアキ(横方向)

tell application "Finder"
  set aSel to selection as alias list
if aSel = {} or aSel = "" then return
end tell

set imgList to NSMutableArray’s new()
repeat with i in aSel
  set aPath to POSIX path of i
set aRes to (GEMagicKit’s magicForFileAtPath:aPath)
set utiList to (aRes’s uniformTypeHierarchy()) as list
if "public.image" is in utiList then
    set aNSImage to (NSImage’s alloc()’s initWithContentsOfFile:aPath)
imgList’s addObject:aNSImage)
  end if
end repeat

set sizeList to (imgList’s valueForKeyPath:"size") as list –NSSize to list of record conversion
set maxHeight to ((NSArray’s arrayWithArray:sizeList)’s valueForKeyPath:"@max.height") as real
set totalWidth to ((NSArray’s arrayWithArray:sizeList)’s valueForKeyPath:"@sum.width") as real
set totalCount to ((NSArray’s arrayWithArray:sizeList)’s valueForKeyPath:"@count") as integer

set tSize to current application’s NSMakeSize((totalWidth + (xGap * totalCount)), maxHeight)
set newImage to NSImage’s alloc()’s initWithSize:tSize

set xOrig to 0
repeat with i in (imgList as list)
  set j to contents of i
set curSize to j’s |size|()
set aRect to {xOrig, (maxHeight – (curSize’s height())), (curSize’s width()), (curSize’s height())}
set newImage to composeImage(newImage, j, aRect) of me
set xOrig to (curSize’s width()) + xGap
end repeat

set aDesktopPath to current application’s NSHomeDirectory()’s stringByAppendingString:"/Desktop/"
set savePath to aDesktopPath’s stringByAppendingString:((NSUUID’s UUID()’s UUIDString())’s stringByAppendingString:".png")
set fRes to saveNSImageAtPathAsPNG(newImage, savePath) of me

on composeImage(backImage, composeImage, aTargerRect)
  set newImage to NSImage’s alloc()’s initWithSize:(backImage’s |size|())
copy aTargerRect to {x1, y1, x2, y2}
set bRect to current application’s NSMakeRect(x1, y1, x2, y2)
newImage’s lockFocus()
set newImageRect to current application’s CGRectZero
set newImageRect’s |size| to (newImage’s |size|)
backImage’s drawInRect:newImageRect
composeImage’s drawInRect:bRect
newImage’s unlockFocus()
return newImage
end composeImage

on saveNSImageAtPathAsPNG(anImage, outPath)
  set imageRep to anImage’s TIFFRepresentation()
set aRawimg to NSBitmapImageRep’s imageRepWithData:imageRep
set pathString to NSString’s stringWithString:outPath
set newPath to pathString’s stringByExpandingTildeInPath()
set myNewImageData to (aRawimg’s representationUsingType:(NSPNGFileType) |properties|:(missing value))
set aRes to (myNewImageData’s writeToFile:newPath atomically:true) as boolean
return aRes –成功ならtrue、失敗ならfalseが返る
end saveNSImageAtPathAsPNG

★Click Here to Open This Script 

Please follow and like us:


メールアドレスが公開されることはありません。 * が付いている欄は必須項目です