Here you\'ll find tutorials that don\'t belong to either of the above categories. This is the place for the most extraordinary materials.  Home Photoshop Miscellaneous Opening and Closing Documents

Opening and Closing Documents


Creating a New Photoshop Document

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.

Click to enlarge
Click to enlarge

1. Because we need to set the units (pixels, inches, mm, etc) when creating a new document, we need to store the old ruler unit settings inside a variable and change it to the unit we want. For more information about ruler units, please read the Setting the Ruler and Type Units tutorial.

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


2. After we've stored the old unit settings and changed it to one of our own that will be used in this script, we can type in the script that'll create a new document. The method we'll call to do this is the documents.add() command.

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

documents.add();

There are 6 optional parameters you can set inside the documents.add():

  1. width - Width of the image in the ruler units.
  2. height - Width of the height in the ruler units.
  3. resolution - Resolution in pixels/inch
  4. mode - Color Mode (RGB, CMYK, LAB, Grayscale, or HSB.)
  5. initial fill - Background color (WHITE, BACKGROUNDCOLOR, or TRANSPARENT)

Type in the variable inside the documents.add() parenthesis in the order specified above.

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

documents.add(800, 600, 72, "My Photoshop File", NewDocumentMode.RGB, DocumentFill.WHITE);

Run the script inside Photoshop and it should create a new 800x600 pixels document inside Photoshop with the parameters set above.

image 2

Tip:
To create a new document based on the specifications of an image inside a clipboard, don't enter any parameters (ex. "documents.add();"). Photoshop will automatically enter the image specifications from the image the user copied into the clipboard.

Closing a Photoshop Document

Closing the Photoshop document is simple with the document.close() method but you must tell Photoshop which active document to close first using the activeDocument property. The document.close() method works just like closing a document inside Photoshop using the File> Close menu. If the document was modified, it'll ask if the user wants to save, discard and close or cancel.

1. Begin by typing in the document.close() method. 

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

documents.add(800, 600, 72, "My Photoshop File", NewDocumentMode.RGB, DocumentFill.WHITE);

documents.close();


2. Telling the script to close the current active document is simple. Just replace "documents" with the activeDocument property.

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

documents.add(800, 600, 72, "My Photoshop File", NewDocumentMode.RGB, DocumentFill.WHITE);

activeDocument.close();



Author's URL: FotoFects
Thank you for voting.
Rate this Materials:
Bad 
1 2 3 4 5 Excellent
print this page subscribe to newsletter subscribe to rss

Advance your current skills or acquire new skills in Photoshop by creating projects using our step-by-step tutorials. More Tutorials: Most Popular Materials | Fresh Materials | TutorialKit New Photoshop Tutorials

Add comments to "Opening and Closing Documents"

Only registered users can write comment

No comments yet...