javascript - 字符串替换功能没有响应

标签 javascript string photoshop

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

returns an array of File and Folder objects, or null if this object's referenced folder does not exist.

幸运的是,FileFolder 都具有以下属性:

  • fsName - 引用文件的特定于平台的完整路径名
  • fullName - 引用文件的完整路径名(采用 URI 表示法)。
  • 名称 - 引用文件的绝对 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;
}

最后一个建议:我个人认为创建一个新数组比原位替换更干净。该语言当然支持您正在做的事情,但它仍然让我从文件或文件夹数组转到字符串数组。 (当然,您认为您正在执行字符串数组到字符串数组的操作。)这也会简化删除文件夹索引等的任何问题。

关于javascript - 字符串替换功能没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19361738/

相关文章:

ruby - `String#===` 文档示例

android - 适用于 10.0 英寸安卓平板电脑的 photoshop 中的尺寸和分辨率

javascript - 使用工厂内部路由 Controller

javascript - 以最佳、快速、最小的方式检查变量是否存在 JavaScript 和 jQuery

javascript - 获取最后选择的值

Java 字符串排序错误

c - 在新字符串中仅打印一次重叠字符(字符串组合)

algorithm - 模拟 PhotoShop 的 "Color Range"算法

c# - CONTENT-AWARE FILL 在 Photoshop 中如何工作?

javascript - Webview localStorage.setItem(javascript) 无法在加载 url 之前设置 (android)