我需要一个用于 photoshop 的脚本,它可以创建一个方形或矩形选区(使用矩形选框工具),它会捕捉到参考线,无论参考线的位置如何,有时有 2 个参考线,有时我会添加 4 个参考线。或者如果脚本从指南中选择照片底部就可以了。
首先,应该明白 SO 不是代码编写服务。此外,您应该包括一个最小的、可重现的代码示例。
好的,继续你的问题:
您可以获取具有选择范围的选择的直角坐标。 这只是向这些边界添加水平或垂直指南的情况: 指南的代码取自 script listener.
// Switch off any dialog boxes
displayDialogs = DialogModes.ERROR; // OFF
// Top, Left, Bottom, Right
var sb = get_selection_bounds();
if (sb != undefined)
{
add_guide(sb[0], "v");
add_guide(sb[1], "h");
add_guide(sb[2], "v");
add_guide(sb[3], "h");
}
// Set Display Dialogs back to normal
displayDialogs = DialogModes.ALL; // NORMAL
function add_guide(dist, dir)
{
var vert = true;
if (dir.toLowerCase() == "v") vert = true;
if (dir.toLowerCase() == "h") vert = false;
var idMk = charIDToTypeID( "Mk " );
var desc1583 = new ActionDescriptor();
var idNw = charIDToTypeID( "Nw " );
var desc1584 = new ActionDescriptor();
var idPstn = charIDToTypeID( "Pstn" );
var idPxl = charIDToTypeID( "#Pxl" );
desc1584.putUnitDouble( idPstn, idPxl, dist );
var idOrnt = charIDToTypeID( "Ornt" );
var idOrnt = charIDToTypeID( "Ornt" );
if (vert == true)
{
var idVrtc = charIDToTypeID( "Vrtc" );
desc1584.putEnumerated( idOrnt, idOrnt, idVrtc );
}
else
{
var idHrzn = charIDToTypeID( "Hrzn" );
desc1584.putEnumerated( idOrnt, idOrnt, idHrzn );
}
var idKnd = charIDToTypeID( "Knd " );
var idKnd = charIDToTypeID( "Knd " );
var idDcmn = charIDToTypeID( "Dcmn" );
desc1584.putEnumerated( idKnd, idKnd, idDcmn );
var idnull = charIDToTypeID( "null" );
var ref457 = new ActionReference();
var idDcmn = charIDToTypeID( "Dcmn" );
ref457.putIdentifier( idDcmn, 1015 );
var idGd = charIDToTypeID( "Gd " );
ref457.putIndex( idGd, 1 );
desc1584.putReference( idnull, ref457 );
var idGd = charIDToTypeID( "Gd " );
desc1583.putObject( idNw, idGd, desc1584 );
var idnull = charIDToTypeID( "null" );
var ref458 = new ActionReference();
var idGd = charIDToTypeID( "Gd " );
ref458.putClass( idGd );
desc1583.putReference( idnull, ref458 );
var idguideTarget = stringIDToTypeID( "guideTarget" );
var idguideTarget = stringIDToTypeID( "guideTarget" );
var idguideTargetCanvas = stringIDToTypeID( "guideTargetCanvas" );
desc1583.putEnumerated( idguideTarget, idguideTarget, idguideTargetCanvas );
executeAction( idMk, desc1583, DialogModes.NO );
}
// function GET SELECTION BOUNDS ()
// ----------------------------------------------------------------
function get_selection_bounds()
{
try
{
var t = parseFloat(app.activeDocument.selection.bounds[0]);
var l = parseFloat(app.activeDocument.selection.bounds[1]);
var b = parseFloat(app.activeDocument.selection.bounds[2]);
var r = parseFloat(app.activeDocument.selection.bounds[3]);
// T, L, B, R
// return the results as an array
return [t, l, b, r];
}
catch(eek)
{
return undefined;
}
}
此处创建矩形选区的脚本:
// Get the active document
var doc = app.activeDocument;
// Get the guides
var guides = doc.guides;
// Get the first horizontal and vertical guides
var hGuide1 = guides[0];
var vGuide1 = guides[1];
// Get the second horizontal and vertical guides (if they exist)
var hGuide2 = guides.length > 2 ? guides[2] : null;
var vGuide2 = guides.length > 3 ? guides[3] : null;
// Calculate the selection coordinates based on the guides
var x1 = vGuide1.direction == Direction.VERTICAL ? vGuide1.coordinate : hGuide1.coordinate;
var y1 = hGuide1.direction == Direction.HORIZONTAL ? hGuide1.coordinate : vGuide1.coordinate;
var x2 = vGuide2 != null && vGuide2.direction == Direction.VERTICAL ? vGuide2.coordinate : doc.width.as("px");
var y2 = hGuide2 != null && hGuide2.direction == Direction.HORIZONTAL ? hGuide2.coordinate : doc.height.as("px");
// Create the rectangular selection
doc.selection.select([[x1, y1], [x2, y1], [x2, y2], [x1, y2]], SelectionType.REPLACE, 0, false);