You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Photoshop 2020脚本中获取活动文件的驱动器盘符?

Photoshop脚本提取活动文档驱动器盘符的解决方案

问题根源

app.activeDocument.path返回的是Adobe脚本模型中的Folder对象,并非JavaScript原生字符串类型,所以直接调用toLowerCase()、substring()等JS内置字符串方法会触发"xxx is not a function"错误。

解决步骤

  1. 将Folder对象转为原生路径字符串:使用Folder对象的fsName属性,它会返回系统原生格式的路径字符串(Windows下如C:\Projects\Demo.psd,Mac下如/Users/xxx/Demo.psd)。
  2. 提取Windows驱动器盘符:针对Windows系统,盘符是路径的前2个字符,或通过分割路径的方式获取。

示例代码

// 获取活动文档的路径Folder对象
var docFolder = app.activeDocument.path;
// 转换为系统原生路径字符串
var docPathStr = docFolder.fsName;

// 方法1:截取前两位作为盘符(Windows专用)
var driveLetter = docPathStr.substring(0, 2);

// 方法2:通过分割反斜杠获取盘符(注意Windows路径需用双反斜杠转义)
var driveLetter = docPathStr.split("\\")[0];

// 输出验证
alert("驱动器盘符:" + driveLetter);

跨系统兼容(可选)

如果需要同时兼容Windows和Mac系统,可添加系统判断:

var docPathStr = app.activeDocument.path.fsName;
var rootPath;
if ($.os.indexOf("Windows") !== -1) {
    // Windows系统提取盘符
    rootPath = docPathStr.substring(0, 2);
} else {
    // Mac系统提取根目录卷名
    rootPath = docPathStr.split("/")[1];
}
alert("根路径:" + rootPath);

内容的提问来源于stack exchange,提问作者Steve G.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 21:22:38