Highlighted

Check if opened doc is saved already

Contributor ,
Apr 01, 2019

Copy link to clipboard

Copied

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

Views

507

Likes

Translate

Translate

Report

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

Check if opened doc is saved already

Contributor ,
Apr 01, 2019

Copy link to clipboard

Copied

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

Views

508

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Apr 01, 2019 0
LEGEND ,
Apr 01, 2019

Copy link to clipboard

Copied

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)

};

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 01, 2019 1
Contributor ,
Apr 01, 2019

Copy link to clipboard

Copied

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!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 01, 2019 1
Guide ,
Apr 01, 2019

Copy link to clipboard

Copied

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'));

};

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 01, 2019 1
Adobe Community Professional ,
Sep 08, 2020

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 08, 2020 0
Most Valuable Participant ,
Apr 01, 2019

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 01, 2019 0
Contributor ,
Apr 02, 2019

Copy link to clipboard

Copied

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
Mars Premedia

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 02, 2019 0
Adobe Community Professional ,
Apr 02, 2019

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 02, 2019 1
Most Valuable Participant ,
Apr 02, 2019

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 02, 2019 2