Copy link to clipboard
Copied
Hi I'm trying to prompt the user to draw a rectangle in the document with the goal of then documenting the coordinates of said rectangle. Any idea how? Any ideas much appreciated!
1 Correct answer
Thanks, Joonass! As a beginner I assumed that the AM code was some type of boolean, it recorded as .NO so I assumed that .YES was the only other value which of course errored!
.ALL worked, fantastic!
...// Capture the current ruler units
var savedRuler = app.preferences.rulerUnits;
// Set the current ruler units to pixels
app.preferences.rulerUnits = Units.PIXELS;
// Set the current doc as the active doc
var doc = app.activeDocument;
// Select all
doc.selection.selectAll();
// Ask the user to transform the
Explore related tutorials & articles
Copy link to clipboard
Copied
Set a rectangle then put the user into an interactive transform selection step for them to transform the rectangle to size and position. When they commit the transform use slection bounds,
Copy link to clipboard
Copied
I used a selection, however, I am stuck at the interactive transform. Easy with an action, however all my attempts at converting the "Transform Selection" command into scripting have failed.
var savedRuler = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var doc = app.activeDocument;
doc.selection.selectAll();
// Add an interactive transform of the selection
var selBounds = doc.selection.bounds;
// var
// xLeft = selBounds[0],
// yTop = selBounds[1],
// xRight = selBounds[2],
// yBottom = selBounds[3];
alert(selBounds);
app.preferences.rulerUnits = savedRuler;
Copy link to clipboard
Copied
I'm thinking Scripting listener code where you change dialogmode to "ALL"?
Copy link to clipboard
Copied
Thanks, Joonass! As a beginner I assumed that the AM code was some type of boolean, it recorded as .NO so I assumed that .YES was the only other value which of course errored!
.ALL worked, fantastic!
// Capture the current ruler units
var savedRuler = app.preferences.rulerUnits;
// Set the current ruler units to pixels
app.preferences.rulerUnits = Units.PIXELS;
// Set the current doc as the active doc
var doc = app.activeDocument;
// Select all
doc.selection.selectAll();
// Ask the user to transform the selection into size/position
alert('Size and position the selection and commit the transform')
// AM code to transform selection
var idTrnf = charIDToTypeID( "Trnf" );
var desc276 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref50 = new ActionReference();
var idChnl = charIDToTypeID( "Chnl" );
var idfsel = charIDToTypeID( "fsel" );
ref50.putProperty( idChnl, idfsel );
desc276.putReference( idnull, ref50 );
// Change NO to ALL for interactive transform
executeAction( idTrnf, desc276, DialogModes.ALL );
// Set selection bounds
var selBounds = doc.selection.bounds;
// Code for possible future use
// var
// xLeft = selBounds[0],
// yTop = selBounds[1],
// xRight = selBounds[2],
// yBottom = selBounds[3];
// Deselect
app.activeDocument.selection.deselect();
// Report the selection bounds, this would probably be replaced with the actual code to "do something"...
alert(selBounds);
// Restore the original saved ruler units
app.preferences.rulerUnits = savedRuler;
Copy link to clipboard
Copied
try{// in case user cancels
TransformSelection();
}catch(e){}
//========= Interactive Transform selection ==============
function TransformSelection() {
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putProperty(app.charIDToTypeID('Chnl'), app.stringIDToTypeID("selection"));
desc1.putReference(app.charIDToTypeID('null'), ref1);
executeAction(app.charIDToTypeID('Trnf'), desc1, DialogModes.ALL );
};
Copy link to clipboard
Copied
Hi JJ, thank you for supplying actual code rather than just saying you may wish to "add a try/catch block".
However plugging in your code, I see no difference in execution whether one cancels or accepts the free transform step? Was the code incomplete? As I am new to scripting, perhaps you were presuming that I would add something to flesh it out.
Copy link to clipboard
Copied
I believe You should see a difference I think though I did not test. Your code I think should through a script error user cancel the transform selection and terminate if the user hits enter to accept the current selection. My code try catch catches that and uses the selection set by the script. It Accepts the set selection.
Yes I just tested your code It does through that error. In Actions I record the transform selection that I make interactive in the action are a rotate 180 degrees. Which does not change the actual selected rectangle area. When the user hits enter to accept the set selection the recorded transform rotates the selection 180 degrees because the user made no change. So there is no user canceled the transform message because the receded step did make a change. I do not know why Photoshop generates the massage when the current selection is not changed. I believe it should accept the current secretion and continue the script. I feel only ESC should indicated cancel but I'm not Adobe.... So I catch that and continue the scripts execution because the selection I set in the Script or Action is the selection I believe the user want to set.

