Skip to main content
Participant
March 27, 2023
Answered

Can't save png with extendscript uxp, getting "is not defined" error

  • March 27, 2023
  • 1 reply
  • 1951 views

I just started learning photoshop scripting with extendscript, and am utterly unable to figure out how to get it to save a file, I looked in a few places on the internet, including here, adobe community forums, scripting ref ebooks, chatgpt, and saw a lot of suggestions from people and robots who seem to have gotten it working, but none of them is working for me.

 

Here is a list of methods I tried, which are all giving me the same error. I only included the line of code which threw the error, which is usually the very first line that deals with defining a File object, I can't even get past that part.

 

var expFile = new PhotoshopSaveOptions();

 

Uncaught ReferenceError: PhotoshopSaveOptions is not defined

 

var pngFile = new File("D:/temp/test.png"); 

 

Uncaught ReferenceError: File is not defined

 

var pngSaveOptions = new PNGSaveOptions(); 

 

Uncaught ReferenceError: PNGSaveOptions is not defined

 

var saveOptions = new JPEGSaveOptions();

 

Uncaught ReferenceError: JPEGSaveOptions is not defined

 

var options = new ExportOptionsSaveForWeb(); options.format = SaveDocumentType.PNG;

 

Uncaught ReferenceError: ExportOptionsSaveForWeb is not defined

 

var options = new ExportOptions(); options.format = SaveDocumentType.PNG; options.transparency = true;

 

Uncaught ReferenceError: ExportOptions is not defined

 

I'll keep looking and experimenting, but if you have a solution for me, I'd very much appreciate it.

This topic has been closed for replies.
Correct answer jazz-y

Explain in more detail where you are trying to run this code?

 

Your code looks like Extend Script (*.jsx). Save it as a separate text file, change the extension to *.jsx and try dragging it into Photoshop (or use menu File -> Sripts -> browse)

 

If we are talking about UXP Developer Tools, then it is quite logical that you get errors - there is a different structure of DOM objects. Read about it here:Photoshop API

1 reply

jazz-yCorrect answer
Legend
March 27, 2023

Explain in more detail where you are trying to run this code?

 

Your code looks like Extend Script (*.jsx). Save it as a separate text file, change the extension to *.jsx and try dragging it into Photoshop (or use menu File -> Sripts -> browse)

 

If we are talking about UXP Developer Tools, then it is quite logical that you get errors - there is a different structure of DOM objects. Read about it here:Photoshop API

ze0D45Author
Participant
March 27, 2023

Thank you for that, I'm too new to this process to really understand why this is the case, could you help further?

I'm writing a .js with VS code, the goal is to turn off and on certain layers in a PSD then export a PNG, and do this a large number of times without any human input. (10s to 100s of thousands of times)

 

I'm using nested loops to get layers and layer.visible to turn them off or on.

 

The last step in the process should be to save a PNG, and this is where I'm stuck.

 

I see in the page about photoshop DOM you linked that I can doAction with UXP.

Is one possible solution then to write a separate .jsx for the file export, and have my main script doAction after layer visibility changes, and have the .jsx for exporting execute with an action?

 

ze0D45Author
Participant
March 27, 2023

I got it to work now, just as you said, all I had to do was instead of trying to run the code by hitting Load on the UXP Tool, save it out as a .jsx from VS Code, then drag the file into photoshop.

 

Thanks so much for the tip!