• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Remove the images

Explorer ,
Mar 30, 2023 Mar 30, 2023

Copy link to clipboard

Copied

Hi,

The image frame used to be removed from the InDesign document after the images had been placed in the newly added frames on another page. Below you will find the script I used to remove the image. However, I have been unable to remove existing images from the Indesign document using the script.

 

   var doc = app.activeDocument;
   var page = doc.pages[i];
    var pageHeight = page.bounds[2] - page.bounds[0];
    
    // Loop through all text frames on the page
    for (var j = 0; j < page.textFrames.length; j++) {
   
      var frame = page.textFrames[j];
var pageValue = app.activeWindow.activePage + 1;
var imageNamesArray = [];
imageNamesArray.splice(0, imageNamesArray.length);
// Loop through each item on the active page
for (var m = 0; m < page.allPageItems.length; m++) {
  // Get a reference to the current page item
  var item = page.allPageItems[m];
  // Check if the current item is an image and log its name
  if (item instanceof Image) {
    var imageName = item.itemLink.name;
    if (imageName.length > 0) {
      imageNamesArray.push(imageName);
    }
  }
}
}
var frameWidth = 100;
var frameHeight = 50;
var frameX = 60;
var frameY = 640;
for (var k = 0; k < imageNamesArray.length; k++) {
  // Get the path to the image file
  var imagePath = FolderPath() + "/" + imageNamesArray[k];

  var activePage = app.activeWindow.activePage;
  var activePageNum = activePage.documentOffset;
  var currentFrameX = frameX + k * (frameWidth + 10);
  var newFrame = activePage.textFrames.add({
    geometricBounds: [frameY, currentFrameX, frameY + frameHeight, currentFrameX + frameWidth]
  });
  // Place the image in the new text frame
  var placedItem = newFrame.place(File(imagePath));
  newFrame.fit(FitOptions.proportionally);

  // Remove the original image frame
  var item = placedItem.parent;
  if (item instanceof Image) {
    var parentFrame = item.parent;
    parentFrame.remove();
  }
}

 

TOPICS
How to , Scripting

Views

339

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 02, 2023 Apr 02, 2023

Not sure if this is what you are trying to do, but it deletes the original:

 

app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;

var doc = app.activeDocument;
//page 1
var page = doc.pages[0];
//var pageHeight = page.bounds[2] - page.bounds[0];
var api = page.allPageItems
var frameWidth = 100;
var frameHeight = 50;
var frameX = 60;
var frameY = 640;
var item, imagePath, activePage, activePageNum, currentFrameX, newFrame;
for (var i = 0; i < api.length; i++){
    //activePage = app
...

Votes

Translate

Translate
Community Expert ,
Apr 02, 2023 Apr 02, 2023

Copy link to clipboard

Copied

I'm not a scripter ---- but had a quick look based on my limited knowledge

I've not tested - basically because I have no idea what you expect to happen with script.

 

var doc = app.activeDocument;
var page = doc.pages[0]; // replace 0 with the index of the desired page
var pageHeight = page.bounds[2] - page.bounds[0];
var imageNamesArray = [];

for (var m = 0; m < page.allPageItems.length; m++) {
  // Get a reference to the current page item
  var item = page.allPageItems[m];
  if (item instanceof Image) {
    var imageName = item.itemLink.name;
    if (imageName.length > 0) {
      imageNamesArray.push(imageName);
    }
  }
}

var frameWidth = 100;
var frameHeight = 50;
var frameX = 60;
var frameY = 640;
for (var k = 0; k < imageNamesArray.length; k++) {
  // Get the path to the image file
  var imagePath = "/path/to/image/folder/" + imageNamesArray[k];

  var activePage = app.activeWindow

  

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 02, 2023 Apr 02, 2023

Copy link to clipboard

Copied

Hi @Barathi , The if statement at the bottom of your script is returning false, so nothing gets removed. Also, you’ve declared the item variable again so it’s no longer referring to the image in your loop—now it is the parent frame of the newly placed image.

 

 

 // Remove the original image frame
  // item is not the original image, it’s now the parent frame of the newly placed image
  // placedItem.parent is not an image, so the if statement returns false
  var item = placedItem.parent;
  if (item instanceof Image) {
    var parentFrame = item.parent;
    parentFrame.remove();
  }

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 02, 2023 Apr 02, 2023

Copy link to clipboard

Copied

LATEST

Not sure if this is what you are trying to do, but it deletes the original:

 

app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;

var doc = app.activeDocument;
//page 1
var page = doc.pages[0];
//var pageHeight = page.bounds[2] - page.bounds[0];
var api = page.allPageItems
var frameWidth = 100;
var frameHeight = 50;
var frameX = 60;
var frameY = 640;
var item, imagePath, activePage, activePageNum, currentFrameX, newFrame;
for (var i = 0; i < api.length; i++){
    //activePage = app.activeWindow.activePage;
    //activePageNum = activePage.documentOffset;
    //check if the page item is an image
    if (api[i] instanceof Image) {
        //the file path of the image
        imagePath = api[i].itemLink.filePath;
        currentFrameX = frameX + i * (frameWidth + 10);
        newFrame = activePage.textFrames.add({
            geometricBounds: [frameY, currentFrameX, (frameY + frameHeight), (currentFrameX + frameWidth)]
        });
        newFrame.place(File(imagePath))
        newFrame.fit(FitOptions.proportionally);
        api[i].parent.remove()
    }
};   

app.scriptPreferences.measurementUnit = AutoEnum.AUTO_VALUE;

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines