在Photoshop中显示指定图层的脚本(JSX)。

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

Script To Show Specified Layers in Photoshop(JSX)

问题

I'm trying to write a script that toggles the specific layers that I enter into a prompt window, but I keep getting "namesArray.indexOf is not a function". Here is the code-

// 提示用户输入以逗号分隔的图层名称
var layerNames = prompt("输入以逗号分隔的图层名称:");

// 将输入字符串转换为名称数组
var namesArray = layerNames.split(",");

// 循环遍历文档中的所有图层
for (var i = 0; i < app.activeDocument.layers.length; i++) {
  var layer = app.activeDocument.layers[i];

  // 切换匹配名称的图层的可见性
  if (namesArray.indexOf(layer.name) >= 0) {
    layer.visible = !layer.visible;
  }
}

How do I go about fixing this error?

英文:

I'm trying to write a script that toggles the specific layers that I enter into a prompt window, but I keep getting "namesArray.indexOf is not a function". Here is the code-

// Prompt the user for comma-separated layer names
var layerNames = prompt(&quot;Enter layer names separated by commas:&quot;);

// Convert the input string into an array of names
var namesArray = layerNames.split(&quot;,&quot;);

// Loop through all layers in the document
for (var i = 0; i &lt; app.activeDocument.layers.length; i++) {
  var layer = app.activeDocument.layers[i];

  // Toggle visibility for layers with matching names
  if (namesArray.indexOf(layer.name) &gt;= 0) {
    layer.visible = !layer.visible;
  }
}

How do I go about fixing this error?

答案1

得分: 2

It seems that Photoshop's version of indexOf only works on strings - it doesn't work on your array. I get the same error. I can only think that the version of ECMA script that Photoshop uses is so antique that the above is true. See here for more details.

A quick workaround is to add a function that does the same trick:

// Prompt the user for comma-separated layer names
var layerNames = prompt("Enter layer names separated by commas:");

// Convert the input string into an array of names
var namesArray = layerNames.split(",");

// Loop through all layers in the document
for (var i = 0; i < app.activeDocument.layers.length; i++)
{
  var layer = app.activeDocument.layers[i];

  var idx = get_index(namesArray, layer.name);

  // Toggle visibility for layers with matching names
  // if (namesArray.indexOf(layer.name) >= 0) 
  if (idx >= 0) 
  {
    layer.visible = !layer.visible;
  }
}

function get_index(arr, str)
{
  for (var i = 0; i < arr.length; i++)
  {
    if (arr[i] == str) return i;
  }
}
英文:

It seems that Photoshop's version of indexOf only works on strings - it doesn't work on your array. I get the same error. I can only think that the version of ECMA script that Photoshop uses is so antique that the above is true. See here for more details.

A quick workaround is to add a function that does the same trick:

// Prompt the user for comma-separated layer names
var layerNames = prompt(&quot;Enter layer names separated by commas:&quot;);

// Convert the input string into an array of names
var namesArray = layerNames.split(&quot;,&quot;);

// Loop through all layers in the document
for (var i = 0; i &lt; app.activeDocument.layers.length; i++)
{
  var layer = app.activeDocument.layers[i];

  var idx = get_index(namesArray, layer.name);

  // Toggle visibility for layers with matching names
  // if (namesArray.indexOf(layer.name) &gt;= 0) 
  if (idx &gt;= 0) 
  {
    layer.visible = !layer.visible;
  }
}


function get_index(arr, str)
{
  for (var i = 0; i &lt; arr.length; i++)
  {
    if (arr[i] == str) return i;
  }
}

huangapple
  • 本文由 发表于 2023年3月9日 20:55:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684925.html
匿名

发表评论

匿名网友

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

确定