Get an image's height/width from clipboard?

I’m currently using a script in PhotoShop that copies up to 52 channel selections and, with each one, makes a new document, pastes the clipboard contents to it, then saves and closes the new document with a unique, sequential name.

At the moment, I have the script create a new document at a size that’s large enough to hold any of the selections, but I’d like to make each new document the exact size of the selection that’s been copied to the clipboard.

Any ideas of how to get the height and width, in pixels, of the image copied to the clipboard?

I’m using OSX 10.3.6 and PhotoShop CS.

– Walt Sterdan, Freelance

I don’t know if there are osaxen that add clipboard commands. I know the standard additions has “clipboard info”, but that only tells you the class of what’s in the clipboard.

How about just asking Photoshop the bounds of the chanel you’ve selected right before you copy it and using that information to size the new document. You could also select the channel in the new doc and crop the new document to that size.

Would that do the trick?

If possible, sure; I can find commands for setting the bounds, but not for getting them, though it’s always possible I’m looking right at them. :wink:

Any hints?

– Walt Sterdan, Freelance

Hi,

Panther has a different app for scripting picutre data and I don’t know if it still has Image Capture Scripting as in Jaguar. Something like this works in Jaguar:

set desk_path to (path to desktop) as string
set file_spec to (desk_path & “New Pic.tif”) as file specification
tell application “Finder”
activate
set pic_data to item 1 of (the clipboard)
end tell
– create the file
set ref_num to (open for access file_spec with write permission)
try
write pic_data to ref_num
close access ref_num
on error
close access ref_num
beep 2
return
end try
– read pic info
tell application “Image Capture Scripting”
set the_doc to (open file_spec)
set {w, h, r} to {width, height, resolution} of the_doc
end tell
– if you want to delete the newly created file
(*
tell application “Finder”
delete file_spec
end tell
*)
return {w, h, r}

gl,

Hi;

Sorry to take so long to reply. Thanks for the suggestion, I gave it a few tries but I get an error on the line quoted above; it seems unable to set “w”, or rather to get the value for it. I’m not sure if the overall method would be viable as well, as each file has 52-68 selections to work on, and the time lost to writing then deleting all of those temp files is worth more than the compressed blank space the final files would save.

I still think there must be a way to easily get the height and width of either the selection in PhotoShop or from the clipboard, but I’m still drawing a blank.

Again, thanks very much for taking the time to offer your code and help, it’s greatly appreciated.

– Walt Sterdan, Freelance

At first I thought you could just call an action that just would “Make - New :Document Preset:Clipboard” but for some reason the action doesn’t work properly…it only retains the clipboard setting of the first selection…go figure.

Soooo, I think the direction you might want to look into is when you make your new doc
set the initial fill to transparent, paste your selection, then trim transparent pixels.

Search the PS applescript reference guide for:

new document
trim or trim document

Post results!

ORANGE

Yup, that’s one of the first things I tried… I even tried recording the action changing the preset to something else, then back to Clipboard, but same thing, it remembers the first selection.

Sounds like that might just work; I’ll test it and get back to you, thanks!

– Walt Sterdan

Worked like a charm!

Oddly, I did have to add a bit extra that I don’t understand (like that’s a new thing).

Previously, I was making a new document, pasting the clipboard to it, merging the layers, saving the doc as a JPEG and then closing the doc. Now, with the background layer transparent, I don’t have to merge the layers but, after saving the doc as a JPEG, when I try to close the doc it was pausing to ask me if I wanted to save the document “Untitled-1”, even though I’d just finished saving it and despite having “set display dialogs to never”. Curious. I just had to add “without saving” after the “close document” so it’s no big deal, just curious.

For anyone interested, here’s the quick ‘n’ dirty PhotoShop CS version of the program; in this instance, it steps through 52 alpha channels of the fiche scan of “Captain Marvel #30”, loading and saving each channel as a seperate document:

– program paginate:

tell application “Adobe Photoshop CS” – PS7 code: tell application “Adobe Photoshop 7”
set display dialogs to never
with timeout of 1000 seconds – timeout set high; default setting caused timeout while building histograms
activate

	set docInfoRef to info of current document
	
	repeat with n from 1 to 3
		
		set indexPage to {n}
		indexPage as string
		
		set myPage2 to ((("Alpha ") as string) & indexPage) as string
		if n ? 9 then
			set myPage to ((("0") as string) & indexPage) as string
		else
			set myPage to (indexPage) as string
		end if
		
		tell application "Finder"
			set myTempFileName to ((("Fire:Documents:Comic Scans:Capt. Marvel:30:30_") as string) & myPage) as string
		end tell
		
		load selection of current document from channel myPage2 of current document combination type replaced
		
		-- PS7 code: copy selection of current layer of current document
		copy -- PS CS version
		
		set doc to make new document with properties {width:1100 as pixels, height:1530 as pixels, initial fill:transparent}
		-- do action "NewDoc" from "Default Actions.atn"
		
		paste -- PS CS version
		-- PS7 code: set newLayerReg to paste
		
		-- merge visible layers of current document
		
		set myOptions to {class:JPEG save options, embed color profile:false, format options:standard, quality:12, matte:none}
		
		--save current document in file myTempFileName as JPEG with options myOptions without copying
		
		trim current document basing trim on transparent pixels
		
		save current document in file myTempFileName as JPEG with options myOptions without copying
		
		close current document without saving
	end repeat
end timeout

end tell

Thanks very, very much for your help, guys, it’s muchly appreciated.

– Walt Sterdan, Freelance