Photoshop  Home Photoshop Miscellaneous Switching Between Open Documents
rss

Switching Between Open Documents

Author: FotoFects More by this author


If you are new to scripting for Photoshop, please view our Photoshop Scripting Basics tutorial for an introduction to Photoshop scripting and a few exercises about basic JavaScript.

What if we have several opened documents and want to choose only one of them to be closed? Documents are identified by the ordered they were opened in. Open several files inside Photoshop and click on the Window menu. We'll notice that the documents shown on the bottom of the menu are sorted by the order that they were opened in. The top document is document 0 and the ones below it are document 1, document 2, etc. You may also switch to different documents based on the the file name, including the extension (ex. my_photoshop_file.PSD).

1. To begin, we'll try out a simple way of switching documents. First lets type in a basic code that creates 3 documents with different sizes:

var oldRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;

documents.add(800, 600, 72, "My Photoshop File 1", NewDocumentMode.RGB, DocumentFill.WHITE);
documents.add(640, 480, 72, "My Photoshop File 2", NewDocumentMode.RGB, DocumentFill.BACKGROUNDCOLOR);
documents.add(320, 240, 72, "My Photoshop File 3", NewDocumentMode.RGB, DocumentFill.TRANSPARENT);

For a better understanding of the above code, please read the Opening and Closing Documents tutorial.


2. To switch documents, simply type in "activeDocument = documents[]". You can select which file to open entering filename inside the square brackets []. An example of selecting an active document using a filename is "activeDocument = document[my_photoshop_file.psd]". Another way is to set the active document by the order number. The order number is simply the order that the document was opened in. Have a look at the diagram below for examples.

The code can be entered on its own line and it'll be run.

var oldRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;

documents.add(800, 600, 72, "My Photoshop File 1", NewDocumentMode.RGB, DocumentFill.WHITE);
documents.add(640, 480, 72, "My Photoshop File 2", NewDocumentMode.RGB, DocumentFill.BACKGROUNDCOLOR);
documents.add(320, 240, 72, "My Photoshop File 3", NewDocumentMode.RGB, DocumentFill.TRANSPARENT);

activeDocument = documents[1]

Save the script and run it in Photoshop. With the script below, 3 windows should open and the 640x480 document should be the active window on top of all the other windows.



Rate this Material: Bad 1 2 3 4 5 Excellent
print this page tell a friend subscribe to newsletter subscribe to rss

Add comments to "Switching Between Open Documents"