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

Script to resize a file PNG

Community Beginner ,
Oct 12, 2022 Oct 12, 2022

Copy link to clipboard

Copied

Good evening everyone:

My script, opens a PNG file and reads its contents. Then the script opens another file (Mockup_001.ai) and pastes the PNG file. The problem now is that I need this PNG image to be in a certain position and with a certain size. How to proceed? Follow the script...

Thanks for any suggestion.


var filePng = "C:\\Temp\\PNG.png";
var fileMockup = "C:\\Temp\\Mockup_001.ai";

var docPng=app.open(File(filePng));

var newItem;
var copiaPNG = app.activeDocument.selection;

mockup = app.open(File(fileMockup));

if (copiaPNG.length > 0) { 
for(var i=0; i<copiaPNG.length; i++){

copiaPNG[i].selected = false;

newItem = copiaPNG[i].duplicate(mockup, ElementPlacement.PLACEATBEGINNING);

}
}

TOPICS
Scripting , SDK

Views

63

Likes

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 2 Correct answers

Community Expert , Oct 12, 2022 Oct 12, 2022

opening the png file and copying it to the mockup file is not very efficient. I would open the mockup file and "place" (import) the png file instead

 

 

var filePng = "C:\\Temp\\PNG.png";
var fileMockup = "C:\\Temp\\Mockup_001.ai";

var mockup = app.open(File(fileMockup));

var placedPng = mockup.placedItems.add();
placedPng.file = File(filePng);

// you need to figure out the right size and placement;
placedPng.width = 200;
placedPng.height = 300;
placedPng.position = [50, -30];

 

Likes

Translate

Translate
Community Expert , Oct 13, 2022 Oct 13, 2022

Hi @Alexandre - Alenac2001,

Adding more what @CarlosCanto  suggested,  Another way to resize the item can be using the scale factor

var newItem = app.selection[0];
var scaleFactor = 0.25;
newItem.resize(scaleFactor * 100, scaleFactor * 100, true);
newItem.position = new Array(0,0);

 

The above snippet, will resize the selected item by 25% and will position at x=0 and y=0.

Likes

Translate

Translate
Community Expert ,
Oct 12, 2022 Oct 12, 2022

Copy link to clipboard

Copied

opening the png file and copying it to the mockup file is not very efficient. I would open the mockup file and "place" (import) the png file instead

 

 

var filePng = "C:\\Temp\\PNG.png";
var fileMockup = "C:\\Temp\\Mockup_001.ai";

var mockup = app.open(File(fileMockup));

var placedPng = mockup.placedItems.add();
placedPng.file = File(filePng);

// you need to figure out the right size and placement;
placedPng.width = 200;
placedPng.height = 300;
placedPng.position = [50, -30];

 

Likes

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 ,
Oct 13, 2022 Oct 13, 2022

Copy link to clipboard

Copied

LATEST

Hi @Alexandre - Alenac2001,

Adding more what @CarlosCanto  suggested,  Another way to resize the item can be using the scale factor

var newItem = app.selection[0];
var scaleFactor = 0.25;
newItem.resize(scaleFactor * 100, scaleFactor * 100, true);
newItem.position = new Array(0,0);

 

The above snippet, will resize the selected item by 25% and will position at x=0 and y=0.

Best regards

Likes

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