Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

An AppleScript for combining multi-page PDFs Apps
Last week simdude was showing how to combine multiple PDFs into one document using Mail. You can carry out the same action in Appleworks and Microsoft Word. The trouble is that only single page docs can be dealt with in this way. A bit laborious! And the process can't deal with multi-page docs.

As was explained on the same thread, there is a utility called joinPDF that will handle any number of files to be joined together. The trouble with this util is that it is command line only, and unwieldy for a large number of small size files. There is a bolt-on GUI (joinPDF GUI) available, and this works well but only allows two files at a time to be stuck together. I scan a lot of documents, and have a lot of single- and two-page PDFs to stick together, so I decided to write an Applescript droplet to automate the process, using joinPDF to do the actual sticking together. It's not pretty, but it will do until the author of the GUI can be persuaded to allow multiple file selections to be made.

[robg adds: Rather than pasting the somewhat long AppleScript here, I made it available for download from macosxhints: CombinePDF.sit.]

Select the appropriate files and drop onto the script icon. The script requests an output file name, checks to see if it exists, declares an error if it does, and gives the user the opportunity to enter another name. Because the command line uses UNIX syntax, much of the script is concerned with converting from MAC OS X format to Unix. Potentially a large number of file names might need converting, so there are subroutines to handle the necessary conversions. I've tried to write these to be reusable as possible. Terminal is used to send the assembled command line script to UNIX. For some reason, I could not get Terminal to exit normally. So the script does not quit Terminal, and any user will have to quit Terminal themselves. Any advice on how to deal with this gratefully received.

Best to place the files to be stuck together in the same directory. Select the files in the order in which they should be joined, and the script gets it right. Finally, the joinPDF routine runs quite slowly for big files, so don't assume that the routine has hung up. Give it a chance!
    •    
  • Currently 1.67 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[16,296 views]  

An AppleScript for combining multi-page PDFs | 18 comments | Create New Account
Click here to return to the 'An AppleScript for combining multi-page PDFs' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Shorter script
Authored by: Darkshadow on Jun 04, '03 11:06:07AM
Here, the same script done a little easier. With the save to place defaulting to the user's desktop for free. :)

on open theItems
	set userDesktop to the path to desktop from user domain as text
	tell application "Finder"
		repeat
			display dialog "Enter output file name" default answer userDesktop & "untitled.pdf"
			set rawdata to text returned of the result
			exists file rawdata
			if result = true then
				display dialog "File already exists. Enter different file name."
			else
				exit repeat
			end if
		end repeat
	end tell
	set RawScript to "joinPDF" & space & (the POSIX path of rawdata)
	repeat with aFile in theItems
		set RawScript to RawScript & space & (the POSIX path of aFile)
	end repeat
	do shell script RawScript
end open


This should take a little less time to operate (of course, depends on how fast or slow joinPDF runs). Also, it won't start up Terminal to do the shell script. ;)

[ Reply to This | # ]
Shorter script
Authored by: Darkshadow on Jun 04, '03 11:14:52AM
Oops, little goof, I forgot to add the apostrophes. Change the two lines above to:

set RawScript to "joinPDF" & "\"" & (the POSIX path of rawdata) & "\""
....
set RawScript to RawScript & "\"" & (the POSIX path of aFile) & "\""


...sorry 'bout that one!
(okay, okay, so I didn't use apostrophes, I used quotes...does the same thing :D )

[ Reply to This | # ]
Shorter script
Authored by: Darkshadow on Jun 04, '03 11:24:02AM
Grrr...forgot about the slash thing...you need to do those quotes as:

"'"

(okay, so this time I used apostrophes :p )

[ Reply to This | # ]
An AppleScript for combining multi-page PDFs
Authored by: Dave Creek on Jun 04, '03 01:13:01PM

I got lost with your script corrections. Could you please post the full corrected script? Thanks.



[ Reply to This | # ]
An AppleScript for combining multi-page PDFs
Authored by: paulrob on Jun 04, '03 02:16:25PM

Arggh!

I had rather hoped that my original post wouldn't make it to the big screen. As you might have guessed I'm still learning Applescript. I'm a bit red faced!

I have now "discovered" Scripting Additions as used by Dark Shadow. And got rid of the first clunky loop too! The combined PaulRob/Dark Shadow script is: -

[Code]
on open (theItems)
set userDesktop to the path to desktop from user domain as text
set output_name to (choose file name with prompt
"Enter output filename" default name userDesktop & "untitled.pdf") as string
set UnixScript to "joinPDF " & "'" & (POSIX path of output_name) & "'"
repeat with aFile in theItems
set UnixScript to UnixScript & space & "'" & (POSIX path of aFile) & "'"
end repeat
do shell script UnixScript
end open
[/Code]

TTFN



[ Reply to This | # ]
An AppleScript for combining multi-page PDFs
Authored by: mr. applescript on Jun 04, '03 02:41:38PM

If you want to quote the POSIX path use the parameter "quoted form" as in this example:

quoted form of POSIX path of (choose file name)

-- returns: "'/Users/yourname/Desktop/myfile.pdf'"

The "quoted form" parameter adds the single quotes for you. The Apple TechNote for the Do Shell Script command is here:

<http://developer.apple.com/technotes/tn2002/tn2065.html>



[ Reply to This | # ]
An AppleScript for combining multi-page PDFs
Authored by: mbenchoff on Jun 04, '03 03:44:33PM

Funny...I was just beginning to write the exact same script...I'm glad that somebody beat me too it...

The (new) script works great, except for one problem. I had 4 PDFs, named 1.pdf, 2.pdf, 3.pdf, & 4.pdf, and in the resulting PDF (all.pdf) the order was 4-1-2-3. When I originally tried joinPDF, the resulting PDF was in the correct order. Anyone know what's going on here? Thanks.



[ Reply to This | # ]
An AppleScript for combining multi-page PDFs
Authored by: paulrob on Jun 04, '03 06:18:08PM

I've not had any problems with PDFs being out of order - although if I inadvertently selected the files in the wrong order, the final PDF was out of order.

The dropped files are processed in the order they are selected.

Did you select 4.pdf first by accident?



[ Reply to This | # ]
An AppleScript for combining multi-page PDFs
Authored by: mbenchoff on Jun 05, '03 10:40:59AM

Interesting...if I select the originals in order (1.pdf, 2.pdf, 3.pdf, 4.pdf) everything works out fine. However, if I just "Select All" the order is different. Most peculiar.



[ Reply to This | # ]
An AppleScript for combining multi-page PDFs
Authored by: Evil B on Jun 14, '03 07:40:52AM

that's a finder thing, the same thing happens when selecting images for opening in image viewer ...



[ Reply to This | # ]
An AppleScript for combining multi-page PDFs
Authored by: jijin on Jun 05, '03 12:58:25PM

Help
I am having the same problems with the page sorting as well
I command click them in the order that I want them but then they all seem to just get jumbled up again
am I doing anything wrong?
thanks



[ Reply to This | # ]
An AppleScript for combining multi-page PDFs
Authored by: paulrob on Jun 05, '03 06:37:34PM

Oh-oh! It's humble pie time! Sorry all but I have discovered that I too am having problems with more than 2 files. I tested this routine with 3 files originally and had no problems. It worked once - so I thought why wouldn't it work properly twice. Never gave it another thought. But now ....

It would seem that the problem is something to do with the way the system sees the files rather than the order you select them in. When I wrote the script, the way the system sees the files and the way I selected them coincided so I didn't have a problem - but that wasn't by design. And right now, I have four files on my desktop that seem to behave and assemble in the right order - but if I change the viewing options - move the files between folders - or sneeze too loudly (joke - just) the order goes to pieces.

I'm still trying to work it out. I can't believe it's a random problem - doesn't make sense. There must be some logic here. I would guess that any application that relies on the order of the drag'n'dropped files will have this problem. But its 12:00 am this side of "the Pond" and I need some kip! TTFN.



[ Reply to This | # ]
Combining PDF's from multiple sources (flexible)
Authored by: maartensneep on Jun 04, '03 06:07:15PM

If you need more control on selecting pages, scaling, the order of pages, etc, and you have latex installed, I would advice you to take a look at the pdfpages package.

Example: you have four pdf-files (1.pdf, 2.pdf, 3.pdf and 4.pdf), each with about a dozen pages. You would like to copy page 5 from 4.pdf followed by pages 4 through 8 and 12 through 14 of 2.pdf, and close with page 1 of 3.pdf and 1.pdf. Here's how to do it:

\documentclass[letter]{article}
\usepackage{pdfpages}
\begin{document}
\includepdf[pages=5]{4.pdf}
\includepdf[pages={4-8,12-14}]{2.pdf}
\includepdf[pages=1]{3.pdf}
\includepdf[pages=1]{1.pdf}
\end{document}

Of course you have all the tools available with LaTeX, and downloading ~60MB just to do this is overkill, but if you already have it, this is probably the most flexible method.

Maarten



[ Reply to This | # ]
Combining PDF's from multiple sources (flexible)
Authored by: maartensneep on Jun 04, '03 06:10:16PM

Oops, discovered a typo (despite the preview). The commands in my previous comment need to be prepended with a backslash (\): \begin{...}, \pdgpages[...]{...}

Sorry

Maarten



[ Reply to This | # ]
HELP using this...
Authored by: inblosam on Jun 14, '03 07:29:40AM

I think I have the script right, but I can't even seem to get it to work! I have the AppleScript icon in the top dock near the clock (nice rhyme). I try to click on the script name there, and nothing. I can't drag anything there either. What am I doing wrong? I am somewhat new to this applescript thing. Is this right for putting together the new script:

<code>
on open theItems
set userDesktop to the path to desktop from user domain as text
tell application "Finder"
repeat
display dialog "Enter output file name" default answer userDesktop & "untitled.pdf"
set rawdata to text returned of the result
exists file rawdata
if result = true then
display dialog "File already exists. Enter different file name."
else
exit repeat
end if
end repeat
end tell

set RawScript to "joinPDF" & "'" & (the POSIX path of rawdata) & "'"
repeat with aFile in theItems
set RawScript to RawScript & "'" & (the POSIX path of aFile) & "'"
end repeat
do shell script RawScript
end open


on right_str(data_str, char)
(* this user defined routine returns a string that is part of the input less
the section up to the first ocurance of char*)
set trip to false
set output_str to ""
repeat with aCharacter in data_str
set letter to aCharacter as string
(* make same datatype as char*)
if char = letter then
set trip to true
(* means that first occurance of char has been found*)
end if
if trip = true then
set output_str to output_str & letter
(* if char has been found then copy input str to output str
character by character*)
end if
end repeat
return output_str
end right_str

on FindnReplace(data_str, findchar, replacechar)
set processed_str to ""
repeat with aCharacter in data_str
set letter to aCharacter as string
(*make same datatype as findchar*)
if letter = findchar then
(*if true then this character is to be replaced. copy replacechar
to output str.
if false then this character is to be retained. copy character to
output str*)
set processed_str to processed_str & replacechar
else
set processed_str to processed_str & letter
end if
end repeat
return processed_str
end FindnReplace
</code>

PLEASE help!



[ Reply to This | # ]
An AppleScript for combining multi-page PDFs
Authored by: qetipadgu on Jul 19, '03 11:24:52PM

I would like to post a shameless plug for my PDFJoiner application. It was started before the GUI was included with joinPDF, and uses it for the actual combination. You will need to make sure you install joinPDF first.

It has drag and drop support (multiple PDFs at a time, but no folders), thumbnail page-selectable viewing of the selected PDF, and drag and drop reordering. It is a mere 80k download in addition to the joinPDF application (180 uncompressed).

The source code is availible from two versions ago, and I will post the updated source when I clean up the progress from the last couple days.

http://qetipadgu.org/software



[ Reply to This | # ]
An AppleScript for combining multi-page PDFs
Authored by: qetipadgu on Jul 21, '03 01:51:11PM

In all fairness, you might also want to take a look at the Combine PDFs application. It is around 800k, which is smaller than joinPDF and PDFJoiner. I believe it also supports drag and drop, and people say it does combine them at an acceptable speed.

I still prefer PDFJoiner, though.... :-P



[ Reply to This | # ]
Command line advantages
Authored by: Kool on Oct 13, '04 07:45:32AM

For those who have problems with order, why not just use the command line? Just type "joinPDF result.pdf "<- there is a space after the f, and drag 'n drop the files in the order you want them on the Terminal window and hit enter when done.

C'est tout!



[ Reply to This | # ]