英文:
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("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];
// Toggle visibility for layers with matching names
if (namesArray.indexOf(layer.name) >= 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("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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论