[AppleScript] SIPS+OBJCで画像のピクセルサイズと解像度の取得
【スクリプトエディタで開く】 |
sipsOCで画像のピクセルサイズと解像度.scpt.scpt | ソース |
|---|
| 001 | #!/usr/bin/env osascript |
| 002 | #coding: utf-8 |
| 003 | ----+----1----+----2----+----3----+----4----+----5----+----6----+----7-- |
| 004 | (* |
| 005 | SIPSの戻り値をPLISTにすることで |
| 006 | そのままDICT辞書として値を取得する方法 |
| 007 |
|
| 008 | ファイルにしなくてもPISTとして値を取得できるので |
| 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パスにしてSIPSを実行 |
| 033 | set strImageFilePath to (POSIX path of aliasFilePath) as text |
| 034 | set strCmd to ("/usr/bin/sips --getProperty allxml \"" & strImageFilePath & "\"") |
| 035 | set strStdOut to (do shell script strCmd) as text |
| 036 |
|
| 037 | ########### |
| 038 | #コマンドの戻り値をテキストにして |
| 039 | set ocidPlistString to refMe's NSString's stringWithString:(strStdOut) |
| 040 | #NSDATAに変換します |
| 041 | set ocidPlistData to ocidPlistString's dataUsingEncoding:(refMe's NSUTF8StringEncoding) |
| 042 | #NSPropertyListSerializationでDICTに変換します |
| 043 | set ocidOption to (refMe's NSPropertyListMutableContainersAndLeaves) |
| 044 | set ocidFormat to (refMe's NSPropertyListXMLFormat_v1_0) |
| 045 | set listResponse to (refMe's NSPropertyListSerialization's propertyListWithData:(ocidPlistData) options:(ocidOption) format:(ocidFormat) |error|:(reference)) |
| 046 | set ocidPlistDict to (first item of listResponse) |
| 047 | #DICTから値を取得する |
| 048 | set ocidWpx to ocidPlistDict's objectForKey:("pixelWidth") |
| 049 | set ocidHpx to ocidPlistDict's objectForKey:("pixelHeight") |
| 050 | set ocidPPI to ocidPlistDict's objectForKey:("dpiWidth") |
| 051 | log ocidWpx as integer |
| 052 | log ocidHpx as integer |
| 053 | log ocidPPI as integer |
| 054 |
|
| 055 | (* |
| 056 | SIPSのキー 画像から取得する時用 |
| 057 | bitsPerSample |
| 058 | dpiHeight |
| 059 | dpiWidth |
| 060 | format |
| 061 | formatOptions |
| 062 | hasAlpha |
| 063 | path |
| 064 | pixelHeight |
| 065 | pixelWidth |
| 066 | profile |
| 067 | samplesPerPixel |
| 068 | space |
| 069 | typeIdentifier |
| 070 | *) |
| AppleScriptで生成しました |
|---|