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
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)
};
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!
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'));
};
Copy link to clipboard
Copied
Such codes works much faster with property: Error of unsaved new created file
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
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.
}
Copy link to clipboard
Copied
SuperMerlin way to do it is the fastest, however in this case it may be not so important.
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?
Find more inspiration, events, and resources on the new Adobe Community
Explore Now