Copy link to clipboard
Copied
Hello everyone,
I want to create a log file and later open it in Excel. I believe a txt file will be just fine. (maybe if it's easy to differ values in the log by tab or comma or something)
I want to know how to:
1. create it
2. update it
I would like to be able to write error logs, and other logs as well (for example - if a document is 1000x1000px then write to log: document name + path)
Thank you in advance
There is no easy way to close the file if it is opened by an external program, it would depend on the operating system on how you could manage it again calling an external program.
The easy way would be to tell the user to close the program if the script could not open the csv file...
...#target photoshop
main();
function main(){
if(!documents.length) return;
var doc = activeDocument;
var a,b,c,d ="";
var log = File(Folder.desktop + "/LogFile.csv");
var result = log.open("a");
if(!result) {
aler
Copy link to clipboard
Copied
Is the log created via
Photoshop > Preferences > History Log
not sufficient?
What exactly do you need to record?
Copy link to clipboard
Copied
I have a script that opens every TIF file in a folder (sub-folders included), i want in the log to see in a separate row for each file:
1. file name
2. path
3. metadata (tags, description)
I know the lines for each of the things i want, I just don't know the lines to create/update to log.
Copy link to clipboard
Copied
var doc = activeDocument;
var a,b,c,d ="";
a=decodeURI(doc.name);
b=decodeURI(doc.path.fsName);
c=doc.info.keywords.toString().replace(/,/g,";");
d=doc.info.caption;
$.writeln(a +","+b +"," + c + "," +d);
Copy link to clipboard
Copied
SuperMerlin​ where is the path of the txt file?
Copy link to clipboard
Copied
var log = File(Folder.desktop + "/LogFile.csv");
if(documents.length){
log.open("a");
var doc = activeDocument;
var a,b,c,d ="";
a=decodeURI(doc.name);
b=decodeURI(doc.path.fsName);
c=doc.info.keywords.toString().replace(/,/g,";");
d=doc.info.caption;
log.writeln(a +","+b +"," + c + "," +d);
log.close();
}
Copy link to clipboard
Copied
This is great
It saves everything in a new row as desired, but if the csv file is opened then the script doesn't update the information. is there a workaround?
I made couple of small adjustments:
#target photoshop
if(documents.length){
var doc = activeDocument;
var a,b,c,d ="";
// Name without file extension
var Name = doc.name;
if (Name.indexOf(".") != -1 ) Name=Name.substr(0,Name.lastIndexOf("."));
//
var log = File(Folder.desktop + "/" + Name + ".csv");
log.open("a");
a=decodeURI(doc.name);
b=decodeURI(doc.path.fsName);
c=doc.info.keywords.toString().replace(/,/g,";");
d=doc.info.caption;
log.writeln(a +","+b +"," + c + "," +d);
log.close();
}
Copy link to clipboard
Copied
If you open the file you will have problems, the work-a-round is make sure the file is closed.
The alterations you have done are not being used, if you want to use yours, it would be:-
log.writeln(decodeURI(Name) +","+b +"," + c + "," +d);
Copy link to clipboard
Copied
SuperMerlin​ can you please share the code to:
see if the file is closed
if not then close it
if prompted to save the file (changes where made) then save the file
r-bin​ csv file was created, let's say I opened it in Excel
Copy link to clipboard
Copied
There is no easy way to close the file if it is opened by an external program, it would depend on the operating system on how you could manage it again calling an external program.
The easy way would be to tell the user to close the program if the script could not open the csv file...
#target photoshop
main();
function main(){
if(!documents.length) return;
var doc = activeDocument;
var a,b,c,d ="";
var log = File(Folder.desktop + "/LogFile.csv");
var result = log.open("a");
if(!result) {
alert("you must close " + log.name + " before running this script");
return;
}
a=decodeURI(doc.name).replace(/\.[^\.]+$/, '');
b=decodeURI(doc.path.fsName);
c=doc.info.keywords.toString().replace(/,/g,";");
d=doc.info.caption;
log.writeln(a +","+b +"," + c + "," +d);
log.close();
};
Copy link to clipboard
Copied
I think it's good enough, thank you
Copy link to clipboard
Copied
open the cvs file in excel only for reading
Copy link to clipboard
Copied
I wanted to ask 2 questions:
Here is an example to a single line in the csv:
1. How do I write Titles row on row 1? I want it to be more readable: "Date", "Filename" "LayerName" "Path" "Tags"
2. all my Tags (column E) getting an apostrophe before them for some reason ( ' ), why is that?
BEFORE F2 + Enter
AFTER F2 + Enter
alternatively, using Find and Replace find it:
BEFORE
AFTER
Copy link to clipboard
Copied
This will create a header line if the log file does not exist.
I do not know why you are getting an apostrophe, I would have to look at your file to see how it is formatted.
I have added a section to remove these though.
#target photoshop
main();
function main(){
if(!documents.length) return;
var doc = activeDocument;
var a,b,c,d ="";
var log = File(Folder.desktop + "/LogFile.csv");
if(!log.exists){
var header = "Date, Filename, LayerName, Path, Tags";
log.open("w");
log.writeln(header);
log.close();
}
var result = log.open("a");
if(!result) {
alert("you must close " + log.name + " before running this script");
return;
}
var fDate = File(doc.fullName).created;
a = zeroPad(fDate.getDay(),2) + "/" + zeroPad(fDate.getMonth(),2) + "/" + fDate.getFullYear() + " " + fDate.toLocaleTimeString();
b=decodeURI(doc.name).replace(/\.[^\.]+$/, '');
c=decodeURI(activeDocument.activeLayer.name);
d=decodeURI(doc.path.fsName);
e=doc.info.keywords.toString().replace(/,/g,";").replace(/'/g,"");
log.writeln(a +","+b +"," + c + "," +d + "," + e);
log.close();
};
function zeroPad(n, s) {
n = n.toString();
while (n.length < s) n = '0' + n;
return n;
};
Copy link to clipboard
Copied
it works
would it be possible to add a search step and fill the correct line, example:
I have 4 files:
the log searches anyway for "doc.name", can I pre-make a csv file that will look like this:
and the script will find the correct file name and fill rest of the details in the line.
is it possible? (alternatively don't fill row if the filename is anywhere in column B)
Copy link to clipboard
Copied
It would be difficult as the script would not know which folder(s) the files are in.
Copy link to clipboard
Copied
What do you mean when you say "if the csv file is opened" ?
Copy link to clipboard
Copied
Wanted to ask what about writing an error to log and continue?
I believe it's possible, whenever you encounter an error, for example: I have tried to open a psd file and got the error message:
"Could not complete your request because an unexpected end-of-file was encountered", i want to be able to write to log the error quote (if possible) and continue to the next file.