这是 Adobe ExtendScript 的 javascript。基本上我想在脚本中有一个持久变量来存储用户的偏好,就像你可以使用 AppleScript 一样property
。我能想到的唯一方法是让脚本用File.write()
.
var MY_PROPERTY = true;
function reassignProperty(propName, propValue) {
var thisFile = new File($.fileName);
if (thisFile.open("r")) {
var myScript = thisFile.read();
thisFile.close();
// Look for the property declaration and overwrite with new value
var searchStr = new RegExp("(var " + propName + " = )" + ".+");
var newScript = myScript.replace(searchStr, "$1" + propValue + ";");
thisFile.open("w");
thisFile.write(newScript);
thisFile.close();
}
}
reassignProperty("MY_PROPERTY", "false");
据我所知,这行得通。但它安全吗?我的直觉告诉我应该有一种更简洁的方式来拥有持久变量,但如果没有,我只想知道自覆盖脚本是否存在任何潜在问题。