1

I've made an Automator service for extracting PDF text and saving it in the current directory, and it works reasonably well.

It takes one or several PDF files, extracts the text, and saves it as separate .rtfs placed in the same directory as where the original PDFs was found.
Fine, excellent, except for one small rub: what is invariably also saved alongside the other .rtfs is an empty one (zero bytes), with the name of the current directory.

Looking at the workflow below, it seems like both variables ("PDF" and "Bane") are passed to the action "Hent ut PDF-tekst". Is my assumption reasonable, and in any case, how do I fix it?

enter image description here

The script in plain text:

on run {input, parameters}
 tell application "System Events"
 set thePath to POSIX path of (container of (item 1 of input))
 end tell
 return thePath
end run

As an aside, Automator automatically creates a PNG representation of your workflow when you save it, and it can be easily got at by revealing the package contents.
Genius me realized this only after a bit of faffing about with screen capture and GIMP.

  • I guess you do not expect anyone who cannot read/understand Danish to help you! Also Snow Leopard is pretty old too which narrows even further those that can test in the same environment your using as "Hent ut PDF-tekst" ("Download PDF text" (if Google Translate is correct) doesn't exist in Automator in the later version of OS X I'm running. – user3439894 Jan 14 '16 at 18:38
  • For anyone familiar with Automator it shouldn't be too difficult to deduce the meaning of the various fields and windows. (and it's Norwegian, not that there's much difference). Surely I'm not the only SL-fogey left out here? – AkselA Jan 14 '16 at 18:48
  • Sorry it came up as Danish in Google Translate and the point I was trying to make is when asking for help in English while displaying images written in a language other then English you should translate what's on the image as well so as to help us help you! I translated using Google Translate however as "Hent ut PDF-tekst" doesn't exist in Automator in the version of OS X I'm using I can't test what you have to see where it might be going wrong. Although that said, have you reviewed the output under "Resultater" (Results) throughout for any clues? – user3439894 Jan 14 '16 at 19:00
  • Results show one error: "index (2) beyond bounds (2)". If that action doesn't exist in your version of Automator, how would you go about automating this task? – AkselA Jan 14 '16 at 19:22
1

Here is the issue I ran into with the translation of "Hent ut PDF-tekst" in Google Translate while translating from Norwegian to English, it translated as "Get the PDF text" so when I typed "Get" in the Actions Search Box it didn't show any PDF Actions with the "Get" Actions that did show. Upon further examination I found Extract PDF Text and used it.

I was able to replicate the issue of a zero sized RTF file being created along with the one for the actual PDF file however I was not able to debug it as to why and I did try many different things. Through the Workflow it showed Bane as a Folder being the path to the selected PDF File but then converted it to the zero sized RTF along with the proper RTF file and to me it looks like a bug in Automator.

That said, I present a workaround that you can choose to use if no one else has an answer that resolves the issue without resorting to this workaround.

Add a Run Shell Script Action to the end of the list of Actions, setting Shell: /bin/bash and Pass input: as argument with the following code:

for f in "$@"; do
    if [ ! -s "$f" ]; then
        rm "$f"
    fi
done

Which translates to: If this file does not have a size greater than zero, then delete it.

BTW If you want to test the code first, you can temporarily replace rm "$f" with something like say deleting "$f" so you can hear what file it's going to delete. When satisfied it's going to delete the zero sized file, then put it back to rm "$f".

Here is an image of my Automator Service.

Export PDF Text Automator Service

  • If it is a bug if must have been persisting for a whole decade now, I almost want to applaud it. Thank you for the bash fix, I didn't even think about solving it that way. – AkselA Jan 15 '16 at 2:44
  • @AkselA, The reason I say it's a bug is it appears that the Extract PDF Text Action isn't properly validating what's being passed to it, that it's assuming everything passed is a PDF file from which to extract the text. As a programer I'd have made sure that if one of the objects being passed was a Variable assigned to Save Output to: not to process it as a PDF file, which it unfortunately is not doing. As to being around for a long time, that's nothing... look how long Shellshock in Bash has been around, since Sept 1989 and not started to be patched until Sept 2014... 25 years! – user3439894 Jan 15 '16 at 17:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .