Copy & Paste with AppleScript

Apple Automator

Today I was struggling with an extremely repetitive task that basically consisted of copying content from one program into another, then making a few minor changes or having an Excel script calculate a result for me, and then pasting the result back into Firefox. After that, this pretty mind-numbing task had to be repeated. More than 1,400 times.

After typing in around 300 entries by hand, I looked around for a better solution and once again came across Automator. This software comes with Mac OS X by default (I’ve been working on a Mac for a while now; if you’re interested in the details, check out my switching series on my personal blog) and lets you create extensive macros. They even work when they involve several programs.

And here’s the source code of the AppleScript:

repeat 25 times
	
	tell application "Firefox" to activate
	tell application "System Events"
		tell process "Firefox"
			keystroke "c" using command down
			keystroke tab
		end tell
	end tell
	
	delay 0.3
	
	tell application "Microsoft Excel" to activate
	tell application "System Events"
		tell process "Microsoft Excel"
			keystroke "v" using command down
			keystroke tab
		end tell
	end tell
	
	delay 0.3
	
	tell application "Firefox" to activate
	tell application "System Events"
		tell process "Firefox"
			keystroke "c" using command down
		end tell
	end tell
	
	delay 0.3
	
	tell application "Microsoft Excel" to activate
	tell application "System Events"
		tell process "Microsoft Excel"
			keystroke "v" using command down
			delay 0.3
			keystroke tab
			delay 0.4
			keystroke "c" using command down
		end tell
	end tell
	
	delay 0.3
	
	tell application "Firefox" to activate
	tell application "System Events"
		tell process "Firefox"
			keystroke "v" using command down
			keystroke tab
			keystroke tab
			keystroke tab
			keystroke tab
		end tell
	end tell
	
	delay 0.3
	
	tell application "Microsoft Excel" to activate
	tell application "System Events"
		tell process "Microsoft Excel"
			keystroke return
		end tell
	end tell
	
	delay 0.3

end repeat

Making your own adjustments should be quick and easy; otherwise, just get in touch with us and we’ll be happy to lend you a hand.

As always, we’d of course love to hear your feedback.

Enter opens the slider, Esc leaves it. Arrow keys browse, Tab moves on.

More from this category

Leave a Reply

Your email address will not be published. Required fields are marked *