Skip to main content
Participant
October 3, 2023
Question

Inserting photos on the page into frames from the applied template.

  • October 3, 2023
  • 1 reply
  • 190 views

I have a template in my document, which, among other things, contains an empty frame. Using this template, I create a certain number of pages in a document, and then I insert a photo into this frame on each page. At the same time, the program extracts this frame from the template to the page itself. Quite tedious.

And so I have a script that automatically inserts photos into frames. However, how to make the insertion happen according to the scenario described above (extracting the frame from the template and inserting) I don't know. Tell me what needs to be changed in the code? The code is attached (there are a lot of russian words, don't be afraid 🙂 😞

 var imageFiles = File.openDialog("Выберите фотографии", "*.jpg; *.jpeg; *.png; *.gif", true);

  // Проверка, что пользователь выбрал файлы
  if (imageFiles != null && imageFiles.length > 0) {
    // Получение текущего слоя
    var currentLayer = app.activeDocument.activeLayer;

    // Получение всех фреймов на текущем слое
    var framesOnLayer = app.activeDocument.pageItems.everyItem().getElements();

    // Создание переменных для управления размещением изображений
    var currentPage = app.activeWindow.activePage;
    var currentFrameIndex = 0;
	var currentPageIndex = currentPage.documentOffset;

    // Цикл по всем фреймам на слое, начиная с текущей страницы
for (var i = 0; i < framesOnLayer.length; i++) {
  // Проверка, что текущий элемент является фреймом и находится на видимом слое
  if (framesOnLayer[i].constructor.name == "Rectangle" && framesOnLayer[i].itemLayer.visible) {
    // Проверка, что элемент находится на текущей странице или на следующих страницах
    var framePageIndex = framesOnLayer[i].parentPage.documentOffset;
    if (framePageIndex >= currentPageIndex) {
      // Проверка, что есть еще изображения для размещения
      if (currentFrameIndex < imageFiles.length) {
        // Размещение изображения в текущем фрейме
        framesOnLayer[i].place(imageFiles[currentFrameIndex]);

        // Проверка, что текущий фрейм не может вместить изображение
        if (!framesOnLayer[i].images[0].isValid) {
          // Переход на следующую страницу
          currentPage = currentPage.nextPage;

          // Начало размещения изображения с первого фрейма на новой странице
          currentFrameIndex = 0;
          currentPageIndex = currentPage.documentOffset;
          framesOnLayer = currentPage.pageItems.everyItem().getElements();
          i = -1; // начать цикл заново
        } else {
          // Увеличение значения currentFrameIndex на 1
          currentFrameIndex++;
        }
      } else {
        // Все изображения размещены
        break;
      }
    }
  }
}
}
}

app.doScript(vstavka, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Add Images');

 

This topic has been closed for replies.

1 reply

Robert at ID-Tasker
Legend
October 3, 2023

First, you need to define what do you mean by "template". 

 

Then, in your code you are using variable "framesOnLayer" - which is a bit misleading as it a list of all objects from the document - no matter what layer they are on.

 

Then you are getting the name of a currently active layer - "currentLayer" - but you are not using it anywhere - to check if object that you want to process is on this layer?