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

Help for Script modification, it creates selection copies , looking for limit width

Contributor ,
Nov 24, 2022 Nov 24, 2022

Copy link to clipboard

Copied

Hello,

the following script create copies of selection, (user prompt for copies and margin, selected 20x20 cm rectangle)

Currently, there is no horizontal limit

siomosp_0-1669308193466.jpeg

Can someone adapt it, setting horizontal limit, eg to 110 cm 

Setting e.g 110 cm horizontal limit result should be

 

(After reaching the width limit, the "copies" created at  new row

 

The script

 

 

// A Script for Adobe Illustrator to Duplicate Items Hor/Ver with Margin
// Originally by https://github.com/hilukasz
// This https://gist.github.com/ExtremeGTX/2ca86e77c78153a3b43b6eb5088a426f
// Modified by Mohamed ElShahawi (ExtremeGTX)

app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
var doc = app.activeDocument;
var thislayer = app.activeDocument.activelayer;
var selected = doc.selection;
var selectedHeight = selected[0].height;
var selectedWidth = selected[0].width;
var OriginX = selected[0].position[0];
var OriginY = selected[0].position[1];
var repeatAmount,
      myInput,
      splitInput,
      margin,
      selectedPosition,
	  Direction;

// Run
userPrompt();
Direction == 'v' ? DuplicateVertical() :  DuplicateHorizontal();

function DuplicateHorizontal(){
	selectedPosition = selected.position;
	var newItem = selected[0].duplicate( thislayer, ElementPlacement.INSIDE );  
	for(i=0; i< repeatAmount -1 ; i++){ 
		var newPosX = selectedWidth + newItem.position[0] + margin;
		newItem.position = [newPosX,newItem.position[1]];
		doc.selection = null;
		newItem.selected = true;
		var selectedPosition = selected.position;
		newItem.duplicate( thislayer, ElementPlacement.INSIDE );  
		// app.redraw();
	}
		newItem.remove();
	app.redraw(); 
}

function DuplicateVertical(){
	selectedPosition = selected.position;
	var newItem = selected[0].duplicate( thislayer, ElementPlacement.INSIDE );  
	for(i=0; i< repeatAmount -1 ; i++){ 
		var newPosY = selectedHeight - newItem.position[1] + margin;
		newItem.position = [newItem.position[0],-newPosY];
		doc.selection = null;
		newItem.selected = true;
		var selectedPosition = selected.position;
		newItem.duplicate( thislayer, ElementPlacement.INSIDE );  
	}
	newItem.remove();
	app.redraw();
}

function userPrompt(){
myInput = prompt('Ποσότητα, Διάστημα','10 0.2');
splitInput = myInput.split(" ");
repeatAmount = Number(splitInput[0]);
margin = Number(splitInput[1]*28.346456693);

 

 

 

TOPICS
Scripting

Views

270

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 1 Correct answer

Advisor , Nov 24, 2022 Nov 24, 2022

I've made two changes, both bound by empty comments (//). 

 

var limit = 110;  // horizontal, cm

// A Script for Adobe Illustrator to Duplicate Items Hor/Ver with Margin
// Originally by https://github.com/hilukasz
// This https://gist.github.com/ExtremeGTX/2ca86e77c78153a3b43b6eb5088a426f
// Modified by Mohamed ElShahawi (ExtremeGTX)

app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
var doc = app.activeDocument;
var thislayer = app.activeDocument.activelayer;
var selected = doc
...

Likes

Translate

Translate
Advisor ,
Nov 24, 2022 Nov 24, 2022

Copy link to clipboard

Copied

I've made two changes, both bound by empty comments (//). 

 

var limit = 110;  // horizontal, cm

// A Script for Adobe Illustrator to Duplicate Items Hor/Ver with Margin
// Originally by https://github.com/hilukasz
// This https://gist.github.com/ExtremeGTX/2ca86e77c78153a3b43b6eb5088a426f
// Modified by Mohamed ElShahawi (ExtremeGTX)

app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
var doc = app.activeDocument;
var thislayer = app.activeDocument.activelayer;
var selected = doc.selection;
var selectedHeight = selected[0].height;
var selectedWidth = selected[0].width;
var OriginX = selected[0].position[0];
var OriginY = selected[0].position[1];
var repeatAmount,
    myInput,
    splitInput,
    margin,
    selectedPosition,
    Direction;

// Run
userPrompt();
Direction == 'v' ? DuplicateVertical() :  DuplicateHorizontal();

function DuplicateHorizontal() {
    selectedPosition = selected.position;
    var newItem = selected[0].duplicate(thislayer, ElementPlacement.INSIDE);
    //
    var startPosition = newItem.position[0];
    //
    for(i = 0; i < repeatAmount - 1 ; i++){ 
        var newPosX = selectedWidth + newItem.position[0] + margin;
        //
        if (newPosX - startPosition < limit * 28.346456693 - selectedWidth) {
            newItem.position = [newPosX, newItem.position[1]];
        } else {
            newItem.position = [
                startPosition, 
                newItem.position[1] - selectedWidth - margin
            ];
        }
        //
        doc.selection = null;
        newItem.selected = true;
        var selectedPosition = selected.position;
        newItem.duplicate(thislayer, ElementPlacement.INSIDE);  
    }
    newItem.remove();
    app.redraw(); 
}

// function DuplicateVertical(){
// 	  selectedPosition = selected.position;
// 	  var newItem = selected[0].duplicate( thislayer, ElementPlacement.INSIDE );  
// 	  for(i=0; i< repeatAmount -1 ; i++){ 
// 		    var newPosY = selectedHeight - newItem.position[1] + margin;
// 		    newItem.position = [newItem.position[0],-newPosY];
// 		    doc.selection = null;
// 		    newItem.selected = true;
// 		    var selectedPosition = selected.position;
// 		    newItem.duplicate( thislayer, ElementPlacement.INSIDE );  
// 	  }
// 	  newItem.remove();
// 	  app.redraw();
// }

function userPrompt(){
    myInput = prompt('Ποσότητα, Διάστημα','10 0.2');
    splitInput = myInput.split(" ");
    repeatAmount = Number(splitInput[0]);
    margin = Number(splitInput[1]*28.346456693);
}

 

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
Contributor ,
Nov 24, 2022 Nov 24, 2022

Copy link to clipboard

Copied

LATEST

Thank you,  @femkeblanco

exactly what I needed!!!

 

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