[Automator & AppleScript] Automator: 入力が画像ファイルかを判別する

拡張子をみるか、file コマンドの返値を使うか。

[markdown]
## 画像ファイルの情報を確認する

`info for` で返ってくる値を確認してみる。
拡張子あり。

“`prettyprinted
{name:”dialog.png”, creation date:date “2014年2月13日木曜日 0:02:49”, modification date:date “2014年2月13日木曜日 0:02:50″, size:46570, folder:false, alias:false, package folder:false, visible:true, extension hidden:false, name extension:”png”, displayed name:”dialog.png”, default application:alias “Macintosh HD:Applications:Preview.app:”, kind:”PNG イメージ”, file type:””, file creator:””, type identifier:”public.png”, locked:false, busy status:false, short version:””, long version:””}
“`

拡張子なし。

“`prettyprinted
{name:”desktop”, creation date:date “2014年2月12日水曜日 15:44:35”, modification date:date “2014年2月12日水曜日 15:44:35″, size:571035, folder:false, alias:false, package folder:false, visible:true, extension hidden:false, name extension:missing value, displayed name:”desktop”, default application:alias “Macintosh HD:Applications:TextEdit.app:”, kind:”書類”, file type:””, file creator:””, type identifier:missing value, locked:false, busy status:false, short version:””, long version:””}
“`

## 拡張子を利用する

試さなかったけれども参考ソース。

> * [MacScripter / Moving files out of subfolders based on extension](http://macscripter.net/viewtopic.php?id=34633)

`whose file type is` や `whose name extension is` のような形で判別しているよう。

> * [itunes – Applescript selecting files of a certain extension – Stack Overflow](http://stackoverflow.com/questions/3845623/applescript-selecting-files-of-a-certain-extension)
> * [Use applescript automatic copy Specified files from new added Mobile hard disk to mac – MacRumors Forums](http://forums.macrumors.com/showthread.php?t=1545240)

`.jpg` とか `.jpeg` とか付ける方や、拡張子なしの方とかもいたような気がするし、いろいろ考えて見送りました。

> * [applescript – Automator check file type – Stack Overflow](http://stackoverflow.com/questions/8104816/automator-check-file-type)

## file コマンドを利用する

`file –mime-type -br` で `image/png` といった値が返ってくる。

こちらを参考にさせて頂きました。

> * [AppleScript Folder Action to add missing image extensions on download – The Pug Automatic](http://thepugautomatic.com/2008/11/extension-action/)

対応したい画像の MIME Type を列挙して比較する。

“`prettyprinted
on isImagefile(someItem)
set filePosixPath to quoted form of (POSIX path of (someItem as alias))
set fileType to (do shell script “file –mime-type -br ” & filePosixPath)
set validFileType to {“image/png”, “image/jpeg”, “image/gif”}
if fileType is in validFileType then return true
return false
end isImagefile
“`

## 補遺

> * [AppleScript: Image Events](http://www.macosxautomation.com/applescript/imageevents/08.html)
[/markdown]