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

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

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 不是代码编写服务。此外,你应该包括一个代码的最小可复现示例

好的,继续你的问题:

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

  1. // 关闭任何对话框
  2. displayDialogs = DialogModes.ERROR; // 关闭
  3. // 顶部、左侧、底部、右侧
  4. var sb = get_selection_bounds();
  5. if (sb != undefined)
  6. {
  7. add_guide(sb[0], "v");
  8. add_guide(sb[1], "h");
  9. add_guide(sb[2], "v");
  10. add_guide(sb[3], "h");
  11. }
  12. // 设置对话框显示模式回到正常
  13. displayDialogs = DialogModes.ALL; // 正常
  1. function add_guide(dist, dir)
  2. {
  3. var vert = true;
  4. if (dir.toLowerCase() == "v") vert = true;
  5. if (dir.toLowerCase() == "h") vert = false;
  6. var idMk = charIDToTypeID("Mk ");
  7. var desc1583 = new ActionDescriptor();
  8. var idNw = charIDToTypeID("Nw ");
  9. var desc1584 = new ActionDescriptor();
  10. var idPstn = charIDToTypeID("Pstn");
  11. var idPxl = charIDToTypeID("#Pxl");
  12. desc1584.putUnitDouble(idPstn, idPxl, dist);
  13. var idOrnt = charIDToTypeID("Ornt");
  14. var idOrnt = charIDToTypeID("Ornt");
  15. if (vert == true)
  16. {
  17. var idVrtc = charIDToTypeID("Vrtc");
  18. desc1584.putEnumerated(idOrnt, idOrnt, idVrtc);
  19. }
  20. else
  21. {
  22. var idHrzn = charIDToTypeID("Hrzn");
  23. desc1584.putEnumerated(idOrnt, idOrnt, idHrzn);
  24. }
  25. var idKnd = charIDToTypeID("Knd ");
  26. var idKnd = charIDToTypeID("Knd ");
  27. var idDcmn = charIDToTypeID("Dcmn");
  28. desc1584.putEnumerated(idKnd, idKnd, idDcmn);
  29. var idnull = charIDToTypeID("null");
  30. var ref457 = new ActionReference();
  31. var idDcmn = charIDToTypeID("Dcmn");
  32. ref457.putIdentifier(idDcmn, 1015);
  33. var idGd = charIDToTypeID("Gd ");
  34. ref457.putIndex(idGd, 1);
  35. desc1584.putReference(idnull, ref457);
  36. var idGd = charIDToTypeID("Gd ");
  37. desc1583.putObject(idNw, idGd, desc1584);
  38. var idnull = charIDToTypeID("null");
  39. var ref458 = new ActionReference();
  40. var idGd = charIDToTypeID("Gd ");
  41. ref458.putClass(idGd);
  42. desc1583.putReference(idnull, ref458);
  43. var idguideTarget = stringIDToTypeID("guideTarget");
  44. var idguideTarget = stringIDToTypeID("guideTarget");
  45. var idguideTargetCanvas = stringIDToTypeID("guideTargetCanvas");
  46. desc1583.putEnumerated(idguideTarget, idguideTarget, idguideTargetCanvas);
  47. executeAction(idMk, desc1583, DialogModes.NO);
  48. }
  1. // 获取选择区域的边界
  2. function get_selection_bounds()
  3. {
  4. try
  5. {
  6. var t = parseFloat(app.activeDocument.selection.bounds[0]);
  7. var l = parseFloat(app.activeDocument.selection.bounds[1]);
  8. var b = parseFloat(app.activeDocument.selection.bounds[2]);
  9. var r = parseFloat(app.activeDocument.selection.bounds[3]);
  10. // 返回结果作为数组
  11. return [t, l, b, r];
  12. }
  13. catch(eek)
  14. {
  15. return undefined;
  16. }
  17. }

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.

  1. // Switch off any dialog boxes
  2. displayDialogs = DialogModes.ERROR; // OFF
  3. // Top, Left, Bottom, Right
  4. var sb = get_selection_bounds();
  5. if (sb != undefined)
  6. {
  7. add_guide(sb[0], "v");
  8. add_guide(sb[1], "h");
  9. add_guide(sb[2], "v");
  10. add_guide(sb[3], "h");
  11. }
  12. // Set Display Dialogs back to normal
  13. displayDialogs = DialogModes.ALL; // NORMAL
  14. function add_guide(dist, dir)
  15. {
  16. var vert = true;
  17. if (dir.toLowerCase() == "v") vert = true;
  18. if (dir.toLowerCase() == "h") vert = false;
  19. var idMk = charIDToTypeID( "Mk " );
  20. var desc1583 = new ActionDescriptor();
  21. var idNw = charIDToTypeID( "Nw " );
  22. var desc1584 = new ActionDescriptor();
  23. var idPstn = charIDToTypeID( "Pstn" );
  24. var idPxl = charIDToTypeID( "#Pxl" );
  25. desc1584.putUnitDouble( idPstn, idPxl, dist );
  26. var idOrnt = charIDToTypeID( "Ornt" );
  27. var idOrnt = charIDToTypeID( "Ornt" );
  28. if (vert == true)
  29. {
  30. var idVrtc = charIDToTypeID( "Vrtc" );
  31. desc1584.putEnumerated( idOrnt, idOrnt, idVrtc );
  32. }
  33. else
  34. {
  35. var idHrzn = charIDToTypeID( "Hrzn" );
  36. desc1584.putEnumerated( idOrnt, idOrnt, idHrzn );
  37. }
  38. var idKnd = charIDToTypeID( "Knd " );
  39. var idKnd = charIDToTypeID( "Knd " );
  40. var idDcmn = charIDToTypeID( "Dcmn" );
  41. desc1584.putEnumerated( idKnd, idKnd, idDcmn );
  42. var idnull = charIDToTypeID( "null" );
  43. var ref457 = new ActionReference();
  44. var idDcmn = charIDToTypeID( "Dcmn" );
  45. ref457.putIdentifier( idDcmn, 1015 );
  46. var idGd = charIDToTypeID( "Gd " );
  47. ref457.putIndex( idGd, 1 );
  48. desc1584.putReference( idnull, ref457 );
  49. var idGd = charIDToTypeID( "Gd " );
  50. desc1583.putObject( idNw, idGd, desc1584 );
  51. var idnull = charIDToTypeID( "null" );
  52. var ref458 = new ActionReference();
  53. var idGd = charIDToTypeID( "Gd " );
  54. ref458.putClass( idGd );
  55. desc1583.putReference( idnull, ref458 );
  56. var idguideTarget = stringIDToTypeID( "guideTarget" );
  57. var idguideTarget = stringIDToTypeID( "guideTarget" );
  58. var idguideTargetCanvas = stringIDToTypeID( "guideTargetCanvas" );
  59. desc1583.putEnumerated( idguideTarget, idguideTarget, idguideTargetCanvas );
  60. executeAction( idMk, desc1583, DialogModes.NO );
  61. }
  62. // function GET SELECTION BOUNDS ()
  63. // ----------------------------------------------------------------
  64. function get_selection_bounds()
  65. {
  66. try
  67. {
  68. var t = parseFloat(app.activeDocument.selection.bounds[0]);
  69. var l = parseFloat(app.activeDocument.selection.bounds[1]);
  70. var b = parseFloat(app.activeDocument.selection.bounds[2]);
  71. var r = parseFloat(app.activeDocument.selection.bounds[3]);
  72. // T, L, B, R
  73. // return the results as an array
  74. return [t, l, b, r];
  75. }
  76. catch(eek)
  77. {
  78. return undefined;
  79. }
  80. }

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

答案2

得分: 0

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

  1. // 获取活动文档
  2. var doc = app.activeDocument;
  3. // 获取参考线
  4. var guides = doc.guides;
  5. // 获取第一个水平和垂直参考线
  6. var hGuide1 = guides[0];
  7. var vGuide1 = guides[1];
  8. // 获取第二个水平和垂直参考线(如果存在)
  9. var hGuide2 = guides.length > 2 ? guides[2] : null;
  10. var vGuide2 = guides.length > 3 ? guides[3] : null;
  11. // 根据参考线计算选择区域的坐标
  12. var x1 = vGuide1.direction == Direction.VERTICAL ? vGuide1.coordinate : hGuide1.coordinate;
  13. var y1 = hGuide1.direction == Direction.HORIZONTAL ? hGuide1.coordinate : vGuide1.coordinate;
  14. var x2 = vGuide2 != null && vGuide2.direction == Direction.VERTICAL ? vGuide2.coordinate : doc.width.as("px");
  15. var y2 = hGuide2 != null && hGuide2.direction == Direction.HORIZONTAL ? hGuide2.coordinate : doc.height.as("px");
  16. // 创建矩形选择
  17. 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:

  1. // Get the active document
  2. var doc = app.activeDocument;
  3. // Get the guides
  4. var guides = doc.guides;
  5. // Get the first horizontal and vertical guides
  6. var hGuide1 = guides[0];
  7. var vGuide1 = guides[1];
  8. // Get the second horizontal and vertical guides (if they exist)
  9. var hGuide2 = guides.length > 2 ? guides[2] : null;
  10. var vGuide2 = guides.length > 3 ? guides[3] : null;
  11. // Calculate the selection coordinates based on the guides
  12. var x1 = vGuide1.direction == Direction.VERTICAL ? vGuide1.coordinate : hGuide1.coordinate;
  13. var y1 = hGuide1.direction == Direction.HORIZONTAL ? hGuide1.coordinate : vGuide1.coordinate;
  14. var x2 = vGuide2 != null && vGuide2.direction == Direction.VERTICAL ? vGuide2.coordinate : doc.width.as("px");
  15. var y2 = hGuide2 != null && hGuide2.direction == Direction.HORIZONTAL ? hGuide2.coordinate : doc.height.as("px");
  16. // Create the rectangular selection
  17. 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:

确定