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

Click here to return to the 'Use Automator for manipulating Scanned PDFs' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Use Automator for manipulating Scanned PDFs
Authored by: promo1 on May 18, '12 09:49:11PM

I had a similar problem, but had the scanner make sets of PDFs each with multiples pages in it.
The solution posted here didn't work, so I eventually ended up writing an applescript that *uses PDFtk* (you need to download that that does the following
1- ask for files containing odd pages
2- ask file files containing even pages
3- ask for output filename (defaults to a randomly named file in the same folder as the first odd page file)
4- interleaves the 1st odd pages file with the *reverse of* the 1st even pages file
5- repeats step 4 for the number of files provided (must be the same number of odds and evens) and combines in to one output file
6 - deletes temporary files and opens the new PDF

Obviously you can use PDFtk via the command to do the same thing, this is just a lot easier...
Here's the script - it works well, but is not very robust in terms of error checking or anything like that and is kind of a hack.
----------cut and paste everything below here in to applescript and save----------
-- courtesy of the Beanlander
-- requires download of pdftk, available from on 18 May 2012

on run {}
set PDFtk to "/usr/local/bin/pdftk"
set PDFext to ".pdf"

-- get odd files
set files_OddPages to choose file of type {"PDF ", missing value, "PDF Document"} with prompt "Choose files containing odd numbered pages" with multiple selections allowed

-- get even files
set files_EvenPages to choose file of type {"PDF ", missing value, "PDF Document"} with prompt "Choose files containing even numbered pages (to be reversed)" with multiple selections allowed

-- check the same number of odd and even files
if (count of files_OddPages) ≠ (count of files_EvenPages) then
display dialog "Error - different number of odd and even files"
end if

-- create a temporary folder for files
tell application "Finder" to set tmpFolderLocation to container of item 1 of files_OddPages
set tmpFolder to createTempFileDirectory(tmpFolderLocation, createRandomUniqueFilename())

-- set output filename / location
set outputFile to getOutputFile(tmpFolderLocation as alias, createRandomUniqueFilename() & PDFext)

set tmpFiles to {}
repeat with i from 1 to count of files_OddPages

-- create tempfile name
if (count of files_OddPages) = 1 then
set pdftkScript to (POSIX path of outputFile)
set newTempFile to createTempFile(tmpFolder, createRandomUniqueFilename(), PDFext)
set end of tmpFiles to newTempFile
set pdftkScript to newTempFile
end if

set pdftkScript to PDFtk & " A=" & (POSIX path of item i of files_OddPages) & " B=" & (POSIX path of item i of files_EvenPages) & " shuffle A Bend-1 output " & pdftkScript

do shell script pdftkScript
end repeat

if (count of tmpFiles) > 0 then
-- combine the temporary files in to one
set fileHandles to ""
set operations to "cat "
repeat with i from 1 to count of tmpFiles
set fileHandles to fileHandles & (item i of alphabet) & "=" & (item i of tmpFiles) & " "
set operations to operations & (item i of alphabet) & " "
end repeat

set pdftkScript to PDFtk & " " & fileHandles & " " & operations & " output " & (POSIX path of outputFile)

do shell script pdftkScript
end if

-- erase the temporary files

-- open neew PDF
tell application "Finder" to open outputFile

end run

on createTempFile(theFolder, filebaseName, fileExt)

set newFilename to (POSIX path of theFolder as text) & filebaseName & fileExt

return newFilename
end createTempFile

on getOutputFile(defaultLocation, defaultFilename)

tell application "Finder"
set myPrompt to "Enter output filename"
--set defaultLocation to path to desktop
set outputFile to choose file name with prompt myPrompt default name defaultFilename default location defaultLocation
end tell

return outputFile
end getOutputFile

on eraseTempFileDirectory(tmpFolder)
tell application "Finder"
delete tmpFolder
do shell script "rm -rf ~/.Trash/\"" & name of tmpFolder & "\""

end try
end tell
end eraseTempFileDirectory

on createTempFileDirectory(baseFolder, folderName)
tell application "Finder"
if not (exists folder folderName of baseFolder) then
make new folder at baseFolder with properties {name:folderName}
end if
set newFolder to folder folderName of baseFolder as alias
end tell
return newFolder
end createTempFileDirectory

on createRandomUniqueFilename()
set randomCharacterList to {}
repeat with i from 1 to 8
set end of randomCharacterList to (some item of (every character of "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890"))
end repeat

return (randomCharacterList as text) & getDateString(current date)

end createRandomUniqueFilename

on getDateString(theDate)

set {year:y, month:m, day:d, time string:t} to theDate

set date_format to (y * 10000 + m * 100 + d) as text
set time_format to (t) as text

set reformated_date to (text items 3 thru 4 of date_format)
& (text items 5 thru 6 of date_format)
& (text items 7 thru 8 of date_format)
& getTimeString(time_format)

return reformated_date

end getDateString

-- returns time in format HHMMSS in 24 hour format
on getTimeString(theTime)

set currentDelimiter to AppleScript's text item delimiters

set AppleScript's text item delimiters to ":"
set timeComponents to (text items 1 thru 2 of theTime)
set secondsAMPM to (text item 3 of theTime)

set AppleScript's text item delimiters to " "
set end of timeComponents to (text item 1 of secondsAMPM)
set end of timeComponents to (text item 2 of secondsAMPM)

if (last item of timeComponents) is equal to "PM" then
set formattedString to ((item 1 of timeComponents as number) + 12) as text
set formattedString to item 1 of timeComponents
end if
set formattedString to formattedString & (item 2 of timeComponents) & (item 3 of timeComponents)

set AppleScript's text item delimiters to currentDelimiter

return formattedString

end getTimeString

[ Reply to This | # ]