« [NSSize] NSMakeSize | トップページ | [com.apple.screencapture]スクリーンキャプチャの保存先 »

[NSImage]画像の解像度変更

#!/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
property refNSString : a reference to refMe's NSString
property refNSURL : a reference to refMe's NSURL

property boolFileChk : true
property boolFolderChk : false
property listUTI : {"public.png", "public.jpeg"}
####指定解像度ppi
property numResolution : 72

on run
    set objFileManager to refMe's NSFileManager's defaultManager()
    set ocidGetUrlArray to (objFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
    set ocidDesktopDirPathURL to ocidGetUrlArray's objectAtIndex:0
    set aliasDesktopDirPath to ocidDesktopDirPathURL as alias
    ####ダイアログ
    set listFilePathAlias to (choose file with prompt "ファイルを選んでください" default location aliasDesktopDirPath of type listUTI with multiple selections allowed and showing package contents without invisibles) as list
    open listFilePathAlias
end run


on open listFilePathAlias
    ##ファイルマネージャー起動
    set objFileManager to refMe's NSFileManager's defaultManager()
    ##繰り返しのはじまり
    repeat with itemFilePathAlias in listFilePathAlias
        
        
        
        set strFilePath to POSIX path of itemFilePathAlias as text
        set ocidFilePath to (refNSString's stringWithString:strFilePath)
        set ocidFilePathURL to (refNSURL's alloc()'s initFileURLWithPath:ocidFilePath)
        ####拡張子を取得
        set ocidFileExtension to ocidFilePathURL's pathExtension()
        set ocidFileExtension to ocidFileExtension's lowercaseString()
        set strFileExtension to ocidFileExtension as text
        ####データ読み込み
        set ocidImageData to (refMe's NSImage's alloc()'s initWithContentsOfURL:ocidFilePathURL)
        ####set ocidImageSize to ocidImageData's |size|()
        set ocidImageRepArray to ocidImageData's representations()
        set ocidImageRep to (ocidImageRepArray's objectAtIndex:0)
        set numpixelsWidth to ocidImageRep's pixelsWide()
        set numpixelsHeight to ocidImageRep's pixelsHigh()
        set ocidImageRepSize to ocidImageRep's |size|()
        set numPointWidth to width of ocidImageRepSize
        set numPointHeight to height of ocidImageRepSize
        set numCurrentDPI to ((numResolution * numpixelsWidth) / numPointWidth) as integer
        set numNewImageWidth to ((numResolution * numpixelsWidth) / numResolution)
        set numNewImageHeight to ((numResolution * numpixelsHeight) / numResolution)
        #####リサイズして-->この場合、ピクセル数そのままサイズ変更=解像度が変わる
        set recordNewImageSize to {width:numNewImageWidth, height:numNewImageHeight} as record
        (ocidImageRep's setSize:recordNewImageSize)
        ###展開
        if strFileExtension is "png" then
            set ocidSaveImageType to refMe's NSBitmapImageFileTypePNG
            set recordPngProperties to {NSImageInterlaced:true, NSImageDitherTransparency:true}
            set ocidNewImageData to (ocidImageRep's representationUsingType:ocidSaveImageType |properties|:recordPngProperties)
        else if strFileExtension is "jpeg" then
            set ocidSaveImageType to refMe's NSBitmapImageFileTypeJPEG
            set ocidBackGroundColor to (refMe's NSColor's colorWithDeviceRed:0.0 green:0.0 blue:0.0 alpha:1.0)
            set recordJpgProperties to {NSImageCompressionFactor:1.0, NSImageProgressive:false, NSImageFallbackBackgroundColor:ocidBackGroundColor}
            set ocidNewImageData to (ocidImageRep's representationUsingType:ocidSaveImageType |properties|:recordJpgProperties)
        else if strFileExtension is "jpg" then
            set ocidSaveImageType to refMe's NSBitmapImageFileTypeJPEG
            set ocidBackGroundColor to (refMe's NSColor's colorWithDeviceRed:0.0 green:0.0 blue:0.0 alpha:1.0)
            set recordJpgProperties to {NSImageCompressionFactor:1.0, NSImageProgressive:false, NSImageFallbackBackgroundColor:ocidBackGroundColor}
            set ocidNewImageData to (ocidImageRep's representationUsingType:ocidSaveImageType |properties|:recordJpgProperties)
            
        end if
        
        ###上書き保存
        set boolResults to (ocidNewImageData's writeToURL:ocidFilePathURL atomically:true)
        set ocidImageRep to ""
        set ocidNewImageData to ""
        set ocidImageData to ""
        
        
        ##繰り返しの終わり
    end repeat
end open


|

« [NSSize] NSMakeSize | トップページ | [com.apple.screencapture]スクリーンキャプチャの保存先 »

NSImage」カテゴリの記事