PDFpen OCR Folder Action Script

As discussed on Mac Power Users episode 3, “Going Paperless,” the nice people at Smile On My Mac put together an Applescript that, when combined with a folder action, gives you a way to automatically OCR documents using PDFpen or PDFpenPro. So here is the promised walk through:
What you’ll need:
1. Some scanned PDF images;
2. PDFpen or PDFpenPro (See my review here);
3. A bit of patience.

Step 1 – Load up the Script Editor

Script Editor.png

This little application allows you to create and save AppleScripts.

Step 2 – Copy in the below script

on adding folder items to this_folder after receiving added_items
repeat with i from 1 to number of items in added_items
set this_item to item i of added_items
tell application “PDFpenPro”
open this_item
set theDoc to document 1
repeat with aPage in pages of theDoc
ocr aPage
— Looks like we need to modify PDFpen so that we can detect when OCR is done; for now use 15 seconds
delay 15
end repeat
save theDoc
close theDoc
end tell
end repeat
on error errText
display dialog “Error: ” & errText
end try
end adding folder items to
Note – if you use PDFpenPro instead of PDFpen, you’ll need to open the script and edit the command that reads “tell application “PDFpen” to read “tell application “PDFpenPro”.
Note 2 – Wordpress seems to have converted the double dash before the comment in to an em-dash and the quotes to smart quotes. Although I fixed it in the wordpress code, it still reverts to “fixing” things when I publish so you’ll have to correct those in your editor. Sorry. If anyone knows a better way to post applescript via wordpress, please drop me a note.

Step 3 – Save the script

You need to save it to a specific directory:
HD/Library/Scripts/Folder Action Scripts/
I named mine “PDFpen Scriptacular”

Step 4 – Create a folder

Save the folder wherever is convenient. Perhaps in your documents folder or (for you anarchists) on the desktop. By the way, did you know that command-shift-n gets you a new folder? I named mine “OCR Drop.”

Step 5 – Enable folder actions

Secondary click on the folder and enable folder actions under the “More” item.

Enable Folder Actions.jpg

Step 6 – Configure Folder Action

Right clicking the folder a second time gives you a new option, Configure Folder Action. Click it.

Configure Folder Actions-1.jpg

Step 7 – Pick Your Folder

On the menu that appears, hit the plus (+) sign under the “Folders with Actions” box.

FA pick folder.jpg

Select your folder, wherever you located it. It will then ask you to pick a script. Pick the PDFpen scriptacular.scpt

pick script.jpg

It should now look like this.

Script menu.jpg

Close the window and you are done.
Now just drag a few PDFs in and let the script go to work. Copy the OCR’d PDFs where they belong and you are done. There are a few additional points:
1. There is no Applescript command in PDFpen that reports when it is done doing an OCR so instead there is a 15 second timer. The PDFpen wizards report they are going to try and fix this in a future release.
2. While this script generally works, it sometimes gave me an error when I overloaded it. Be patient.
I want to give my personal thanks to the gang at Smile On My Mac, particularly Greg, who put this script together for Mac Power Users just because we asked.

Continue reading

Bento Syncing with Applescript


As I continue my attempts to synchronize using two Macs I have run into a wrinkle. My database application, Bento, does not sync. Since I’m only using one machine at a time, and the address and iCal data is already synced, it works fine if I just copy the database file between computers when switching. The trouble is that Bento insists the database be located in my Application Preferences/Bento folder so it is a pain to drill to it and copy it over to my iDisk. This seemed the perfect excuse to try my Applescript chops. So here is the script:
set SendOrReceive to button returned of (display dialog “Hey Sparky, Sending or Receiving?” buttons {“Sending”, “Recieving”} default button 2 with icon caution)
set LocalBentoFile to POSIX file “Users/david/Library/Application Support/Bento/bento.bentodb”
set iDiskBentoFile to POSIX file “/Volumes/iDisk/Documents/Bento Data/bento.bentodb”
set LocalBentoFolder to POSIX file “Users/david/Library/Application Support/Bento/”
set iDiskBentoFolder to POSIX file “/Volumes/iDisk/Documents/Bento Data/”
tell application “Finder”
if SendOrReceive = “Sending” then
duplicate LocalBentoFile to the folder iDiskBentoFolder with replacing
duplicate iDiskBentoFile to the folder LocalBentoFolder with replacing
end if
end tell

This script asks if I’m sending (to iDisk) or receiving (from iDisk) and then copies the file in the right direction. I thought about making it more automatic by comparing dates and duplicating the newest version in both places, but decided I want to have control over what direction the sync is going.
If anyone out there has any ideas for improving it, let me know or leave a comment.

Continue reading

AppleScript for Creating Form Documents in Word 2008


Okay gang, put on your propeller beanie caps. This one is going to be complicated.
One of the things I do during the day job includes a lot of corporate transactional work. As a result I have a few forms that I find myself using repeatedly. For instance, sometimes I have a client that needs a set of corporate minutes. I would like to have a system where I can run a program that prompts me for certain bits of information (i.e. date, corporate officers and directors) and then goes off and opens the form and fills in the basic information for me.
You think there would be guides all over the InterWeb for this but I couldn’t find any. So I spent a few hours today learning to Applescript and came up with the script I’m reporting below. I’m no expert at this and I’m pretty sure this could get better but at least my script is functional and hopefully saves the next person from the trouble of starting from scratch.
So I’m going to list the whole script below and then I’m going to break it into pieces. So lets start with the whole script …


— Dialog Box to Get Information
set response to display dialog “Type in the name you want to paste” default answer “Thelonious Monk”
set name to text returned of response
tell application “Microsoft Word”
open “Macintosh HD:Users:david:Library:Application Support:Microsoft:Office:User
Templates:My Templates:Piano Legends.dot”
— Name
set selFind to find object of selection
tell selFind
set content to “**Name**”
set content of replacement of selFind to name
execute find replace replace all
end tell
end tell

So breaking it down let me explain as best as my tiny programming brain can. If you are a complete Applescript newbie you need to first open Script Editor which can be found in the Applescript subdirectory of you Applications folder and then copy the above script in.
— Dialog Box to Get Information
The two dashes make this line a remark so the program basically ignores it. This sample has only one variable but the actual script has twelve variables. I named each with a remark so I can get back to where I need easily to debug if necessary.
set response1 to display dialog “Type in the name you want to paste” default answer “Thelonious Monk”
This line does two things:
First it pops up a dialog box that says “Type in the name you want to paste”
Second it fills in the box with a default answer of “Thelonious Monk” (Has anyone figured out yet what a big Monk fan I am?)
set name to text returned of response
This was the line that vexxed me the most. Simply putting up the dialog box does not create a variable that can be used to fill in a Word form. This line of code creates a new variable called “name” and fixes the problem. It took me an hour to figure this out.
tell application “Microsoft Word”
Now we are getting to the good stuff. Applescript just opened Word 2008.
open “Macintosh HD:Users:david:Library:Application Support:Microsoft:Office:User Templates:My Templates:Piano Legends.dot”
If you are going to be creating forms you first need to create a template in word. In this example I’ve created a template (.dot extension) in word called “Piano Legends.dot”. Obviously the location of your document template may vary slightly. When you create the template it is important that you distinguish the phrases you plan on replacing. I did it with asterisks. For instance the name section of the document is written “**Name**”. In setting it up this way you don’t need to bother Applescripting the formatting because the script just uses whatever formatting you chose in the template (i.e. All Caps, bold, etc…)
— Name
Another comment telling me I’m about to do the find and replace on the Name variable.
set selFind to find object of selection
I’m a bit clueless on this line but the script fails if it is not there. I think it selects the entire document for the find/replace action.
tell selFind
set content to “**Name**”

This starts up the find and replace process. It also sets the variable “content” to the search text I placed in the template as explained above, “**Name**”
set content of replacement of selFind to name
I just told Word “Find every instance of “**Name**” and replace it with the variable “Name”
execute find replace replace all
Word knows what I want it to do. Now it has to go do it.
end tell
Closing the loop.
end tell
Closing the loop again.
So there you have it. A rather tame Applescript that helps automate document production. You can duplicate as many variables and replacements as you need. I’m surprised about how easy this was to figure out considering I’m not much of a code jockey but it sure is handy.

Continue reading

Apple Mail Scripts 2.8


As I continue to fumble my way through Applescript, I stumbled upon Apple’s own Mail script package that has several nice applications.
Apple explains them as follows:
– Add Addresses (Mail): Add addresses found in the selected messages (in the header fields “From”, “To”, “Cc”, and “Bcc”) to the Address Book. This is much more flexible than the “Add Sender to Address Book” available in Mail and provides a convenient way for creating mailing lists.
– Archive Messages (Mail): Move messages from the selected mailbox(es) to an archive mailbox or export them to standard mbox or plain text files for backup purposes or import into other applications. You can select to move all messages or only messages sent within or certain period as well filter messages based on their read and flagged status.
– Change SMTP Server (Mail): Switch between different already defined SMTP servers or define a new one. This is especially useful if you are using your computer in more than one location and have to switch servers for several accounts at once.
– Create Rule (Mail): Create a new rule based on the first of the selected messages. This saves you the trouble of copy/pasting address or other info between the message and the rule window and provides a much quicker way for setting up a rule with multiple criteria/actions.
– Remove Duplicates (Mail): Locate all duplicate messages found in the selected mailbox(es) and move them to a separate mailbox for easy removal (duplicate matching is based on the unique message header “Message-Id”).
– Schedule Delivery (Mail): Allows you to send individual messages at predefined times (this script uses iCal for scheduling message delivery).
– Many additional features.

Technorati Tags:
, ,

Continue reading