How to detect a PDF without an extension?

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

How to detect a PDF without an extension? Sat Mar 14, 2020 7:12 pm • by chazzo
Can anyone suggest how Hazel might recognise a PDF file whose extension has been lost (and not just hidden)?

When I download PDFs that a server has generated on the fly, they quite often lack a ".pdf" extension. Bank statements are a typical example, but I've just seen the same problem for a product manual from a manufacturer's website. The file is a valid PDF, but my Mac assigns it the creator "TextEdit". Opening it in a text editor shows "%PDF-1.4" (or other version number) at the top of the file. When I add the ".pdf" extension manually, the default app becomes Preview. To repeat, a "Get Info" shows that the extension isn't hidden; it's simply missing.

Getting Hazel to add the extension automatically would be good, but I'm stuck. I can't look for the "%PDF" because both "Contents" and "Text content" appear to be blank when I inspect them within Hazel. And "Kind" is not only not "PDF", it's not even "Document".

For specific bad actors I can match "Source URL/Address" against a list of known URLs. But I'd like a generic solution. Any ideas?
chazzo
 
Posts: 6
Joined: Tue Oct 08, 2019 12:18 pm

Re: How to detect a PDF without an extension? Sun Mar 15, 2020 8:44 am • by chazzo
I think this shell script does the trick:

Code: Select all
mystring=$(head -c 5 $1)
if [ $mystring = "%PDF-" ]
then
  exit 0
else
  exit 1
fi


The basic problem is that Spotlight doesn't recognise such a file and can't read its contents.
chazzo
 
Posts: 6
Joined: Tue Oct 08, 2019 12:18 pm

Re: How to detect a PDF without an extension? Mon Apr 06, 2020 10:47 am • by Ni€ls
Hello,
I have the same problem that I have the extension *.pdf missing on some files.

Where and how do I have to use the Schelscript to make it work?

Code: Select all
mystring=$(head -c 5 $1)
if [ $mystring = "%PDF-" ]
then
  exit 0
else
  exit 1
fi


Can you show me an Hazel example, thanks.
Ni€ls
 
Posts: 5
Joined: Thu May 16, 2019 10:50 am

Re: How to detect a PDF without an extension? Tue Apr 07, 2020 10:34 am • by Mr_Noodle
You can paste the script into a Run ShellScript action.

One modification to the script I would recommend is putting double-quotes around the $1 argument ("$1").
Mr_Noodle
Site Admin
 
Posts: 8522
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support