NSBitmapImageRep

画像のCROP w指定 (少し修正)


あくまでも参考にしてください

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

サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#com.cocolog-nifty.quicktimer.icefloe
004----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
005use AppleScript version "2.8"
006use framework "Foundation"
007use framework "AppKit"
008use scripting additions
009
010property refMe : a reference to current application
011
012############################
013### 入力ダイアログ
014############################
015## クリップボードの中身取り出し
016###初期化
017set appPasteboard to refMe's NSPasteboard's generalPasteboard()
018set ocidPastBoardTypeArray to appPasteboard's types
019###テキストがあれば
020set boolContain to ocidPastBoardTypeArray's containsObject:"public.utf8-plain-text"
021if boolContain = true then
022  ###値を格納する
023  tell application "Finder"
024    set strReadString to (the clipboard as text) as text
025  end tell
026  ###Finderでエラーしたら
027else
028  set boolContain to ocidPastBoardTypeArray's containsObject:"NSStringPboardType"
029  if boolContain = true then
030    set ocidReadString to ocidPasteboard's readObjectsForClasses:({refMe's NSString}) options:(missing value)
031    set strReadString to ocidReadString as text
032  else
033    log "テキストなし"
034    set strReadString to ("横方向左右CROP『px』数値入力") as text
035  end if
036end if
037
038###ダイアログを全面に
039set strName to (name of current application) as text
040if strName is "osascript" then
041  tell application "Finder" to activate
042else
043  tell current application to activate
044end if
045set aliasIconPath to (POSIX file "/System/Applications/Calculator.app/Contents/Resources/AppIcon.icns") as alias
046set strTitle to ("入力してください") as text
047set strMes to ("横方向左右CROP『px』数値入力\n例:100だと\n左100px右100pxCropします") as text
048set 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)
049
050if (gave up of recordResult) is true then
051  return "時間切れです"
052else if (button returned of recordResult) is "キャンセル" then
053  return "キャンセルです"
054else
055  set strReturnedText to (text returned of recordResult) as text
056end if
057#戻り値の整形
058set ocidResponseText to (refMe's NSString's stringWithString:(strReturnedText))
059###タブと改行を除去しておく
060set ocidTextM to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
061ocidTextM's appendString:(ocidResponseText)
062##改行除去
063set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\n") withString:("")
064set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\r") withString:("")
065##タブ除去
066set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\t") withString:("")
067####戻り値を半角にする
068set ocidNSStringTransform to (refMe's NSStringTransformFullwidthToHalfwidth)
069set ocidTextM to (ocidTextM's stringByApplyingTransform:ocidNSStringTransform |reverse|:false)
070##カンマ置換
071set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:(",") withString:(".")
072###数字以外の値を取る
073set ocidDecSet to refMe's NSCharacterSet's decimalDigitCharacterSet
074set ocidCharSet to ocidDecSet's invertedSet()
075set ocidCharArray to ocidTextM's componentsSeparatedByCharactersInSet:ocidCharSet
076set ocidInteger to ocidCharArray's componentsJoinedByString:""
077#
078set numCropW to ocidInteger as integer
079
080############################
081### ダイアログ
082############################
083set appFileManager to refMe's NSFileManager's defaultManager()
084###デフォルトロケーション
085set ocidUserDocumentPathArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
086set ocidDesktopPathURL to ocidUserDocumentPathArray's firstObject()
087set aliasDefaultLocation to (ocidDesktopPathURL's absoluteURL()) as alias
088#####ダイアログを前面に
089tell current application
090  set strName to name as text
091end tell
092####スクリプトメニューから実行したら
093if strName is "osascript" then
094  tell application "Finder" to activate
095else
096  tell current application to activate
097end if
098####ダイアログを出す
099set listUTI to {"public.image"} as list
100set listAliasFilePath to (choose file with prompt "ファイルを選んでください" default location (aliasDefaultLocation) of type listUTI with invisibles, showing package contents and multiple selections allowed) as list
101############
102#URLのARRAYに
103set ocidFilePathURLArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
104repeat with itemAliasFilePath in listAliasFilePath
105  set strFilePath to (POSIX path of itemAliasFilePath) as text
106  set ocidFilePath to (refMe's NSString's stringWithString:(strFilePath))
107  set ocidFilePathURL to (refMe's NSURL's fileURLWithPath:(ocidFilePath))
108  (ocidFilePathURLArray's addObject:(ocidFilePathURL))
109end repeat
110set numCntURL to (count of ocidFilePathURLArray) as integer
111############
112#並び替え
113set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("absoluteString") ascending:(yes) selector:("localizedStandardCompare:")
114set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
115set ocidSortedArray to ocidFilePathURLArray's sortedArrayUsingDescriptors:(ocidDescriptorArray)
116#ダイアログ用のURL
117set ocidFirstObjectURL to ocidSortedArray's firstObject()
118set ocidContainerDirPathURL to ocidFirstObjectURL's URLByDeletingLastPathComponent()
119set aliasSaveDirPath to (ocidContainerDirPathURL's absoluteURL()) as alias
120############
121#保存先
122set strMes to "保存先フォルダを選んでください" as text
123set strPrompt to "保存先フォルダを選択してください" as text
124try
125  set aliasSaveDirPath to (choose folder strMes with prompt strPrompt default location aliasSaveDirPath with invisibles and showing package contents without multiple selections allowed) as alias
126on error
127  log "エラーしました"
128  return "エラーしました"
129end try
130set strSaveDirPath to (POSIX path of aliasSaveDirPath) as text
131set ocidSaveDirPath to refMe's NSString's stringWithString:(strSaveDirPath)
132set ocidSaveDirPathURL to refMe's NSURL's fileURLWithPath:(ocidSaveDirPath)
133
134
135
136############################
137####選んだファイルパスの数だけ繰り返し
138############################
139repeat with itemAliasFilePath in listAliasFilePath
140  ##入力パス
141  set strFilePath to (POSIX path of itemAliasFilePath) as text
142  set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
143  set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
144  set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
145  set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
146  set ocidFileName to ocidFilePathURL's lastPathComponent()
147  #保存先パス
148  set ocidSaveFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(ocidFileName))
149  ###########################
150  ###【1】元イメージ
151  ##NSIMAGEに読み込む
152  set ocidReadImage to (refMe's NSImage's alloc()'s initWithContentsOfURL:(ocidFilePathURL))
153  set ocidNsImageSize to ocidReadImage's |size|()
154  set numPtWidth to ocidNsImageSize's width
155  set numPtHeigh to ocidNsImageSize's height
156  ##BitMapRepに変換
157  set ocidReadImageRepArray to ocidReadImage's representations()
158  set ocidReadImageRep to (ocidReadImageRepArray's objectAtIndex:0)
159  ##ピクセルサイズ取得
160  set numPixelsWidth to ocidReadImageRep's pixelsWide()
161  set numPixelsHeight to ocidReadImageRep's pixelsHigh()
162  ##まずは72ppiにする
163  set ocidPixelsSize to refMe's NSMakeSize(numPixelsWidth, numPixelsHeight)
164  (ocidReadImageRep's setSize:(ocidPixelsSize))
165  log numPtWidth as integer
166  log numPixelsHeight as integer
167  
168  ###########################
169  ###【2】出力イメージ numCropW
170  ###サイズ計算
171  #
172  set numSetPxWidth to (numPixelsWidth - (numCropW * 2)) as integer
173  #
174  set numSetPxHeight to numPixelsHeight as integer
175  #出力用の画像ピクセルサイズ
176  set ocidNewImagePixelsSize to refMe's NSMakeSize(numSetPxWidth, numSetPxHeight)
177  ###出力用イメージ NSBitmapImageRep
178  # samplesPerPixel
179  set intSPP to 4 as integer
180  # bitsPerSample
181  set intBPS to 8 as integer
182  # bytesPerRow
183  set intBPR to 0 as integer
184  # bitsPerPixel
185  set intBPP to 32 as integer
186  # RGB系のカラースペース
187  set ocidColorSpaceName to refMe's NSCalibratedRGBColorSpace
188  # アルファあり
189  set ocidBitmapFormat to refMe's NSAlphaFirstBitmapFormat
190  ##出力ピクセルサイズのブランクイメージ
191  set ocidArtBoardRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numSetPxWidth) pixelsHigh:(numSetPxHeight) bitsPerSample:(intBPS) samplesPerPixel:(intSPP) hasAlpha:(true) isPlanar:(false) colorSpaceName:(ocidColorSpaceName) bitmapFormat:(ocidBitmapFormat) bytesPerRow:(intBPR) bitsPerPixel:(intBPP))
192  
193  ###########################
194  ###【3】ArtBord
195  ### 初期化 CodeBase
196  refMe's NSGraphicsContext's saveGraphicsState()
197  ###Context
198  set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep))
199  ###生成された画像でNSGraphicsContext初期化
200  (refMe's NSGraphicsContext's setCurrentContext:(ocidContext))
201  ##色を個別に指定する場合 値は0が暗 1が明
202  set ocidSetColor to (refMe's NSColor's colorWithSRGBRed:(1.0) green:(1.0) blue:(1.0) alpha:(0.0))
203  ocidSetColor's |set|()
204  ###画像生成
205  set ocidSaveImageRect to refMe's NSMakeRect(0, 0, numSetPxWidth, (numSetPxHeight))
206  #refMe's NSRectFill({origin:{x:0, y:0}, |size|:{width:(numSetPxWidth), height:(numSetPxHeight)}})
207  refMe's NSRectFill(ocidSaveImageRect)
208  ####画像作成終了
209  refMe's NSGraphicsContext's restoreGraphicsState()
210  
211  ###########################
212  ###【3】合成 リザイスでNSCompositeSourceOver
213  ### 初期化
214  refMe's NSGraphicsContext's saveGraphicsState()
215  ###Context
216  set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep))
217  ###NSGraphicsContext初期化
218  (refMe's NSGraphicsContext's setCurrentContext:(ocidContext))
219  ####NSCompositeSourceOver
220  set ocidFromRect to refMe's NSMakeRect((numCropW), 0, numSetPxWidth, numSetPxHeight)
221  # {origin:{x:(numCropW / 2), y:(0)}, |size|:{width:(numSetPxWidth), Hight:(numSetPxHeight)}}
222  
223  # set ocidOption to (refMe's NSCompositeSourceOver)
224  set ocidOption to (refMe's NSCompositingOperationSourceOver)
225  
226  ##  set ocidOption to (refMe's NSCompositeCopy)
227  (ocidReadImageRep's drawInRect:(ocidSaveImageRect) fromRect:(ocidFromRect) operation:(ocidOption) fraction:1.0 respectFlipped:true hints:(missing value))
228  ####画像作成終了
229  refMe's NSGraphicsContext's restoreGraphicsState()
230  
231  ###########################
232  ###【5】保存
233  ####保存オプション
234  set ocidProperty to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
235  (ocidProperty's setObject:(refMe's NSNumber's numberWithBool:false) forKey:(refMe's NSImageInterlaced))
236  (ocidProperty's setObject:(refMe's NSNumber's numberWithDouble:(1 / 2.2)) forKey:(refMe's NSImageGamma))
237  ##保存
238  set ocidNSInlineData to (ocidArtBoardRep's representationUsingType:(refMe's NSBitmapImageFileTypePNG) |properties|:(ocidProperty))
239  set boolDone to (ocidNSInlineData's writeToURL:(ocidSaveFilePathURL) atomically:true)
240  
241  set ocidReadImageRep to ""
242  set ocidArtBoardRep to ""
243end repeat
AppleScriptで生成しました

|

解像度変更 修正


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

#!/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 "Quartz"
# use framework "CoreImage"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application

on run
  
  set aliasIconPass to (POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/MultipleItemsIcon.icns") as alias
  set strDialogText to "ドロップしても利用できます"
  set strTitleText to "画像ファイルを選んでください"
  set listButton to {"ファイルを選びます", "キャンセル"} as list
display dialog strDialogText buttons listButton default button 1 cancel button 2 with title strTitleText with icon aliasIconPass giving up after 2 with hidden answer
  
  set aliasDefaultLocation to (path to desktop folder from user domain) as alias
  set listChooseFileUTI to {"public.png", "public.jpeg"}
  set strPromptText to "イメージファイルを選んでください" as text
  set strPromptMes to "イメージファイルを選んでください" as text
  set listAliasFilePath to (choose file strPromptMes with prompt strPromptText default location (aliasDefaultLocation) of type listChooseFileUTI with showing package contents, invisibles and multiple selections allowed) as list
  
  -->値をOpenに渡たす
open listAliasFilePath
end run


on open listAliasFilePath
  ##########################
  ####解像度を指定する
  ##########################
  ###ダイアログを前面に出す
  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 listResolution to {"72", "96", "120", "144", "216", "288", "300", "360"} as list
  #
  try
    set listResponse to (choose from list listResolution with title "選んでください" with prompt "解像度を選んでください" default items (item 1 of listResolution) 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 strResolution to (item 1 of listResponse) as text
  set numResolution to strResolution as integer
  
  ##########################
  ####ファイルの数だけ繰り返し
  ##########################
  set ocidFalse to (refMe's NSNumber's numberWithBool:false)'s boolValue
  set ocidTrue to (refMe's NSNumber's numberWithBool:true)'s boolValue
  repeat with itemAliasFilePath in listAliasFilePath
    ####まずはUNIXパスにして
    set strFilePath to (POSIX path of itemAliasFilePath) 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)
    ####ファイル名を取得
    set ocidFileName to ocidFilePathURL's lastPathComponent()
    ####ファイル名から拡張子を取っていわゆるベースファイル名を取得
    set strPrefixName to ocidFileName's stringByDeletingPathExtension as text
    ####コンテナディレクトリを取得
    set ocidContainerDirURL to ocidFilePathURL's URLByDeletingLastPathComponent()
    ###拡張子を取得して小文字にしておく
    set ocidExtensionName to ocidFilePathURL's pathExtension()
    set ocidExtensionNameLowCase to ocidExtensionName's lowercaseString()
    set strExtensionName to ocidExtensionNameLowCase as text
    #####################
    #### 本処理
    #####################
    ####データ読み込み
    set ocidImage to (refMe's NSImage's alloc()'s initWithContentsOfURL:(ocidFilePathURL))
    #BmpRepに変換
    set ocidImageRepArray to ocidImage's representations()
    set ocidImageRep to ocidImageRepArray's firstObject()
    set ocidSize to ocidImageRep's |size|()
    set numWptR to ocidSize's width
    set numHptR to ocidSize's height
    #プロパティを保存時用に取得
    try
      ##ちょいちょいpropertiesの取得でエラーになるので
      set ocidPropertiesDict to ocidImageRep's |properties|
    on error
      ##プロパティがある項目だけ保存用に取得する
      set ocidPropertiesDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
      set listProperties to {(refMe's NSImageColorSyncProfileData), (refMe's NSImageCompressionFactor), (refMe's NSImageCompressionMethod), (refMe's NSImageCurrentFrame), (refMe's NSImageCurrentFrameDuration), (refMe's NSImageDitherTransparency), (refMe's NSImageEXIFData), (refMe's NSImageFallbackBackgroundColor), (refMe's NSImageFrameCount), (refMe's NSImageGamma), (refMe's NSImageInterlaced), (refMe's NSImageLoopCount), (refMe's NSImageProgressive), (refMe's NSImageRGBColorTable)} as list
      repeat with itemPropertie in listProperties
        set ocidSetValue to (ocidImageRep's valueForProperty:(itemPropertie))
        if ocidSetValue ≠ (missing value) then
(ocidPropertiesDict's setObject:(ocidSetValue) forKey:(itemPropertie))
        end if
      end repeat
    end try
    #ピクセルサイズを取得
    set numPixelsWidth to ocidImageRep's pixelsWide()
    set numPixelsHeight to ocidImageRep's pixelsHigh()
    #サイズセットする比率
    set numSetResolution to (numResolution / 72.0) as number
    # ピクセルサイズに↑の比率で割って セットするポイントサイズ
    set numNewImageWidth to (numPixelsWidth / numSetResolution) as number
    set numNewImageHeight to (numPixelsHeight / numSetResolution) as number
    #####リサイズの値レコード
    set recordNewImageSize to {width:numNewImageWidth, height:numNewImageHeight} as record
    ###リサイズ
(ocidImageRep's setSize:(recordNewImageSize))
    #拡張子による分岐
    if strExtensionName is "png" then
      set ocidSaveImageType to (refMe's NSBitmapImageFileTypePNG)
      set ocidNewImageData to (ocidImageRep's representationUsingType:ocidSaveImageType |properties|:(ocidPropertiesDict))
      set ocidSaveFilePathURL to ocidFilePathURL
    else if strExtensionName is "jpg" then
      set ocidSaveImageType to (refMe's NSBitmapImageFileTypeJPEG)
      set ocidNewImageData to (ocidImageRep's representationUsingType:ocidSaveImageType |properties|:(ocidPropertiesDict))
      set ocidSaveFilePathURL to ocidFilePathURL
    else if strExtensionName is "jpeg" then
      set ocidSaveImageType to (refMe's NSBitmapImageFileTypeJPEG)
      set ocidNewImageData to (ocidImageRep's representationUsingType:ocidSaveImageType |properties|:(ocidPropertiesDict))
      set ocidSaveFilePathURL to ocidFilePathURL
    else if strExtensionName is "tif" then
      set ocidSaveImageType to (refMe's NSBitmapImageFileTypeTIFF)
      set ocidNewImageData to (ocidImageRep's representationUsingType:ocidSaveImageType |properties|:(ocidPropertiesDict))
      set ocidSaveFilePathURL to ocidFilePathURL
    else if strExtensionName is "tiff" then
      set ocidSaveImageType to (refMe's NSBitmapImageFileTypeTIFF)
      set ocidNewImageData to (ocidImageRep's representationUsingType:ocidSaveImageType |properties|:(ocidPropertiesDict))
      set ocidSaveFilePathURL to ocidFilePathURL
      (*
else if strExtensionName is "bmp" then
set ocidSaveImageType to (refMe's NSBitmapImageFileTypeBMP)
set ocidNewImageData to (ocidImageRep's representationUsingType:ocidSaveImageType |properties|:(ocidPropertiesDict))
set ocidSaveFilePathURL to ocidFilePathURL
else if strExtensionName is "gif" then
set ocidSaveImageType to (refMe's NSBitmapImageFileTypeGIF)
set ocidNewImageData to (ocidImageRep's representationUsingType:ocidSaveImageType |properties|:(ocidPropertiesDict))
set ocidSaveFilePathURL to ocidFilePathURL
*)
    else
log "上記以外の場合はPNGで別名保存"
      set ocidSaveImageType to (refMe's NSBitmapImageFileTypePNG)
      set ocidNewImageData to (ocidImageRep's representationUsingType:ocidSaveImageType |properties|:(ocidPropertiesDict))
      set strSaveFileName to (strPrefixName & ".png") as text
      set ocidSaveFilePathURL to (ocidContainerDirURL's URLByAppendingPathComponent:(strSaveFileName))
    end if
    ###上書き保存
    set boolResults to (ocidNewImageData's writeToURL:(ocidSaveFilePathURL) atomically:true)
    #結果
    if boolResults is true then
log "処理OK"
    else
log "処理NGなのでそのままにする"
log "失敗ラベル赤を塗る"
      set boolResults to (ocidImagFilePathURL's setResourceValue:6 forKey:(refMe's NSURLLabelNumberKey) |error|:(reference))
    end if
    set ocidImageData to ""
    set ocidImageRep to ""
    set ocidNewImageData to ""
  end repeat
end open


|

[2in1]画像合成2in1 2up 右→左順


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

#!/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 "UniformTypeIdentifiers"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application
property strBundleID : "com.apple.Preview"


######################################
##ファイル選択
######################################
#前面に
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
###ダイアログ
tell application "Finder"
  set aliasDefaultLocation to (path to desktop folder from user domain) as alias
end tell
set listUTI to {"public.png", "public.jpeg"}
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 multiple selections allowed without showing package contents and invisibles) as list
#############################################
#URLリストにする
set ocidFilePathURLArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
repeat with itemAlias in listAliasFilePath
  set strFilePath to (POSIX path of itemAlias) 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)
(ocidFilePathURLArray's addObject:(ocidFilePathURL))
end repeat
#############################################
#ソートしておく
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)
#
set numCntArray to (count of ocidSortedArray) as integer
#############################################
#保存先確保
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 ocidSaveDirPathURL to ocidDesktopDirPathURL's URLByAppendingPathComponent:("画像合成済みフォルダ")
#
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
set strBaseFileName to ("Composed") as text


#############################################
#本処理
set numCntComposed to 0 as integer

repeat with itemNO from 0 to (numCntArray - 1) by 2
  ##############################
  ###画像読み込みA
  set ocidAsideFileURL to (ocidSortedArray's objectAtIndex:(itemNO))
  set ocidImageDataA to (refMe's NSImage's alloc()'s initWithContentsOfURL:(ocidAsideFileURL))
  #PTサイズとPXサイズを取得して解像度比を取得しておく
  set ocidImageSizePtA to ocidImageDataA's |size|()
  set ocidImageWidthPtA to ocidImageSizePtA's width()
  set ocidImageHeightPtA to ocidImageSizePtA's height()
  set ocidOsDispatchArrayA to ocidImageDataA's representations()
  set ocidBmpImageRepA to ocidOsDispatchArrayA's firstObject()
  set ocidPxWA to ocidBmpImageRepA's pixelsWide()
  set ocidPxHA to ocidBmpImageRepA's pixelsHigh()
  set numRatioA to (ocidPxWA / ocidImageWidthPtA) as number
  ##############################
  ###画像読み込みB
  set numNextNO to (itemNO + 1 as integer)
  if numNextNO > (numCntArray - 1) then
    #奇数ファイル数時の最後のページ
    set ocidBmpImageRepB to (missing value)
    set ocidPxWB to ocidPxWA
    set ocidPxHB to ocidPxHA
  else
    #PTサイズとPXサイズを取得して解像度比を取得しておく
    set ocidBsideFileURL to (ocidSortedArray's objectAtIndex:(numNextNO))
    set ocidImageDataB to (refMe's NSImage's alloc()'s initWithContentsOfURL:(ocidBsideFileURL))
    set ocidImageSizePtB to ocidImageDataB's |size|()
    set ocidImageWidthPtB to ocidImageSizePtB's width()
    set ocidImageHeightPtB to ocidImageSizePtB's height()
    set ocidOsDispatchDataB to ocidImageDataB's TIFFRepresentation()
    set ocidBmpImageRepB to (refMe's NSBitmapImageRep's imageRepWithData:(ocidOsDispatchDataB))
    set ocidPxWB to ocidBmpImageRepB's pixelsWide()
    set ocidPxHB to ocidBmpImageRepB's pixelsHigh()
    set numRatioB to (ocidPxWB / ocidImageWidthPtB) as number
  end if
  ##############################
  #Aardboard生成 出力イメージ
  set ocidAardboardW to (ocidPxWA + ocidPxWB)
  if ocidPxHA > ocidPxHB then
    set ocidAardboardH to ocidPxHA
  else
    set ocidAardboardH to ocidPxHB
  end if
  set ocidAardboardRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(ocidAardboardW) pixelsHigh:(ocidAardboardH) bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:false colorSpaceName:(refMe's NSCalibratedRGBColorSpace) bitmapFormat:(refMe's NSAlphaFirstBitmapFormat) bytesPerRow:0 bitsPerPixel:32)
  ##############################
  #右側合成 R
  set ocidGraphicsContext to refMe's NSGraphicsContext
ocidGraphicsContext's saveGraphicsState()
(ocidGraphicsContext's setCurrentContext:(ocidGraphicsContext's graphicsContextWithBitmapImageRep:(ocidAardboardRep)))
  ####解像度入れた画像の合成位置
  set numSetSizeWA to (ocidPxWA * numRatioA) as integer
  set numSetSizeHA to (ocidPxHA * numRatioA) as integer
  set ocidRectR to refMe's NSMakeRect(ocidPxWB, 0, numSetSizeWA, numSetSizeHA)
log ocidRectR as list
  ###Aardboard右側のRECT
  set ocidRextA to refMe's NSMakeRect(0, 0, ocidPxWA, ocidPxHA)
log ocidRextA as list
(ocidBmpImageRepA's drawInRect:(ocidRectR) fromRect:(ocidRextA) operation:(refMe's NSCompositeSourceOver) fraction:1.0 respectFlipped:false hints:(missing value))
  ####処理終了
ocidGraphicsContext's restoreGraphicsState()
  ##############################
  if ocidBmpImageRepB is not (missing value) then
    #左側合成 L
    set ocidGraphicsContext to refMe's NSGraphicsContext
ocidGraphicsContext's saveGraphicsState()
(ocidGraphicsContext's setCurrentContext:(ocidGraphicsContext's graphicsContextWithBitmapImageRep:(ocidAardboardRep)))
    ####解像度入れた画像の合成位置
    set numSetSizeWB to (ocidPxWB * numRatioB) as integer
    set numSetSizeHB to (ocidPxHB * numRatioB) as integer
    set ocidRectL to refMe's NSMakeRect(0, 0, numSetSizeWB, numSetSizeHB)
log ocidRectL as list
    ###Aardboard左側のRECT
    set ocidRextB to refMe's NSMakeRect(0, 0, ocidPxWB, ocidPxHB)
log ocidRextB as list
(ocidBmpImageRepB's drawInRect:(ocidRectL) fromRect:(ocidRextB) operation:(refMe's NSCompositeSourceOver) fraction:1.0 respectFlipped:false hints:(missing value))
    ####処理終了
ocidGraphicsContext's restoreGraphicsState()
  end if
  
  ##############################
  #ImagePep をNSImageに
  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))
  set ocidSaveData to (ocidAardboardRep's representationUsingType:(refMe's NSPNGFileType) |properties|:(ocidPropertiesDict))
  
  ##############################
  #保存
  set strSaveFileName to (numCntComposed & "-" & strBaseFileName) as text
  set ocidBaseFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strSaveFileName))
  set ocidSaveFilePathURL to (ocidBaseFilePathURL's URLByAppendingPathExtension:("png"))
  #
  set listDone to (ocidSaveData's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error|:(reference))
  if (item 1 of listDone) is false then
return "保存に失敗しました"
  end if
  
  #合成後のファイル番号カウントアップ
  set numCntComposed to numCntComposed + 1 as integer
end repeat




|

[NSBitmapImageRep] 処理一覧

カテゴリーページ
https://quicktimer.cocolog-nifty.com/icefloe/cat76051747/index.html

基本
ピクセルサイズ取得
https://quicktimer.cocolog-nifty.com/icefloe/2024/02/post-7fb9c5.html

イメージサイズの取得(表示サイズpt)
https://quicktimer.cocolog-nifty.com/icefloe/2024/02/post-278688.html

画像解像度取得
https://quicktimer.cocolog-nifty.com/icefloe/2024/02/post-ea8e98.html

画像の解像度を72ppiに変更する
https://quicktimer.cocolog-nifty.com/icefloe/2024/02/post-fc01b2.html

画像の解像度を144ppiに変更する
https://quicktimer.cocolog-nifty.com/icefloe/2024/02/post-70cf76.html

画像のカラースペースを取得
https://quicktimer.cocolog-nifty.com/icefloe/2024/02/post-d711d0.html



画像生成
画像を生成する
https://quicktimer.cocolog-nifty.com/icefloe/2024/02/post-048089.html

生成した画像に色を塗る
https://quicktimer.cocolog-nifty.com/icefloe/2024/02/post-c4abd5.html

生成した画像に文字を描画する
https://quicktimer.cocolog-nifty.com/icefloe/2024/02/post-fe5b34.html

[NSBitmapImageRep]CMYKカラーのTIFFを作る
https://quicktimer.cocolog-nifty.com/icefloe/2022/09/post-494202.html

[NSBitmapImageRep]8bitグレーTIFFを作る
https://quicktimer.cocolog-nifty.com/icefloe/2022/09/post-55237c.html



リサイズ
[NSBitmapImageRep]イメージサイズ変更
https://quicktimer.cocolog-nifty.com/icefloe/2023/06/post-46c144.html
複数画像のリサイズ(高さ揃え)saveGraphicsState使用
https://quicktimer.cocolog-nifty.com/icefloe/2023/09/post-497d06.html
複数画像のリサイズ(高さ揃え)lockFocus使用(非推奨)
https://quicktimer.cocolog-nifty.com/icefloe/2023/09/post-75d498.html


書出
マルチページTIFFをページ毎で別ファイルに書き出す
https://quicktimer.cocolog-nifty.com/icefloe/2023/06/post-759904.html

[Gif]gifアニメーションの各コマを別ファイルに保存(イメージシーケンス保存)
https://quicktimer.cocolog-nifty.com/icefloe/2023/05/post-4c0c6b.html

|

画像を生成する


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

#!/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 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)
#保存用オプション
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))





|

画像の解像度を144ppiに変更する


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

#!/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 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)
  #別名保存=png用
  set strExtensionName to ocidFilePathURL's pathExtension() as text
  set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension()
  set strBaseFileName to ocidBaseFilePathURL's lastPathComponent() as text
  set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
  ##同じフォーマットで保存するために
  set listResourceValue to (ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLContentTypeKey) |error|:(reference))
  set ocidContentType to (item 2 of listResourceValue)
  set strUTI to (ocidContentType's identifier) as text
  ##NSIMAGEに読み込む
  set ocidReadImage to (refMe's NSImage's alloc()'s initWithContentsOfURL:(ocidFilePathURL))
  ##サイズ取得 NSimageとして取得
  set recordSize to ocidReadImage's |size|()
log recordSize
  set numWidthPtSize to (width of recordSize) as number
  set numHeightPtSize to (height of recordSize) as number
  ##BitMapRepに変換
  set ocidReadImageRepArray to ocidReadImage's representations()
  set ocidReadImageRep to (ocidReadImageRepArray's firstObject())
  ##ピクセルサイズ取得
  set numPixelsWidth to (ocidReadImageRep's pixelsWide()) as integer
  set numPixelsHeight to (ocidReadImageRep's pixelsHigh()) as integer
  ##
  set numMag to (numPixelsWidth / numWidthPtSize) as number
log numMag
  ##解像度
  set numResolution to (72 * numMag) as integer
log numResolution
  if numResolution = 144 then
log "解像度は144ppiです"
    set boolChgRes to true as boolean
  else
log "解像度を144ppiに変更します"
    set boolChgRes to false as boolean
  end if
  ##解像度が72以外の場合のみ処理
  if boolChgRes is false then
    #ピクセルサイズの半分でイメージのサイズをセット =144
    #この方法だと奇数PX画像でサイズの丸めが発生するが…
    set numHarfPixelsWidth to (numPixelsWidth / 2) as number
    set numHarfPixelsHeight to (numPixelsHeight / 2) as number
log numPixelsWidth & "→" & numHarfPixelsWidth
log numPixelsHeight & "→" & numHarfPixelsHeight
    
    set ocidPixelsSize to refMe's NSMakeSize(numHarfPixelsWidth, numHarfPixelsHeight)
(ocidReadImageRep's setSize:(ocidPixelsSize))
    set ocidPropertyDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
    #
    set strBaseFileName to (strBaseFileName & "@2x") as text
    set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
    set ocidBaseFilePathURL to (ocidContainerDirPathURL's URLByAppendingPathComponent:(strBaseFileName))
    if strUTI is "public.jpeg" then
      set ocidBackGroundColor to (refMe's NSColor's colorWithDeviceRed:0.0 green:0.0 blue:0.0 alpha:1.0)
(ocidPropertyDict's setObject:(ocidBackGroundColor) forKey:(refMe's NSImageFallbackBackgroundColor))
(ocidPropertyDict's setObject:(efMe's NSNumber's numberWithFloat:(1.0)) forKey:(refMe's NSImageCompressionFactor))
(ocidPropertyDict's setObject:(efMe's NSNumber's numberWithBool:(false)) forKey:(refMe's NSImageProgressive))
      set ocidImageType to (refMe's NSBitmapImageFileTypeJPEG)
      set ocidSaveFilePathURL to (ocidBaseFilePathURL's URLByAppendingPathExtension:("jpg"))
      
    else if strUTI is "public.tiff" then
(ocidPropertyDict's setObject:(refMe's NSTIFFCompressionNone) forKey:(refMe's NSImageCompressionMethod))
(ocidPropertyDict's setObject:(efMe's NSNumber's numberWithFloat:(1.0)) forKey:(refMe's NSImageCompressionFactor))
      set ocidImageType to (refMe's NSBitmapImageFileTypeTIFF)
      set ocidSaveFilePathURL to (ocidBaseFilePathURL's URLByAppendingPathExtension:("tif"))
      
    else if strUTI is "public.png" then
(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)
      set ocidSaveFilePathURL to (ocidBaseFilePathURL's URLByAppendingPathExtension:("png"))
    else
      ## TIFFとJPEG以外はPNGで保存
(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)
      set ocidSaveFilePathURL to (ocidBaseFilePathURL's URLByAppendingPathExtension:("png"))
    end if
    set ocidImageData to (ocidReadImageRep's representationUsingType:(ocidImageType) |properties|:(ocidPropertyDict))
    set listDone to (ocidImageData's writeToURL:(ocidSaveFilePathURL) options:0 |error|:(reference))
  end if
  
  
  
  
  set ocidReadImage to ""
  set ocidReadImageRepArray to ""
  set ocidReadImageRep to ""
end repeat




|

画像の解像度を72ppiに変更する


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

#!/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 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)
  #別名保存=png用
  set strExtensionName to ocidFilePathURL's pathExtension() as text
  set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension()
  ##同じフォーマットで保存するために
  set listResourceValue to (ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLContentTypeKey) |error|:(reference))
  set ocidContentType to (item 2 of listResourceValue)
  set strUTI to (ocidContentType's identifier) as text
  ##NSIMAGEに読み込む
  set ocidReadImage to (refMe's NSImage's alloc()'s initWithContentsOfURL:(ocidFilePathURL))
  ##サイズ取得 NSimageとして取得
  set recordSize to ocidReadImage's |size|()
  set numWidthPtSize to (width of recordSize) as number
  set numHeightPtSize to (height of recordSize) as number
  ##BitMapRepに変換
  set ocidReadImageRepArray to ocidReadImage's representations()
  set ocidReadImageRep to (ocidReadImageRepArray's firstObject())
  ##ピクセルサイズ取得
  set numPixelsWidth to (ocidReadImageRep's pixelsWide()) as integer
  set numPixelsHeight to (ocidReadImageRep's pixelsHigh()) as integer
  ##
  set numMag to (numPixelsWidth / numWidthPtSize) as number
log numMag
  ##解像度
  set numResolution to (72 * numMag) as integer
log numResolution
  if numResolution = 72 then
log "解像度は72ppiです"
    set boolChgRes to true as boolean
  else
log "解像度を72ppiに変更します"
    set boolChgRes to false as boolean
  end if
  ##解像度が72以外の場合のみ処理
  if boolChgRes is false then
    #ピクセルサイズで 画像のサイズをセット =72ppi
    set ocidPixelsSize to refMe's NSMakeSize(numPixelsWidth, numPixelsHeight)
(ocidReadImageRep's setSize:(ocidPixelsSize))
    set ocidPropertyDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
    if strUTI is "public.jpeg" then
      set ocidBackGroundColor to (refMe's NSColor's colorWithDeviceRed:0.0 green:0.0 blue:0.0 alpha:1.0)
(ocidPropertyDict's setObject:(ocidBackGroundColor) forKey:(refMe's NSImageFallbackBackgroundColor))
(ocidPropertyDict's setObject:(efMe's NSNumber's numberWithFloat:(1.0)) forKey:(refMe's NSImageCompressionFactor))
(ocidPropertyDict's setObject:(efMe's NSNumber's numberWithBool:(false)) forKey:(refMe's NSImageProgressive))
      set ocidImageType to (refMe's NSBitmapImageFileTypeJPEG)
      set ocidSaveFilePathURL to ocidFilePathURL
      
    else if strUTI is "public.tiff" then
(ocidPropertyDict's setObject:(refMe's NSTIFFCompressionNone) forKey:(refMe's NSImageCompressionMethod))
(ocidPropertyDict's setObject:(efMe's NSNumber's numberWithFloat:(1.0)) forKey:(refMe's NSImageCompressionFactor))
      set ocidImageType to (refMe's NSBitmapImageFileTypeTIFF)
      set ocidSaveFilePathURL to ocidFilePathURL
      
    else if strUTI is "public.png" then
(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)
      set ocidSaveFilePathURL to ocidFilePathURL
    else
      ## TIFFとJPEG以外はPNGで保存
(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)
      set strExtension to (strExtensionName & ".png")
      set ocidSaveFilePathURL to (ocidBaseFilePathURL's URLByAppendingPathExtension:(strExtension))
    end if
    set ocidImageData to (ocidReadImageRep's representationUsingType:(ocidImageType) |properties|:(ocidPropertyDict))
    set listDone to (ocidImageData's writeToURL:ocidSaveFilePathURL options:0 |error|:(reference))
  end if
  
  
  
  
  set ocidReadImage to ""
  set ocidReadImageRepArray to ""
  set ocidReadImageRep 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 scripting additions

property refMe : a reference to current application


############################
### ダイアログ
############################
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 ocidColorSpace to ocidReadImageRep's colorSpace()
  set ocidColorSpaceName to ocidReadImageRep's colorSpaceName()
log ocidColorSpaceName as text
  #
  set strColorName to ocidColorSpace's localizedName as text
  set numColorNO to ocidColorSpace's colorSpaceModel as integer
log strColorName
log numColorNO
  (*
https://quicktimer.cocolog-nifty.com/icefloe/2024/02/post-38db59.html
  
NSColorSpaceModelUnknown(0): 未知のカラーモデル
NSColorSpaceModelGray(1): グレースケールカラーモデル
NSColorSpaceModelRGB(3): RGBカラーモデル
NSColorSpaceModelCMYK(4): CMYKカラーモデル
NSColorSpaceModelLAB(5): CIELABカラーモデル
NSColorSpaceModelDeviceN(6): デバイスNカラーモデル
NSColorSpaceModelIndexed(7): インデックスカラーモデル
NSColorSpaceModelPattern(8): パターンカラーモデル
*)
  
  set ocidReadImage to ""
  set ocidReadImageRepArray to ""
  set ocidReadImageRep 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 scripting additions

property refMe : a reference to current application


############################
### ダイアログ
############################
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))
  ##サイズ取得 NSimageとして取得
  set recordSize to ocidReadImage's |size|()
  set numWidthPtSize to (width of recordSize) as number
  set numHeightPtSize to (height of recordSize) as number
  ##BitMapRepに変換
  set ocidReadImageRepArray to ocidReadImage's representations()
  set ocidReadImageRep to (ocidReadImageRepArray's firstObject())
  ##ピクセルサイズ取得
  set numPixelsWidth to (ocidReadImageRep's pixelsWide()) as integer
  set numPixelsHeight to (ocidReadImageRep's pixelsHigh()) as integer
  ##
  set numMag to (numPixelsWidth / numWidthPtSize) as number
log numMag
  ##解像度
  set numResolution to (72 * numMag) as number
log numResolution
  
  
  set ocidReadImage to ""
  set ocidReadImageRepArray to ""
  set ocidReadImageRep 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 scripting additions

property refMe : a reference to current application


############################
### ダイアログ
############################
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))
  ##サイズ取得 NSimageとして取得
  set recordSize to ocidReadImage's |size|()
  set numWidthPtSize to (width of recordSize) as integer
  set numHeightPtSize to (height of recordSize) as integer
log recordSize
  
  ##BitMapRepに変換
  set ocidReadImageRepArray to ocidReadImage's representations()
  set ocidReadImageRep to (ocidReadImageRepArray's firstObject())
  ##サイズ取得 BmpImageRepとして取得
  set recordSize to ocidReadImageRep's |size|()
  set numWidthPtSize to (width of recordSize) as integer
  set numHeightPtSize to (height of recordSize) as integer
log recordSize
  
  set ocidReadImage to ""
  set ocidReadImageRepArray to ""
  set ocidReadImageRep 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 VMware Fusion 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