0

我正在用 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

小号

4

1 回答 1

2

发生错误是因为您期待一个字符串,但它不是一个。

http://jongware.mit.edu/idcs5js_html_3.0.3i/idcs5js/pc_Folder.htmlgetFiles

返回 File 和 Folder 对象的数组,如果此对象的引用文件夹不存在,则返回 null。

幸运的是,两者都File具有Folder以下属性:

  • fsName - 被引用文件的特定于平台的完整路径名
  • fullName - URI 表示法中引用文件的完整路径名。
  • name - 引用文件的绝对 URI 的文件名部分,没有路径规范

当然,如果您不想要任何路径,而只想要文件名,请使用name,否则,请使用replace适合您的命令 -fsNamefullName.

所以 - 在你的循环中,你想要:

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;
}

最后一个建议:我个人会觉得制作一个新阵列更干净,而不是在适当的位置进行替换。该语言当然支持你正在做的事情,但它仍然让我抽搐着从File or Folder arraystring array. (当然,您认为您正在对字符串数组执行字符串数组。)这也将简化删除文件夹索引等的任何问题。

于 2013-10-14T13:59:09.153 回答