Re: Script to replace lost file extension
Re: Script to replace lost file extension
- Subject: Re: Script to replace lost file extension
- From: "Steven D. Majewski" <email@hidden>
- Date: Tue, 24 Apr 2007 10:40:13 -0400
On Apr 24, 2007, at 5:27 AM, Wayne Melrose wrote:
On 24/04/2007, at 11:14 AM, Simon, Garry wrote:
Hello, does anyone know if a script could be used to examine a
file that has no extension, and determine what its extension
should be? At my job we frequently receive files that have lost
their extensions, and need to restore them. There must be a shell
script that can determine what a file’s extension was.
Thanks,
Garry Simon
I'll speak for myself but.. . We deal with the same issues, files
can sometimes get damaged in transfer as it is. Sometimes even
correcting the file extension does not solve the problem. This is
where the 'ole right click (ctrl click) "Create archive ..."
instruction is always the best solution.. OK so it requires re-
sending a file on behalf of the sender, although it's good practice
anyway.
I know that
do shell script "head -1 /path/to/file"
will return the first line of file without opening the whole thing.
A pdf file with no extension will give you the result %????1.6
A PDF file always starts with %PDF1.6 so that might be a tip to
have a look at what else you can find. Or start a discussion on a
MUCH smarter way to go about this ;)
Wayne..
In *general*, this is not a solvable problem.
But, for a specific case, if you're willing to make some assumptions
for a best guess,
and handle it manually (or skip it) if there's no best guess, then OK.
You might start with the unix 'file' command.
That gives you a description of the file based on a file of 'magic
numbers' in /usr/share/file/magic
( see: 'man file' and 'man magic' )
For a PDF file, for example, that will give you:
$ file majewski.pdf
majewski.pdf: PDF document, version 1.3
If you do 'file -i' or 'file --mime' , it will output the mime type
instead of a text description:
$ file --mime majewski.pdf
majewski.pdf: application/pdf
Apache web-server has a file that maps mime-types and extensions in /
etc/httpd/mime.types .
So combining them into one command, you get:
$ grep "$( file -b -i majewski.pdf )" /etc/httpd/mime.types
application/pdf pdf
But if you're going to wrap this up into a Applescript or shell script,
you probably want to do each operation separately so that you can test
for not found on either list.
The 'magic' files are a best guess pattern matching: occasionally,
but not often, they are wrong. More occasionally, there are files types
that just aren't in the list and are tagged as generic data or text.
But you can extend both the magic and the mime.types files to add new
patterns that aren't in the standard distribution.
The fallback output for file types that don't match a more specific
pattern are:
'data' or 'ASCII text' ( or maybe text in some other encoding, if it
can identify it.)
or as mime types:
'application/octet-stream' or 'text/plain; charset=us-ascii'
You can also use getFileInfo (from the shell) or StandardAdditions's
'info for' (Applescript) to check if the file type is contained
in the file metadata -- though if these files are uploaded or emailed,
then that metadata does not likely exist.
-- Steve Majewski
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden