20260513

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

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

NOTE記事一覧ですnote.com
 

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

ApplescriptOCで画像のピクセルサイズと解像度.scpt.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
004(*
005pxサイズとptサイズの両方を取得できる
006NSImageRepを使います
007好みでない場合は
008NSImage経由でも取得出来ます
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パスからNSURL
033set strFilePath to (POSIX path of aliasFilePath) as text
034set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
035set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
036set ocidFilePath to ocidFilePath's stringByExpandingTildeInPath()
037set ocidFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false)
038#NSImageRepはptサイズ pxサイズ両方持っています
039set ocidReadImage to refMe's NSImageRep's imageRepWithContentsOfURL:(ocidFilePathURL)
040set ocidImageSize to ocidReadImage's |size|()
041set numWpt to ocidImageSize's width()
042set numHpt to ocidImageSize's height()
043set numWpx to ocidReadImage's pixelsWide()
044set numHpx to ocidReadImage's pixelsHigh()
045#解像度は計算して求めます
046set numPPI to (72 * (numWpx / numWpt)) as integer
047
048log numWpx as integer
049log numHpx as integer
050log numPPI as integer
AppleScriptで生成しました