[AppleScript] NSImageRep+OBJCで画像のピクセルサイズと解像度の取得
【スクリプトエディタで開く】 |
ApplescriptOCで画像のピクセルサイズと解像度.scpt.scpt | ソース |
|---|
| 001 | #!/usr/bin/env osascript |
| 002 | #coding: utf-8 |
| 003 | ----+----1----+----2----+----3----+----4----+----5----+----6----+----7-- |
| 004 | (* |
| 005 | pxサイズとptサイズの両方を取得できる |
| 006 | NSImageRepを使います |
| 007 | 好みでない場合は |
| 008 | NSImage経由でも取得出来ます |
| 009 |
|
| 010 |
|
| 011 | com.cocolog-nifty.quicktimer.icefloe *) |
| 012 | ----+----1----+----2----+----3----+----4----+----5----+----6----+----7-- |
| 013 | use AppleScript version "2.8" |
| 014 | use framework "Foundation" |
| 015 | use framework "AppKit" |
| 016 | use framework "UniformTypeIdentifiers" |
| 017 | use scripting additions |
| 018 |
|
| 019 | property refMe : a reference to current application |
| 020 |
|
| 021 | set listUTI to {"public.image"} as list |
| 022 | try |
| 023 | tell application "SystemUIServer" |
| 024 | activate |
| 025 | set aliasFilePath to (choose file of type listUTI without multiple selections allowed) as alias |
| 026 | end tell |
| 027 | on error strErrMes number numErrNo |
| 028 | log strErrMes & numErrNo |
| 029 | return false |
| 030 | end try |
| 031 |
|
| 032 | #UNIXパスからNSURL |
| 033 | set strFilePath to (POSIX path of aliasFilePath) as text |
| 034 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
| 035 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
| 036 | set ocidFilePath to ocidFilePath's stringByExpandingTildeInPath() |
| 037 | set ocidFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false) |
| 038 | #NSImageRepはptサイズ pxサイズ両方持っています |
| 039 | set ocidReadImage to refMe's NSImageRep's imageRepWithContentsOfURL:(ocidFilePathURL) |
| 040 | set ocidImageSize to ocidReadImage's |size|() |
| 041 | set numWpt to ocidImageSize's width() |
| 042 | set numHpt to ocidImageSize's height() |
| 043 | set numWpx to ocidReadImage's pixelsWide() |
| 044 | set numHpx to ocidReadImage's pixelsHigh() |
| 045 | #解像度は計算して求めます |
| 046 | set numPPI to (72 * (numWpx / numWpt)) as integer |
| 047 |
|
| 048 | log numWpx as integer |
| 049 | log numHpx as integer |
| 050 | log numPPI as integer |
| AppleScriptで生成しました |
|---|