我正在用 JS 编写一个 Photoshop 脚本,此时我要求用户选择文件夹位置并将所有这些文件添加到一个数组中。然后我希望解析该数组,以便只保留文件名。
我收到此错误:fileList[i].replace 不是函数
我想这是因为我传递了错误的值或使用了错误的类型。希望有人能解释这个问题并帮助我解决它吗?
//Prompt for folder location
var Path = Folder.selectDialog("Select Folder Location for Renders")
// Use the path to the application and append the samples folder
var samplesFolder = Folder(Path)
var fileList = samplesFolder.getFiles()
for (var i = 0; i < fileList.length; i++)
{
fileList[i] = fileList[i].replace(/^.*[\\\/]/, '')
}
prompt("Complete")
感谢您的宝贵时间,AtB
小
最佳答案
发生错误是因为您需要一个字符串,但它不是一个。
http://jongware.mit.edu/idcs5js_html_3.0.3i/idcs5js/pc_Folder.html说 getFiles
returns an array of File and Folder objects, or null if this object's referenced folder does not exist.
幸运的是,File
和 Folder
都具有以下属性:
- fsName - 引用文件的特定于平台的完整路径名
- fullName - 引用文件的完整路径名(采用 URI 表示法)。
- 名称 - 引用文件的绝对 URI 的文件名部分,不带路径规范
当然,如果您不需要任何路径,而只需要文件名,请使用name
,否则,请使用适合您的replace
命令- fsName
或 fullName
。
所以 - 在你的循环中,你想要:
fileList[i] = fileList[i].name
您可能想要过滤掉最终结果中的文件夹。这将需要在你的循环中进行类似的操作:
if (fileList[i] instanceof Folder) {
fileList.splice(i, 1);
--i; // go back one i, because you just removed an index. Note, if you're not careful, such shenanigans may mess up the second term of the for loop.
continue;
}
最后一个建议:我个人认为创建一个新数组比原位替换更干净。该语言当然支持您正在做的事情,但它仍然让我从文件或文件夹数组
转到字符串数组
。 (当然,您认为您正在执行字符串数组到字符串数组的操作。)这也会简化删除文件夹索引等的任何问题。
关于javascript - 字符串替换功能没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19361738/