Skip to main content
Participant
December 13, 2023
Question

how to save selected image in inDesign using uxp code

  • December 13, 2023
  • 2 replies
  • 540 views

Can anyone help how to save selected image save in local drive in inDesign uxp code structure. below given code is not working for me.

const app = global.app ?? require("indesign").app;
async function saveImage() {
const selection = app.activeDocument.selection;
if (selection) {
const selectedImage = selection[0];
if (selectedImage) {
try {
const imageCopy = selectedImage.duplicate();
imageCopy.exportFile("JPG", "");
console.log("Image saved successfully!");
imageCopy.remove();
} catch (error) {
console.log("Error saving image: " + error);
}
} else {
console.log("The selected item is not an image.");
}
} else {
console.log("No item selected. Please select an image to save.");
}
}
This topic has been closed for replies.

2 replies

Legend
December 13, 2023

• Function saveImage is defined but not executed. Use `await saveImage()` to execute it

 

• Change the export image type 'JPG' passed to exportFile to ExportFormat.JPG or 1246775072. ExportFormat must be required by yourself

const { app, ExportFormat } = require('indesign') ;

 

• You have to specify a destination path for exportFile

const os = require('os') ;
const dstFolder = path.join(os.homedir(), 'Desktop') ;
imageCopy.exportFile(ExportFormat.JPG, path.join(dstFolder, 'sample.jpg')) ;
brian_p_dts
Community Expert
Community Expert
December 13, 2023

I don't think your exportFile arguments are correct. I don't think they have changed from JSX to UXP. Think you need to pass a valid File object, but I don't have the API in front of me.