20260513

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

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

NOTE記事一覧ですnote.com

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

imagesEventASで画像のピクセルサイズと解像度.scpt.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
004(*
005
006昔ながらのImage Eventsを利用する方法
007パスの渡し方に少しコツがある-->コメント読んでね
008
009
010
011com.cocolog-nifty.quicktimer.icefloe *)
012----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
013use AppleScript version "2.8"
014use scripting additions
015
016#ダイアログ
017set listUTI to {"public.image"} as list
018try
019   tell application "SystemUIServer"
020      activate
021      set aliasFilePath to (choose file of type listUTI without multiple selections allowed) as alias
022   end tell
023on error strErrMes number numErrNo
024   log strErrMes & numErrNo
025   return false
026end try
027
028
029#Image Eventsで処理する場合はパスかエイリアスリストにしておく
030tell application "Finder"
031   set listAliasFilePath to {aliasFilePath} as alias list
032end tell
033
034
035tell application "Image Events"
036   #必ずTRYを入れるようにする
037   try
038      launch
039      --> aliasの場合listの1部として渡すと開くことが出来る
040      set imageReadData to open (first item of listAliasFilePath)
041      set listResolution to (resolution of imageReadData) as list
042      set listDimensions to (dimensions of imageReadData) as list
043      set numPPI to (first item of listResolution) as integer
044      set strWpx to (first item of listDimensions) as text
045      set strHpx to (last item of listDimensions) as text
046      set strColorSpace to (color space of imageReadData) as text
047      set strFileType to (file type of imageReadData) as text
048      set strFileName to (name of imageReadData) as text
049      #OPENしたファイルは必ずクローズする
050      close imageReadData
051      #ループ処理の場合は最後で終了させるでもOKですが
052      #明示的に解放してあげるのが良いでしょう
053      quit
054   on error strErrMsg number numErrNo
055      log "No: " & numErrNo & linefeed & "Error:" & strErrMsg
056      #TRYを入れるのは、失敗した時にCLOSE出来なくなるからです
057      #すなわち 開きっぱなしになる
058      #OPENしたファイルは必ずクローズする
059      close imageReadData
060      quit
061      return false
062   end try
063end tell
064
065log strWpx
066log strHpx
067log numPPI
AppleScriptで生成しました