Skip to main content
Known Participant
September 29, 2016
Answered

Need help editing strings from document info

  • September 29, 2016
  • 1 reply
  • 697 views

I am trying to create a string based on info from the active doument that is then saved as tab delimited text for use in spreadsheets.

I am new to scripting but I have been able to get the info from Photoshop but now I need to process some of it.

Here is the info I want in the order I want: filename, file size ( either working size or actual), pixel width, pixel height, inches width, inches height, image resolution, color mode.

example: CoolPicture_LYRD.psd     5.3 mb     3810    3810    5     5     762     RGB

Here is the info I have gotten back:

CoolPicture_LYRD.psd3810 px3810 px5 px5 px762 DPIDocumentMode.RGB

I need to make these changes:

  1. add the document size
  2. trim the px form the strings OR convert the "px" in the inches string to "in"
  3. short numbers to 6 characters ( for a number like 5.33333333333333 px)
  4. trim theDocumentMode. from the color mode info

I have tried using substrings which gets an error about functions

I have tried toString( ) and setting the range but with no luck.

any help would be appreciated. Thanks for looking.

Here is my script so far

//get the info from document that I need for my excel sheet

var imageHeightPixels = activeDocument.height

var imageWidthPixels = activeDocument.width

var fileResolution = activeDocument.resolution

var imageMode = activeDocument.mode

// get size in inches from pixel based measurement

var imageHeightInches = imageHeightPixels / fileResolution

var imageWidthInches = imageWidthPixels  / fileResolution

var imageName = activeDocument.name.toString()

var imageDimensions = new File ("~/desktop/File Info Report.txt") 

app.preferences.rulerUnits = Units.PIXELS

imageDimensions.open('a'); 

imageDimensions.write(imageName + "\t" + imageHeightPixels + "\t" + imageWidthPixels + "\t" + imageWidthInches + "\t" + imageHeightInches + "\t" + fileResolution + " DPI" + "\t" + imageMode + "\n"); 

activeDocument.close(SaveOptions.DONOTSAVECHANGES) 

 

imageDimensions.close();

This topic has been closed for replies.
Correct answer Tomas Sinkunas

There you go buddy:

var doc = app.activeDocument;

var currentUnits = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.PIXELS;

var fileName = doc.name;

var fileSize = (File(doc.fullName).length / (1024 * 1024)).toFixed(2);

var fileWidthPx   = doc.width.value;

var fileHeightPx = doc.height.value;

var fileResolution = doc.resolution;

var fileWidthInch = (fileWidthPx  / fileResolution).toFixed(6);

var fileHeigthInch = (fileHeightPx / fileResolution).toFixed(6);

var colorMode = doc.mode.toString().split(".")[1];

app.preferences.rulerUnits = currentUnits;

var outputString = fileName + "\t" +

  fileSize + " mb\t" +

  fileWidthPx + "\t" +

  fileHeightPx + "\t" +

  fileWidthInch + "\t" +

  fileHeigthInch + "\t" +

  fileResolution + " DPI\t" +

  colorMode + "\n"; 

var logFile = new File ("~/desktop/File Info Report.txt");

  logFile.open('a');   

  logFile.write(outputString);

  logFile.close();

doc.close(SaveOptions.DONOTSAVECHANGES);

1 reply

Tomas Sinkunas
Tomas SinkunasCorrect answer
Legend
September 29, 2016

There you go buddy:

var doc = app.activeDocument;

var currentUnits = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.PIXELS;

var fileName = doc.name;

var fileSize = (File(doc.fullName).length / (1024 * 1024)).toFixed(2);

var fileWidthPx   = doc.width.value;

var fileHeightPx = doc.height.value;

var fileResolution = doc.resolution;

var fileWidthInch = (fileWidthPx  / fileResolution).toFixed(6);

var fileHeigthInch = (fileHeightPx / fileResolution).toFixed(6);

var colorMode = doc.mode.toString().split(".")[1];

app.preferences.rulerUnits = currentUnits;

var outputString = fileName + "\t" +

  fileSize + " mb\t" +

  fileWidthPx + "\t" +

  fileHeightPx + "\t" +

  fileWidthInch + "\t" +

  fileHeigthInch + "\t" +

  fileResolution + " DPI\t" +

  colorMode + "\n"; 

var logFile = new File ("~/desktop/File Info Report.txt");

  logFile.open('a');   

  logFile.write(outputString);

  logFile.close();

doc.close(SaveOptions.DONOTSAVECHANGES);

Known Participant
September 29, 2016

I was hoping for advice but this will work! I am going to figure out what you did. Then  I will try to make it change the formula for determining the file size based on file name substring. I appreciate your help immensely!

natrev
Legend
September 30, 2016

Hi DevilsChariot,

Use this formula for file size

var fileSize = (File(doc.fullName).length / (1000 * 1000)).toFixed(1);

-yajiv