如何在Photoshop 2020脚本中获取活动文件的驱动器盘符?
Photoshop脚本提取活动文档驱动器盘符的解决方案
问题根源
app.activeDocument.path返回的是Adobe脚本模型中的Folder对象,并非JavaScript原生字符串类型,所以直接调用toLowerCase()、substring()等JS内置字符串方法会触发"xxx is not a function"错误。
解决步骤
- 将Folder对象转为原生路径字符串:使用Folder对象的
fsName属性,它会返回系统原生格式的路径字符串(Windows下如C:\Projects\Demo.psd,Mac下如/Users/xxx/Demo.psd)。 - 提取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.
相关产品推荐
相关产品推荐

