Skip to main content
Inspiring
April 1, 2019
Question

Check if opened doc is saved already

  • April 1, 2019
  • 4 replies
  • 1751 views

Hi everybody!

I wrote a short script which helps me saving docs in PNG or JPG more flexible. Most of this script is done and it works.

One of my last tasks is to check, if the document I´m working woth has already been saved or not (I need the path to save it as PNG or JPG at the same place, where the original PSD Document is).

But, if you´ve never saved the document before (because it has been created as a new one) there is also no path, only an error message. Now I want to awoid this error with give the user the opportunity to select a new Folder.

Here ist the snippet:

var myDoc = app.activeDocument;

var dithArray = [];

var Name = decodeURI(myDoc.name).replace(/\.[^\.]+$/, ''); 

//Prüfen, auf welcher Umgebung der Anwender arbeitet

var os = $.os.toLowerCase().indexOf('mac') >= 0 ? "MAC": "WINDOWS"; 

switch(os){

    case "MAC": var userName = $.getenv("USER")

    break;

    case "WINDOWS": var userName = $.getenv("USERNAME")

    break;

    }

//Standard-Pfad generieren

var defaultPath = "/Users/" + userName + "/Desktop";

//Prüfen, ob das Dokument bereits gespeichert worden ist

if (myDoc.created == false){

    var Path = decodeURI(myDoc.path);

    }

else(Path = Folder.selectDialog ("Bitte einen Speicherort wählen!", defaultPath))

Line 17. is not proper like it is. Any idea?

Thanks in advance

This topic has been closed for replies.

4 replies

pixxxelschubser
Community Expert
Community Expert
April 2, 2019

About this topic there is a good comprehensive response in this older thread: Re: Determine if image has been saved?

willcampbell7
Legend
April 2, 2019

Within try/catch, check for document property "fullName", which is the file object (with full path and file name) of the document.

Documents never saved will throw an error because they lack this property.

try {

    if (myDoc.fullName) {

          // Do nothing; just a test if the property exists.

    }

} catch (e) {

    // Failed means document has never been saved.

    // Execute whatever code applies to condition.

}

William Campbell
Kukurykus
Legend
April 2, 2019

SuperMerlin way to do it is the fastest, however in this case it may be not so important.

JJMack
Community Expert
Community Expert
April 1, 2019

A document mate have been saved  however, it is also posible thate you have madeg chanhes the have not been saved.  The document is more or less in transition..

Re: Document saved property not updating when manipulating guides

JJMack
c.pfaffenbichler
Community Expert
Community Expert
April 1, 2019

A try-clause may suffice.

var thedoc = app.activeDocument;

var docName = thedoc.name;

try {

var basename = docName.match(/(.*)\.[^\.]+$/)[1];

var docPath = thedoc.path;

}

catch (e) {

var basename = thedoc.name;

var docPath = Folder.selectDialog ("Bitte einen Speicherort wählen!", defaultPath)

};

cmoke73Author
Inspiring
April 1, 2019

It seems to be the good way...

Before I mark it as the correct answer, I want to let the script be checked for a while.

Thanks!

SuperMerlin
Inspiring
April 1, 2019

Yet another way to check...

alert(hasFilePath());

function hasFilePath(){

var ref = new ActionReference();

ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );

return executeActionGet(ref).hasKey(stringIDToTypeID('fileReference'));

};