Skip to main content
Inspiring
January 6, 2023
Answered

Moving a Selected Item twice it's width

  • January 6, 2023
  • 3 replies
  • 805 views

Hello I have a Script that will move a selected item but I can't seem to get it to copy, where am I going wrong?

var doc = app.activeDocument;
var sel = doc.selection;
if (sel.length > 0) {
  var item = sel[0];
  var width = item.width;
  item.translate(width * 2, 0);
} else {
  alert("No items are selected.");
}

 

 

var doc = app.activeDocument;
var sel = doc.selection;
if (sel.length > 0) {
  var item = sel[0];
  var width = item.width;
  var copy = item.duplicate();
  copy.translate(width * 2, 0);
} else {
  alert("No items are selected.");
}

 Thank you!

This topic has been closed for replies.
Correct answer pixxxelschubser

There are more reserved words in your code. Try to avoid them.

 

For the selected item you can use eg:

 

var doc = app.activeDocument;
var sel = doc.selection;
if (sel.length > 0) {
  var pIt = sel[0];
  var wdt = item.width;
  var dub = pIt.duplicate();
  doc.selection = null;
  dub.translate(wdt*2, 0);
  dub.selected = true;
} else {
  alert("No items are selected.");
}

 

 

Have fun
😉

3 replies

pixxxelschubser
Community Expert
Community Expert
January 6, 2023

😄
@femkeblanco was two minutes faster.

pixxxelschubser
Community Expert
pixxxelschubserCommunity ExpertCorrect answer
Community Expert
January 6, 2023

There are more reserved words in your code. Try to avoid them.

 

For the selected item you can use eg:

 

var doc = app.activeDocument;
var sel = doc.selection;
if (sel.length > 0) {
  var pIt = sel[0];
  var wdt = item.width;
  var dub = pIt.duplicate();
  doc.selection = null;
  dub.translate(wdt*2, 0);
  dub.selected = true;
} else {
  alert("No items are selected.");
}

 

 

Have fun
😉

femkeblanco
Legend
January 6, 2023

"copy" is a reserved word.  Use something else as the name of the variable, e.g. "copy1". 

 

var doc = app.activeDocument;
var sel = doc.selection;
if (sel.length > 0) {
  var item = sel[0];
  var width = item.width;
  var copy1 = item.duplicate();
  copy1.translate(width * 2, 0);
} else {
  alert("No items are selected.");
}

 

Inspiring
January 6, 2023

Follow Up question if I may @femkeblanco, how do I end this script so only the duplicate item is actively selected?

femkeblanco
Legend
January 6, 2023
var doc = app.activeDocument;
var sel = doc.selection;
if (sel.length > 0) {
  var item = sel[0];
  var width = item.width;
  var copy1 = item.duplicate();
  copy1.translate(width * 2, 0);
} else {
  alert("No items are selected.");
}
app.selection = null;
copy1.selected = true;