Keyboard Maestro “Convert PDF Files into Text Files in the Front Finder Window” Macro


Edited 2022-08-21 to bring installation information up to date. -ccs


Hey Folks,

I've posted a number of times in various threads about the Xpdf command-line tools – particularly about the pdftotext tool.

Download Xpdf and XpdfReader

Look for the “Mac 64-bit” version in the “Download the Xpdf command line tools” section.

The utilities contained in the Xpdf downloadable binary are:

pdfdetach
pdffonts
pdfimages
pdfinfo
pdftohtml
pdftopng
pdftoppm
pdftops
pdftotext

Installation Instructions


Install the files in the bin64 folder here:

/usr/local/bin

Install the files in the doc folder here:

/usr/local/man/man5

The macOS hides these folders by default. To get there open a Finder window, type Cmd-Shift-G, and paste in the path you want to go to.

Then drag the requisite files into the correct folder.

If the necessary folders do not exist then create them. (The system will probably make you authenticate in order to do so.)


You may need to adjust your system PATH variable to use those folders, and keep in mind that doing so does NOT allow Keyboard Maestro see them. Keyboard Maestro has it's very own ENV_PATH variable (create as needed), or you can simply specify full paths in individual shell scripts.

(Specifying full paths in shell scripts is generally a bad idea, as there are many circumstances under which those can change. It's usually a much better idea to use KM's ENV_PATH variable.)


This macro uses the pdftotext tool to convert PDF files in the Front Finder Window to Text files.

The macro is non-destructive – all text files are saved alongside their original PDF in the source directory.

Convert PDF Files into Text Files in the Front Finder Window.kmmacros (11 KB)

It is possible to route the converted text to STDOUT instead of to a file:

pdftotext -layout <POSIX_Path_of_Your_File> -

The trailing hyphen is ESSENTIAL.

I've done the heavy-lifting in the shell, but it is easy enough to iterate through the file list with a Keyboard Maestro For Each PathVar in a Directory action and apply the basic shell command to <PathVar> instead.

-Chris

6 Likes

Chris,

I looked for the "Precompiled binaries" and don't see it. Is the above page still relevant? What I have are the files in the pic below. Is it safe to assume I can run make, make install?

Hey John,

Look in the bin64 folder. :wink:

-Chris

Also I recall you had me install this before so I checked for a man file and it appears I have xpdf (version 3.04) already installed. Can I just install over the top of my current installation?

I wasn't expecting a reply that fast. LOL!

2 Likes

So I tried the macro and nothing happens. When you say finder window, do I open the pdf in the front finder window or do I open the folder where the file is and select it? Neither seems to work.

Ok, never mind. It was added to the test group which was disabled. I got it to run.
I have a text file. It looks really good. I did get an error:

Shell Script
errorText:
xargs: unterminated quote

But since I have the text I need this will work.

Hey John,

This doesn't happen to me.

Send me a pic of the window you're running the macro on, so I can try to recreate the problem.

-Chris

Hey John,

Here's how to get the text of a single selected PDF file in the Finder into BBEdit.

** REQUIRES ** the same command line tool as the macro above.

-Chris


Finder -- Send Text of Selected PDF File to BBEdit.kmmacros (6.5 KB)

A couple of questions. Can I install v. 4.00 over my v. 3.04 version?

The install instructions say to copy xpdf, pdftotext, etc to /usr/local/bin I assume I can do:

cp /Users/zz/Downloads/xpdf-tools-mac-4.00/bin64/pdfdetach /usr/local/bin

Could I just do:

cp /Users/zz/Downloads/xpdf-tools-mac-4.00/bin64/* /usr/local/bin

I also seem to remember I had to do something to get it into my shell.

Hey John,

You don't need cp at all.

Just open a Finder window, type Cmd-Shift-G, and paste in the path you want to go to:

/usr/local/bin
/usr/local/man/man5

Then drag the requisite files into the correct folder.

The Finder will make you authenticate the action, but that's it.

Once you've copied the files you're good to go – UNLESS /usr/local/bin/is NOT in your $PATH.

You should not have a problem if pdftotext v3.0.4 was working for your, but if you do you can see this thread:

Create a PATH Environment Variable for Keyboard Maestro and Add /usr/local/bin to the Default Path

-Chris

I copied 4.0 version successfully, Thanks!

I put it into /usr/local/bin however the man folder appears to be missing from 'local'. Can I just create the folder(s)? (man, man5)

I am also wondering why it would be missing since I am obviously using man with other applications.

Hey John,

This sort of thing is easy enough to test yourself, but yes you can create those folders.

Because /usr/local/bin is a directory for USER-installed executables.

-Chris

Great tool!

1 Like

The script makes a txt file of every pdf in the current folder.
I would like to put the text of a (all) selected pdf file(s) into a KM variable.

How would the macro look like then? I know how to give the shell script a single file instead of a folder, but the shell script is expecting a folder and returns an error if i give it a single file instead.
How can i change this?

Hey @ccstone

A useful tool. Thank you very much for making it available!

1 Like

Thank you @ccstone for this.
Works perfectly!


I am a beginner with Keyboard Maestro, and while I can work the basic stuff, I don't really understand Apple Script / Shell Script (yet).

How can I modify the original KM Macro to save the results to a variable, rather than to a new text file?

I hope to parse some details out of the PDF, and use it to rename the PDF itself. I don't need the data for anything else.

Thank you.

Hey @Sinizter,

Read the last part of Post #1.

If you're still scratching your head let me know.

-Chris

@ccstone

Thank you.

I was scratching my head... But, I went and worked out what xargs was and what it was doing in this script, and read the documentation of xpdfreader and now what you posted makes complete sense.

I am going to try and make something that takes all selected PDFs from Finder window and rename them to my preferred naming convention based on contents.

1 Like

I'm scratching my head over this whole thing! I have done as directed, but am getting this error stream and nothing else:

Shell Script

errorText:

Config Error: Couldn't open 'nameToUnicode' file '/Users/wendydunning/xpdf/cyrillic/Bulgarian.nameToUnicode'

Config Error: Couldn't open 'nameToUnicode' file '/Users/wendydunning/xpdf/greek/Greek.nameToUnicode'

Config Error: Couldn't open 'nameToUnicode' file '/Users/wendydunning/xpdf/thai/Thai.nameToUnicode'

Config Error: Couldn't open 'nameToUnicode' file '/Users/wendydunning/xpdf/cyrillic/Bulgarian.nameToUnicode'

Config Error: Couldn't open 'nameToUnicode' file '/Users/wendydunning/xpdf/greek/Greek.nameToUnicode'

Config Error: Couldn't open 'nameToUnicode' file '/Users/wendydunning/xpdf/thai/Thai.nameToUnicode'

Config Error: Couldn't open 'nameToUnicode' file '/Users/wendydunning/xpdf/cyrillic/Bulgarian.nameToUnicode'

Config Error: Couldn't open 'nameToUnicode' file '/Users/wendydunning/xpdf/greek/Greek.nameToUnicode'

Config Error: Couldn't open 'nameToUnicode' file '/Users/wendydunning/xpdf/thai/Thai.nameToUnicode'

Config Error: Couldn't open 'nameToUnicode' file '/Users/wendydunning/xpdf/cyrillic/Bulgarian.nameToUnicode'

Config Error: Couldn't open 'nameToUnicode' file '/Users/wendydunning/xpdf/greek/Greek.nameToUnicode'

Config Error: Couldn't open 'nameToUnicode' file '/Users/wendydunning/xpdf/thai/Thai.nameToUnicode'

Config Error: Couldn't open 'nameToUnicode' file '/Users/wendydunning/xpdf/cyrillic/Bulgarian.nameToUnicode'

Config Error: Couldn't open 'nameToUnicode' file '/Users/wendydunning/xpdf/greek/Greek.nameToUnicode'

Config Error: Couldn't open 'nameToUnicode' file '/Users/wendydunning/xpdf/thai/Thai.nameToUnicode'

Config Error: Couldn't open 'nameToUnicode' file '/Users/wendydunning/xpdf/cyrillic/Bulgarian.nameToUnicode'

Config Error: Couldn't open 'nameToUnicode' file '/Users/wendydunning/xpdf/greek/Greek.nameToUnicode'

Config Error: Couldn't open 'nameToUnicode' file '/Users/wendydunning/xpdf/thai/Thai.nameToUnicode'

Config Error: Couldn't open 'nameToUnicode' file '/Users/wendydunning/xpdf/cyrillic/Bulgarian.nameToUnicode'

Config Error: Couldn't open 'nameToUnicode' file '/Users/wendydunning/xpdf/greek/Greek.nameToUnicode'

Config Error: Couldn't open 'nameToUnicode' file '/Users/wendydunning/xpdf/thai/Thai.nameToUnicode'

xargs: unterminated quote
Any and all suggestions would be welcome to this newbie!