New at UXP, can't get app.documents.add() to work

I’m new at UXP, but well versed in HTML, CSS and Javascript. I set up a plugin using the quick-layers-starter template and it runs fine. I need to create a new blank document so I simply commented out several lines, added the line from the Photoshop API Reference, plus the async keyword to the function. When I reload it and run it nothing happens. No new document, no exceptions in the debugger, nothing. What am I doing wrong? Here’s the index.js code:

async function showLayerNames() {
const app = require(“photoshop”).app;
// const allLayers = app.activeDocument.layers;
// const allLayerNames = =>;
// const sortedNames = allLayerNames.sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
// document.getElementById(“layers”).innerHTML = // <ul>${ // =>

  • ${name}
  • ).join("") // }</ul>;

    let newDoc1 = await app.documents.add();


    document.getElementById(“btnPopulate”).addEventListener(“click”, showLayerNames);

    Anything that changes the state of Photoshop needs to be wrapped in an executeAsModal function:

    Thanks, that worked nicely. Really appreciate your help.

    Does that also apply to guides? I ask because once the document is created, app.activeDocument.guides.add(…) doesn’t work in either modal or async context.

    Hmm, I’m not sure about guides. I would always err on the side of modal execution though.
    Let’s see your code and see if we can figure it out.

    Here’s the code. I tried it both ways by commenting out one or the other “app.activeDocument.guides.add…” lines.

    async function createDocumentForPOD(executionContext, descriptor) {
        const app = require("photoshop").app;
    	var trimWin = 6.00;
    	var trimHin = 9.00;
    	var trimDpi = 300;
    	var trimWpx = trimDpi * trimWin;
    	var trimHpx = trimDpi * trimHin;
    	let newDoc = await app.documents.add({
    	   width: trimWpx, 
    	   height: trimHpx, 
    	   resolution: trimDpi
    	app.activeDocument.guides.add(Constants.Direction.HORIZONTAL, 75);
    	return newDoc;
    async function setupGuidesAndDocumentForPOD() {
    	try {
    		await require('photoshop').core.executeAsModal(createDocumentForPOD, {"commandName": "My Script Command"});
    	catch(e) {
    	  if (e.number == 9) {
    		  showAlert("executeAsModal was rejected (some other plugin is currently inside a modal scope)");
    	  else {
    		showAlert("Exception thrown inside createGuidesForPOD");
        // const app = require("photoshop").app;
    	// app.activeDocument.guides.add(Constants.Direction.HORIZONTAL, 75);
    document.getElementById("btnPopulate").addEventListener("click", setupGuidesAndDocumentForPOD);

    I believe this one here is related to, and possibly solved by, that one there.

    1 Like