Script自动将选择框捕捉到添加的参考线。

huangapple go评论55阅读模式
英文:

Script auto make selection snap to added guides line

问题

I need a script for Photoshop which can create a square or rectangular selection (use rectangular marquee tool) and it will snap to the guides line, no matter the position of the guides, sometimes with 2 guides sometimes I will add 4 guides. Or it'll be ok if the script make a selection from the guide to the bottom of the photo.

以下是一些其他选项,我期望的:
创建选择 1
创建选择 2

英文:

I need a script for photoshop which can create a square or rectangular selection (use rectangular marquee tool) and it will snap to the guides line, no matter the position of the guides, sometimes with 2 guides sometimes I will add 4 guides. Or it'll be ok if the script make a selection from the guide to the bottom of the photo.

make selection snap to guides

Here are some other options that I'm expecting
make selection 1
make selection 2

答案1

得分: 0

以下是翻译好的部分:

首先,应该理解 Stack Overflow 不是代码编写服务。此外,你应该包括一个代码的最小可复现示例

好的,继续你的问题:

你可以获取选择区域的矩形坐标,只需将水平或垂直参考线添加到这些边界上。参考线的代码是从脚本监听器中提取的。

// 关闭任何对话框
displayDialogs = DialogModes.ERROR; // 关闭

// 顶部、左侧、底部、右侧
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");
}

// 设置对话框显示模式回到正常
displayDialogs = DialogModes.ALL; // 正常
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()
{
  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]);

    // 返回结果作为数组
    return [t, l, b, r];
  }
  catch(eek)
  {
    return undefined;
  }
}

Script自动将选择框捕捉到添加的参考线。

英文:

First up, should understand that SO is not a code writing service. Also, you should include a minimal, reproducible example of code.

Ok, on with your question:

You can get the rectangular coordinates of a selection with selection bounds.
It's just a case of adding a horizontal or vertical guide to those bounds:
The code for the guides was taken from the 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;
}
}

Script自动将选择框捕捉到添加的参考线。

答案2

得分: 0

这是创建矩形选择的脚本:

// 获取活动文档
var doc = app.activeDocument;

// 获取参考线
var guides = doc.guides;

// 获取第一个水平和垂直参考线
var hGuide1 = guides[0];
var vGuide1 = guides[1];

// 获取第二个水平和垂直参考线(如果存在)
var hGuide2 = guides.length > 2 ? guides[2] : null;
var vGuide2 = guides.length > 3 ? guides[3] : null;

// 根据参考线计算选择区域的坐标
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");

// 创建矩形选择
doc.selection.select([[x1, y1], [x2, y1], [x2, y2], [x1, y2]], SelectionType.REPLACE, 0, false);

要运行它,请创建一个扩展名为jsx的文件,例如:script.jsx,然后依次选择“文件->脚本->浏览...”并选择script.jsx文件。

英文:

Here script that creates a rectangular selection:

// 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);

To run it, create a file with the extension jsx. For example: script.jsx

Then File->Scripts->Browse... select the file script.jsx

huangapple
  • 本文由 发表于 2023年4月13日 14:51:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76002451.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定