在 Photoshop 2020 中,我正在编写脚本并尝试将活动文档的驱动器号与 app.activeDocument.path 隔离。
我的问题是,我该怎么做?我收到一些奇怪的错误消息,Google 搜索无助于解决问题。我尝试使用以下功能:
这是我的代码:
var teststring = app.activeDocument.path;
var t = teststring.toLowerCase();
但是对于我尝试的每一个功能,Photoshop 都会给我这个错误信息:
Error 24: teststring.[function name] is not a function.
我什至尝试过一些没有意义的函数,例如 toLowerCase() 和 toUpperCase(),只是想看看我是否可以在 Photoshop 中使用 any 字符串函数。
Photoshop 告诉我,我尝试过的所有字符串函数都不存在。是否有一组用于 Photoshop 脚本的特定字符串函数。我没有在任何地方找到它。我可以使用什么字符串函数从 activeDocument.path 获取驱动器号?或者 Photoshop 没有任何可用的字符串函数?
谢谢大家
那是因为Photoshop的路径是一个对象
var teststring = app.activeDocument.path;
alert(typeof(teststring)); // Object
// Turn the path into a string with either
var t = "" + teststring.toLowerCase();
var t = teststring.toString().toLowerCase();
alert(t); // /c/myfolder/myfile.psd
您应该能够使用正则表达式从那里获取驱动器号:
// Replace all front slashes with back slashes (I use Windows)
t = t.replace(/\//g, "\\");
// Check if first two characters are a backslash and a non-backslash character
if (t.charAt(0) === "\\" && t.charAt(1) !== "\\")
{
t = t.replace(/\\[a-zA-Z]\\/, t.charAt(1).toUpperCase() + ":\\");
}
var driveLetter = t[0];
alert(driveLetter); //c