Convert JPGs to a PDF on a Mac from the Terminal or the Services Menu

Lots of tasks in daily IT repeat themselves and are terribly boring. One of those tasks for me was converting jpgs (of a scanned magazine) to pdfs. You could of course do this with Mac OS X’s Preview Application, but I’ll cover more comfortable and cool ways to do it with Terminal or the Finder‘s Services Menu.

First commands for the Shell

So let’s start with using the Terminal App. Put all the jpgs in one Folder and navigate to it (you can very easily do that with this hint).

Basic command

Once you are inside the Folder in Terminal type:
convert *.jpg myPdf.pdf
and you are done.

Let’s improve it a bit

Ok, so that was easy.
But I said it would become more comfortable.

So, we’ll extend that command a bit. Try:
convert *.JPG "$(basename "$(pwd)").pdf"
and you get a pdf that is named like the parent folder of your jpgs.

How does that work?

In the Shell, code inside the parentheses after a $-Sign is executed first.
$(pwd) returns the current working directory.
The basename command returns the filename of its input (coming from $(pwd)), which, in this case, is the name of the parent directory. The quotes are necessary for the handling of blanks.

The Step to Automator

So how do we get our nice little command out of the Terminal?
The solution is Mac OS X’s Automator.

Fire it up from the Application Directory and choose New Service.

Then, set the Service to receive Folders in Finder.

Add the “Run Shell Script” Automator Action.

Set the Shell to “/bin/sh” and the input pass to “as arguments”.
Then, post the following code into the action:

# Change directory to the Folder
cd “$1”;

# Save Parent Directory Name to variable pdf_name
pdf_name=$(/usr/bin/basename “$(/bin/pwd)”);

# Run convert command
/usr/local/bin/convert *.jpg “$pdf_name.pdf;

and save the Service under a name like “Create PDF from jpgs within”.

Now, right click on the folder with your jpgs, select the new Service and you will get your named PDF.

Tagged , , , , , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: