adding a logo to a photo using Photoshop CS

I am trying to add a miniture corporate logo to a series of images.

set logoize to choose file
set thelogo to "path:to:logo:file.psd" as alias
tell application "Finder"
	set thefilename to the name of logoize
	set thelogoname to the name of thelogo
end tell
tell application "Adobe Photoshop CS"
	set ruler units of settings to pixel units
	set display dialogs to never
	if exists document thelogoname then
	else
		open thelogo
	end if
	open logoize
	tell document thefilename
		set this_info to the info
		set originalresolution to the resolution
		if originalresolution > 72 then
			resize image resolution 72 resample method none
		end if
		set originalwidth to the width
		set originalheight to the height
		if originalwidth > originalheight then
			if originalwidth > 1024 then
				resize image width 1024
				resize canvas height 768
			end if
		else
			if originalheight > 768 then
				resize image height 768
				resize canvas width 1024
			end if
		end if
		set originalmode to mode
		if originalmode is not RGB then
			convert color to RGB
		end if
	end tell
	tell document thelogoname
		select all
		copy
	end tell
end tell

The problem is that PhotoShop won’t let me select from any document that isn’t the frontmost document. I keep getting the error:

Adobe Photoshop CS got an error: The requested action requires that the target document is the frontmost document.

I can’t find any documentation on how to switch which document is the frontmost document. Anyone know?

I found this posting while trying to figure out the same thing. It seems that you use this:

set current document to

At least it worked for me on PS7, OSX.

Personally, I’d look into using something like ImageMagick to add logos to your pictures.
http://www.imagemagick.org/script/index.php

In fact, it looks like the command-line composite will do exactly what you’re looking for.
http://www.imagemagick.org/script/composite.php

Here’s a link to Apple’s page on ImageMagick, where you can download it. There are also binary installs out there, too.
http://www.apple.com/downloads/macosx/unix_open_source/imagemagick.html

Hope that helps!