Thursday, August 07, 2008

Script to reverse order of open documents in Photoshop

I am constantly scanning batches of documents into Photoshop and needed a way to reverse the order of the open documents. For example, I'm scanning 50 documents of a child growing up from age 0 to 20. I start with the baby picture and end with the picture of them at 20. When I'm done scanning I then want to save them in order, starting with 001.jpg as the youngest picture and 050.jpg as the oldest picture. Problem is, when I'm done scanning the active document is the oldest picture, and I often don't know exactly how many documents I've scanned, so I can't just start with 050.jpg and start counting down. If only I could reverse the order of the open documents - then I could start with 001.jpg and just end with whatever number of documents I scanned.

I used to do this by automating a save action and just saving all the documents and then reopening them. This worked OK because the documents were all Untitled-4, Untitled-5, etc, and I could open them in alphabetical order. But I thought that surely there must be a better way. I finally broke down and wrote a very simple vbscript to accomplish this task.

Here it is:
**************
'declare variables:
dim app
dim doc
dim arrDocs
dim strDocs

'set object reference to photoshop:
set app=createObject("Photoshop.Application")

'create a comma delimited string of all open photoshop documents:
for each doc in app.documents
strDocs=strDocs & doc.name & ","
next

'take off the last comma:
strDocs=left(strDocs,len(strDocs)-1)

'split the string into an array:
arrDocs=split(strDocs,",")

'iterate through the array in reverse order, making each document the active document as you go:
i=ubound(arrDocs)
do while i=>0
app.ActiveDocument=app.documents(arrDocs(i))
i=i-1
loop

'cleanup:
set app=nothing
*************************

Copy and paste the area between the asterisks into notepad, save it as reverseOrder.vbs, and double click it. Voila (assuming you have Photoshop open with all your documents open).

Your reason for wanting to reverse the order of open documents in Photoshop could be different, but at least this is a way to do it. I searched the web and couldn't find a way, so there you go. If there is a simple way, and I'm a moron for resorting to a script to accomplish something easily done another way, please let me know!

For more information on Photoshop scripting, refer to the Photoshop CS3 scripting guide, downloadable from Adobe, and maybe on your computer if you installed the content CD with the Master Collection (in my case, this is found in C:\Program Files\Adobe\Master Collection Content CD\Documentation\Scripting\Adobe Photoshop CS3)

0 Comments:

Post a Comment

<< Home