Help me automate a workflow
April 13, 2012 6:35 AM   Subscribe

Automator/macro/scripting help: Is there a way to automate a series of keystrokes, initiated by a command that I create? Running Mac OS 10.7.3.

I have several thousand PDFs that I need to grab a snippet of text from, then paste it into a single Word file. My current workflow goes as follows.
• Open PDFs files in blocks of 100
• Highlight desired text
• Copy text [Cmd-C]
• Switch applications to Word [Cmd-Tab]
• Paste text [Cmd-V]
• Switch applications back to Acrobat [Cmd-Tab]
• Close old PDF [Cmd-W]
• Repeat process on next document

What I would like to do is streamline this so I can execute all the keystrokes in a single command. So I would open my block of files, highlight my desired text and then hit a keyboard command that I assign (for instance, Cmd-Opt-E or whatever).

In this example, Cmd-Opt-E would then initiate [Cmd-C]+[Cmd-Tab]+[Cmd-V]+[Cmd-Tab]+[Cmd-W]. At the end of this, my selected text would be pasted into the Word doc, and the old PDF would be closed, with the new PDF awaiting me to select the text I need and initiate the sequence again.

I've tried figuring this out using Automator, but I can't seem to get it to switch applications, and create a key command to initiate my sequence.

I'm pretty Mac-savvy, but my technical skill is pretty limited when it comes to scripting or macros, and my coding is limited to very basic HTML at best.

So is it possible to set up a workflow like this? How?
posted by slogger to Computers & Internet (9 answers total) 1 user marked this as a favorite
 
Response by poster: Oh, and I forgot to add that there are a few instances where I have to grab some text from a couple of web pages, not many but a couple dozen. I use Chrome but also have Safari and Firefox. If I could incorporate them into my workflow it would make my life much, much easier. Thanks!
posted by slogger at 6:47 AM on April 13, 2012


Response by poster: Also also: I have access to a Windows 7 PC, if there's a feature or program there that will help me on this quest.
posted by slogger at 7:02 AM on April 13, 2012


http://www.autohotkey.com/ will script actions for Windows. There must be something similar for Mac.

I wonder if your problem has a better solution though. There are programs which convert PDF to plain text, and you could then use a programming language to extract the relevant sections from the plain-text version.
posted by richb at 7:17 AM on April 13, 2012


If you want an app to execute a series of keystrokes, try Keyboard Maestro. But it sounds like everything you want to do could also be handled natively with a little bit of AppleScript.

This MacWorld article on scripting Word has a few simple bits of scripting that might help you get started. You don't have to actually hit the keys to switch apps and paste—you basically command it using something along the lines of "Tell Microsoft Word to type text from the clipboard."
posted by bcwinters at 7:38 AM on April 13, 2012


How do you recognize the text you need to highlight?

When you paste it into Word, do you care about fonts and other formatting?
posted by flabdablet at 9:44 AM on April 13, 2012


How do you recognize the text you need to highlight?

From my read, by hand. The automation is for everything after that.

When you paste it into Word, do you care about fonts and other formatting?

It doesn't sound like it.
posted by rhizome at 9:59 AM on April 13, 2012


Response by poster: rhizome is correct: I do the highlighting manually (there is no consistent marker that I could use to automate this part of the process); and I want to remove all formatting -- I've actually just started using BBEdit for this project instead, since it renders everything as plain text.

I think I have a solution to this using Applescript. I've had to set this aside for the day, but will continue working on it over the weekend.
posted by slogger at 12:08 PM on April 13, 2012


Best answer: This is a little off of what you're asking for, but if you're o.k. getting the selections as plain text it might make sense for you, and it's very simple:

Automator allows you to set up services, which you're probably familiar with since you're pretty good at Macs in general. A service will allow you to receive a text selection and do something with it. In this case, you can use Automator to create a service that receives your selected text and adds it to a plain text file you designate ahead of time.

So here's the setup:

- Create your text file and note its Unix file path. So, if you make a new text file in your Desktop, its Unix path is "~/Desktop/textfile.txt"

- Open up Automator. When it asks you what you want to do, tell it you're creating a service (the cog icon)

- An automator service starts with "Service receives text in any application," so this will work anywhere you can select text

- For the first Automator action, pick the "utilities" action "copy to clipboard" and drag that in

- For the next Automator action, pick the "utilities" action "run shell script." Paste this into the text area for the action:
echo "---
" >> ~/Desktop/logfile.txt
pbpaste >> ~/Desktop/logfile.txt
echo "
You'll want to enter your own file's path. In my example, the file "logfile.txt" is sitting on my desktop.

Keep the extra returns ... they're separating your entries. Alternately, replace the "---" and return with something else to separate your entries.

Once you've got that set up, you can just save it with a sensible name and it's now a service you can use. Look for it by selecting some text in any app and going to the "Services" menu under the app's menu name in the menu bar. You're 3/4 of the way there.

Next you need to hook the service you just created up to a keystroke. Go to "Keyboard" under System Preferences, open the "Keyboard Shortcuts" tab, and click "Services". Look for your service under "Text," click the little gray "none" next to the service, and enter the keystroke you'd like to use to invoke it.

Now it should be working, allowing you to go to any app, select some text, use your custom keystroke, and log the text to the end of the text file you set up.

If you want to be able to automatically close the window of the document you're working in, you can add another action to your workflow: Utilities -> Run AppleScript. You're going to send the cmd-W keystroke:
on run {input, parameters}
	
	tell application "System Events"
		keystroke "w" using {command down}
	end tell
	
end run
re-save your workflow, and it'll now close whatever window is active when you invoke the service, once it has copied and appended your selected text.

Here's a link to a screenshot of the workflow in all its glory, in case it helps to have a visual guide to how it goes together:

https://skitch.com/michaelhall/8uy5r/logselection.workflow
posted by mph at 3:27 PM on April 13, 2012


Response by poster: mph: that looks like pretty much exactly what I was asking for. In the meantime I ended up cobbling together my own solution, which is similar but much clunkier than yours.

First, I created an Applescript based on what I saw here. Mine looks like this (I ended up using Preview and BBEdit, rather than Acrobat and Word):
tell application "Preview" to activate
tell application "Preview"
	tell process "Preview"
		tell menu bar 1
			tell menu bar item "Edit"
				tell menu "Edit"
					click menu item "Copy"
				end tell
			end tell
		end tell
	end tell
end tell
tell application "BBEdit" to activate
tell application "System Events"
	tell process "BBEdit"
		tell menu bar 1
			tell menu bar item "Edit"
				tell menu "Edit"
					click menu item "Paste"
				end tell
			end tell
		end tell
	end tell
end tell
tell application "Preview" to activate
tell application "System Events"
	tell process "Preview"
		tell menu bar 1
			tell menu bar item "File"
				tell menu "File"
					click menu item "Close Tab"
				end tell
			end tell
		end tell
	end tell
end tell
Then I saved my Applescript and created an Automator action to add it as a Service using the instructions I found in this article.

Finally, I created a keyboard shortcut for my new service by going to System Preferences >> Keyboard and assigned a command the same as you described, mph.

The whole thing works perfectly! I should be able to finish this project much quicker than I anticipated.

Can I give myself Best Answer? It feels wrong.
posted by slogger at 6:13 PM on April 13, 2012


« Older Why am I getting thinner but the scale says I...   |   Finding the one (BMI calculator that is) Newer »
This thread is closed to new comments.