20260513

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

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

NOTE記事一覧ですnote.com

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

sipsASで画像のピクセルサイズと解像度.scpt.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
004(*
005
006System Events と スクリプトエディタに『フルディスクアクセス』が必要です
007不安に思われる場合は、実行しないようにしましょう
008
009
010一旦PLISTファイルにしてしまう方法
011ファイルのIOが発生するけど
012複数回コマンド打つより、本当にチョットだけ速い
013
014
015
016com.cocolog-nifty.quicktimer.icefloe *)
017----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
018use AppleScript version "2.8"
019use scripting additions
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#このパスは『System Events.app』にフルディスクアクセス権が必要
033#temporary itemsは起動時に自動で削除されるフォルダです
034#ファイルは残らないので容量の心配はありません
035set aliasTempDirPath to (path to temporary items from user domain with folder creation) as alias
036set strTempDirPath to (POSIX path of aliasTempDirPath) as text
037set strTmpFilePath to ("" & strTempDirPath & "sips.plist") as text
038
039#コマンドにして
040set strImageFilePath to (POSIX path of aliasFilePath) as text
041set strCmd to ("/usr/bin/sips --getProperty allxml \"" & strImageFilePath & "\" > \"" & strTmpFilePath & "\"")
042set strStdOut to (do shell script strCmd) as text
043
044#コマンド作成されたPLISTを開いて値を取得する
045tell application "System Events"
046   tell property list file strTmpFilePath
047      set numWpx to (value of property list item "pixelWidth") as integer
048      set numHpx to (value of property list item "pixelHeight") as integer
049      set numPPI to (value of property list item "dpiWidth") as integer
050   end tell
051end tell
052
053
054(*
055SIPSのキー 画像から取得する時用
056bitsPerSample
057dpiHeight
058dpiWidth
059format
060formatOptions
061hasAlpha
062path
063pixelHeight
064pixelWidth
065profile
066samplesPerPixel
067space
068typeIdentifier
069*)
AppleScriptで生成しました