20260513

[AppleScript] SIPS+OBJCで画像のピクセルサイズと解像度の取得

[AppleScript] SIPS+OBJCで画像のピクセルサイズと解像度の取得

NOTE記事一覧ですnote.com

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

sipsOCで画像のピクセルサイズと解像度.scpt.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
004(*
005SIPSの戻り値をPLISTにすることで
006そのままDICT辞書として値を取得する方法
007
008ファイルにしなくてもPISTとして値を取得できるので
009スッキリする
010
011com.cocolog-nifty.quicktimer.icefloe *)
012----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
013use AppleScript version "2.8"
014use framework "Foundation"
015use framework "AppKit"
016use framework "UniformTypeIdentifiers"
017use scripting additions
018
019property refMe : a reference to current application
020
021set listUTI to {"public.image"} as list
022try
023   tell application "SystemUIServer"
024      activate
025      set aliasFilePath to (choose file of type listUTI without multiple selections allowed) as alias
026   end tell
027on error strErrMes number numErrNo
028   log strErrMes & numErrNo
029   return false
030end try
031
032#UNIXパスにしてSIPSを実行
033set strImageFilePath to (POSIX path of aliasFilePath) as text
034set strCmd to ("/usr/bin/sips --getProperty allxml \"" & strImageFilePath & "\"")
035set strStdOut to (do shell script strCmd) as text
036
037###########
038#コマンドの戻り値をテキストにして
039set ocidPlistString to refMe's NSString's stringWithString:(strStdOut)
040#NSDATAに変換します
041set ocidPlistData to ocidPlistString's dataUsingEncoding:(refMe's NSUTF8StringEncoding)
042#NSPropertyListSerializationでDICTに変換します
043set ocidOption to (refMe's NSPropertyListMutableContainersAndLeaves)
044set ocidFormat to (refMe's NSPropertyListXMLFormat_v1_0)
045set listResponse to (refMe's NSPropertyListSerialization's propertyListWithData:(ocidPlistData) options:(ocidOption) format:(ocidFormat) |error|:(reference))
046set ocidPlistDict to (first item of listResponse)
047#DICTから値を取得する
048set ocidWpx to ocidPlistDict's objectForKey:("pixelWidth")
049set ocidHpx to ocidPlistDict's objectForKey:("pixelHeight")
050set ocidPPI to ocidPlistDict's objectForKey:("dpiWidth")
051log ocidWpx as integer
052log ocidHpx as integer
053log ocidPPI as integer
054
055(*
056SIPSのキー 画像から取得する時用
057bitsPerSample
058dpiHeight
059dpiWidth
060format
061formatOptions
062hasAlpha
063path
064pixelHeight
065pixelWidth
066profile
067samplesPerPixel
068space
069typeIdentifier
070*)
AppleScriptで生成しました