Skip to main content
Adobe Photoshop Family

33 Messages

 • 

674 Points

Wed, Sep 5, 2012 8:42 PM

1

Photoshop: Add guides to a selection (a javascript solution)

I posted this question as part of a thread on deleting paths. But as Paul Riggott was kind enough to write a script to solve my need, I thought I'd post the need here along with Paul's script.

The Need: to be able to add guides to a square or rectangular selection.
The Why: On most of the commercial photos I shoot, I need to put cropping guides on the photo so that later, the client or anyone, would have an idea of what I / we were thinking when the photo was created. I make a selection of the cropped area and then drag out 4 guides to the sides of the selection. This procedure is done over and over. So I wanted to way to speed up process. One click.

Open up Photoshop ExtendKit application (it came with Photoshop). Create a new document, copy and paste the code in the window. Save as (give it a name) in applications > Photoshop CSx (x is you version, mine is CS5) > presets > Scripts
Close photoshop if it was open, relaunch so it can load the new script.

Here is the code Paul provided to do just that:

#target Photoshop
main();
function main(){
if(!documents.length) return;
var startRulerUnits = preferences.rulerUnits;
try{
preferences.rulerUnits = Units.PIXELS
var SB = activeDocument.selection.bounds;
}catch(e){return;}
guideLine(SB[1].value,"Hrzn");
guideLine(SB[3].value,"Hrzn");
guideLine(SB[0].value,"Vrtc");
guideLine(SB[2].value,"Vrtc");
preferences.rulerUnits = startRulerUnits;
}
function guideLine(position, type){
var desc = new ActionDescriptor();
var desc2 = new ActionDescriptor();
desc2.putUnitDouble( app.charIDToTypeID('Pstn'), app.charIDToTypeID('#Pxl'), position );
desc2.putEnumerated( app.charIDToTypeID('Ornt'), app.charIDToTypeID('Ornt'), app.charIDToTypeID(type) );
desc.putObject( app.charIDToTypeID('Nw '), app.charIDToTypeID('Gd '), desc2 );
executeAction( app.charIDToTypeID('Mk '), desc, DialogModes.NO );
};

Thanks Paul for your valuable skills!

JeffN

Responses

111 Messages

 • 

1.8K Points

2 years ago

Unfortunately I receive an error in CC2018 when running the script:

Illegal argument, line 21 (3rd line from the bottom)

So I was forced to work out how to do this using an action (dependent on CC2014 or later).


47 Messages

 • 

1.4K Points

You could use a shape instead of a selection and then use the native command »View > New guides from shape«.

111 Messages

 • 

1.8K Points

Thanks Olaf, unfortunately no, a selection was being generated by a script and the guides were required to be added to the temporary selection bounds. As I said, I worked out an action based hack to make the selection into raster fill that qualified as a shape for use with the new guides from shape feature. 

I was just lamenting the multi-step work around that had to be created as a hack.

703 Messages

 • 

9.4K Points

Looks like the script above was just copied wrong.
Try the one below.




#target Photoshop
main();
function main(){
if(!documents.length) return;
var startRulerUnits = preferences.rulerUnits;
try{
preferences.rulerUnits = Units.PIXELS
var SB = activeDocument.selection.bounds;
}catch(e){return;}
guideLine(SB[1].value,"Hrzn");
guideLine(SB[3].value,"Hrzn");
guideLine(SB[0].value,"Vrtc");
guideLine(SB[2].value,"Vrtc");
preferences.rulerUnits = startRulerUnits;
}
function guideLine(position, type){
var desc = new ActionDescriptor();
var desc2 = new ActionDescriptor();
desc2.putUnitDouble( app.charIDToTypeID('Pstn'), app.charIDToTypeID('#Pxl'), position );
desc2.putEnumerated( app.charIDToTypeID('Ornt'), app.charIDToTypeID('Ornt'), app.charIDToTypeID(type) );
desc.putObject( app.charIDToTypeID('Nw  '), app.charIDToTypeID('Gd  '), desc2 );
executeAction( app.charIDToTypeID('Mk  '), desc, DialogModes.NO );
};

6 Messages

 • 

178 Points

The problem is this forum is removing spaces
desc.putObject( app.charIDToTypeID('Nw '), app.charIDToTypeID('Gd '), desc2 );
executeAction( app.charIDToTypeID('Mk '), desc, DialogModes.NO );

the 'Nw ' should be 'Nw space space' 'Gd ' should be 'Gd space space'
also 'Mk ' should be 'Mk space space'
charIDs should be four characters long and this forum is truncating double space to one space.

111 Messages

 • 

1.8K Points

Thanks for the help JEA and Geoff, the code works now!