Is a file an image?

David Bovill david at architex.tv
Tue Jan 26 15:19:00 EST 2010


I've just dug out this very old and pretty untested script for detecting
whether a file is an image. Seems to sort of work on OSX - included here for
insiration :) Does anyone know a better way to do this?

function file_GetType filePath
>    switch the platform
>       case "MacOS"
>          put spotlight_Kind (filePath) into fType
>          return word -1 of fType
>       case "Win32"
>          put offset(".",filePath) into tChar
>          if tChar <> 0 then
>             put char tChar to length(filePath) of filePath into text
>             put
> queryregistry("HKEY_CLASSES_ROOT\"&(queryregistry("HKEY_CLASSES_ROOT\"&tExt&
> "\")) & "\") into tType
>          else
>             put "" into tType
>             put "" into text
>          end if
>          if tType = "" then
>             if text <> "" then
>                delete char 1 of text  -- remove "."
>                put toupper(tExt) && "File" into tType
>             else
>                put "File" into tType
>             end if
>          end if
>          return filePath & numtochar(9) & tType
>       default
>          return empty
>    end switch
> end file_GetType
>

function spotlight_Kind someFile
>    put spotlight_GetAttribute(someFile, "kMDItemKind") into someKind
>    put word 1 to - 1 of someKind into someKind
>    delete char 1 of someKind
>    delete char -1 of someKind
>    return someKind
> end spotlight_Kind
>
> function spotlight_GetAttribute someFile, attributeName
>     put spotlight_FileInfo(someFile) into spotlightFileInfo
>     return spotlight_ExtractAttribute(spotlightFileInfo, attributeName)
> end spotlight_GetAttribute
>
> function spotlight_FileInfo someFile
>     put "mdls" into someShell
>     if someFile is not empty then
>         -- shell_BashParamEscape someFile
>         put space & someFile after someShell
>     end if
>     get shell(someShell)
>     return line 2 to -1 of it
> end spotlight_FileInfo
>
> function spotlight_ExtractAttribute spotlightFileInfo, attributeName
>     if char 1 to 7 of attributeName is not "kMDItem" then put "kMDItem"
> before attributeName
>     put "(?im)" &  attributeName & " += +(.*)" into someReg
>     if matchtext(spotlightFileInfo, someReg, someValue) is true then
>         return someValue
>     else
>         return empty
>     end if
> end spotlight_ExtractAttribute
>



More information about the use-livecode mailing list