Getting file type via 'do shell script'...

Hi,

I’d like t know if there is a Unix command I can implement via ‘do shell script’ that can tell me the file type of a file (like tiff, txt, jpg, etc…). The man files are pretty daunting to sift through, so I thought I’d ask here first.

I know I can check for file type with the Finder, but, well, I don’t wanna. :evil:

Thanks.

:!: When you say “file type” you are referring to a four character string in Mac. File types are
TEXT, "PDF ", 8BPS (Photoshop), JPEG, MPEG.

“tiff, txt, jpg” are referred to as PC Extensions in pre-OS X and are not properties of files; you won’t be able to access them because they are not properties.

“tiff, txt, jpg” in OS X are called “name extensions”, and are a property of the “info for” command.


name extension of (info for whateverfile_.ext)

But that’s from the finder :twisted:

Yes. Pardon my lazy typing :oops:

I know I can do this with the Finder, but I’ve been on a mission lately to do as much as I can via ‘do shell script,’ because I tend to end up with less code, and many steps get performed faster, especially anything that has to do with file management, searching, etc…

Thanks again.

Alright, I got it. I should have known to check sips in the first place, at least for image files.


do shell script "sips -g format /volumes/path/to/MyFile.tif/

There is a UNIX command called “file” that tries to determine many standard file formats. Try:


do shell script "file " & quoted form of somePosixPath

Now, if you can guarantee that whoever uses this script has Developer Tools installed, you can use GetFileInfo to get more Macintosh info about the file:


do shell script "GetFileInfo " & quoted form of somePosixPath

This returns the Macintosh type, creator, attributes, time-stamp created, and time-stamp modified. But, wherever you run this, you need Developer Tools (or at least GetFileInfo) installed.

Info For is part the of Standard Additions. Does it really use the Finder?

As for Carl’s question, try this:

set theFilename to "test.file.txt"

-- This one uses tr
do shell script "basename `echo " & quoted form of theFilename & " | tr '.' '/'`"

-- This one uses sed
do shell script "basename `echo " & quoted form of theFilename & " | sed 's_\\._/_g'`"

Not as far as I’m aware, although there’s a certain amount of crossover between the properties returned by ‘info for’ and those returned by Finder and System Events. Any one of these can be used to get a file extension. In addition, Finder and ‘info for’ should be able to supply a file type.

(Incidentally, I would question whether a shell script would actually be the fastest method in this type of situation - as was suggested earlier. Getting such information will involve an external call, whether it be to the shell, a scripting addition or an application. However, in my tests, Finder generally returns a file extension or file type faster.)