[AppleScript] swift+ASで画像のピクセルサイズと解像度の取得
【スクリプトエディタで開く】 |
swiftASで画像のピクセルサイズと解像度.scpt.scpt | ソース |
|---|
| 001 | #!/usr/bin/env osascript |
| 002 | #coding: utf-8 |
| 003 | ----+----1----+----2----+----3----+----4----+----5----+----6----+----7-- |
| 004 | (* |
| 005 | swiftを利用して |
| 006 | 画像のサイズを取得する変種 |
| 007 | 画像のサイズの取得って意味では無意味だけど |
| 008 | スクリプト内にswift使えるようになると、出来ることの幅が広がります |
| 009 |
|
| 010 |
|
| 011 | com.cocolog-nifty.quicktimer.icefloe *) |
| 012 | ----+----1----+----2----+----3----+----4----+----5----+----6----+----7-- |
| 013 | use AppleScript version "2.8" |
| 014 | use scripting additions |
| 015 |
|
| 016 | #ダイアログ |
| 017 | set listUTI to {"public.image"} as list |
| 018 | try |
| 019 | tell application "SystemUIServer" |
| 020 | activate |
| 021 | set aliasFilePath to (choose file of type listUTI without multiple selections allowed) as alias |
| 022 | end tell |
| 023 | on error strErrMes number numErrNo |
| 024 | log strErrMes & numErrNo |
| 025 | return false |
| 026 | end try |
| 027 |
|
| 028 | #UNIXパスに変換 |
| 029 | set strImageFilePath to (POSIX path of aliasFilePath) as text |
| 030 | #SWIFTにパスを入れて |
| 031 | set strSwiftCode to ("#!/usr/bin/env swift\n//coding: utf-8\nimport Foundation\nimport AppKit\nimport ImageIO\nlet strPath = \"" & strImageFilePath & "\"\nlet urlPath = URL(fileURLWithPath: strPath)\nguard let cgImageRead = CGImageSourceCreateWithURL(urlPath as CFURL, nil) else {exit(1)}\nguard let rawProperties = CGImageSourceCopyPropertiesAtIndex(cgImageRead, 0, nil), let dictProperties = rawProperties as? [String: Any] else {exit(1)}\nlet strWpx = dictProperties[kCGImagePropertyPixelWidth as String] ?? 0\nlet strHpx = dictProperties[kCGImagePropertyPixelHeight as String] ?? 0\nlet strWppi = dictProperties[kCGImagePropertyDPIWidth as String] ?? 72\nprint(\"\\(strWpx)\\t\\(strHpx)\\t\\(strWppi)\")") as text |
| 032 | #実行する |
| 033 | set strCmd to ("/bin/echo '" & strSwiftCode & "' | /usr/bin/swift - ") |
| 034 | set strStdOut to (do shell script strCmd) as text |
| 035 |
|
| 036 | #戻り値をタブ区切りにしたのでタブでリストにして |
| 037 | set strDelim to AppleScript's text item delimiters |
| 038 | set AppleScript's text item delimiters to tab |
| 039 | set listImageSize to (every text item of strStdOut) as list |
| 040 | set AppleScript's text item delimiters to strDelim |
| 041 | #リストの順番であたいを取得する |
| 042 | set strWpx to (first item of listImageSize) as text |
| 043 | set strHpx to (second item of listImageSize) as text |
| 044 | set strPPI to (last item of listImageSize) as text |
| 045 |
|
| 046 |
|
| 047 | log strWpx as integer |
| 048 | log strHpx as integer |
| 049 | log strPPI as integer |
| AppleScriptで生成しました |
|---|