Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Check if opened doc is saved already

Engaged ,
Apr 01, 2019 Apr 01, 2019

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

TOPICS
Actions and scripting
1.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
Apr 01, 2019 Apr 01, 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)

};

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 01, 2019 Apr 01, 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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 01, 2019 Apr 01, 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'));

};

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 08, 2020 Sep 08, 2020
LATEST

Such codes works much faster with property: Error of unsaved new created file

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 01, 2019 Apr 01, 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 02, 2019 Apr 02, 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 02, 2019 Apr 02, 2019

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 02, 2019 Apr 02, 2019

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines