Skip to main content
Participant
December 6, 2023
Question

uxp app undefined

  • December 6, 2023
  • 1 reply
  • 905 views
In below code always got app undefined in below code inside function readInDesignContent()
 
 
const { entrypoints , host} = require("uxp");
const { app } = require("indesign");
entrypoints.setup({
commands: {
showAlert: () => readInDesignContent()
},
panels: {
showPanel: {
show({node} = {}) {}
}
}
});
async function readInDesignContent() {
try {
// Access the UXP context
const { app } = require('uxp').shell;
// Check if the app object is defined
if (!app) {
console.log("Error: 'app' object is undefined.");
return;
}
// Check if there is an active document
if (app.activeDocument) {
const activeDocument = app.activeDocument;
// Access the textFrames in the active document
const textFrames = activeDocument.textFrames;
// Iterate through textFrames and log their contents
for (const textFrame of textFrames) {
console.log("Text Frame Content:", textFrame.text);
}
} else {
console.log("No active document found.");
}
} catch (error) {
console.error("Error:", error);
}
}


This topic has been closed for replies.

1 reply

Known Participant
December 6, 2023

Hi! Try changing the line like this and move it to the top. Or delete this line completely because... shell is not used anywhere below.
const
 { app } = require('uxp').shell   =>   const shell = require('uxp').shell

Participant
December 7, 2023

Thanks @AntonN , I got it, basically I want to read inDesign active document and not able to find the reference any where, could you please share some sample code for how to read inDesign active document.

Known Participant
December 7, 2023

Try the next code:

const { entrypoints } = require("uxp");
const { app } = require("indesign");

entrypoints.setup({
  commands: {
    showAlert: () => showAlert(),
  },
  panels: {
    showPanel: {
      show({ node } = {}) {},
    },
  },
});

showAlert = () => {
  const doc = app.activeDocument;
  console.log(doc);

  if (doc) {
    const textframes = doc.textFrames; // as collection
    console.log(textframes);

    const textframesArr = doc.textFrames.everyItem().getElements(); // as array
    console.log(textframesArr);
  } else {
    console.log("No active document found.");
  }
};