在AppleScript中,可以使用此行获取当前脚本所在的文件夹的POSIX路径:
POSIX path of ((path to me as text) & "::")
结果示例: /Users/aaron/Git/test/
什么是JavaScript等效项?
这是一种方法[注意:我不再推荐这种方法。见编辑,如下:
app = Application.currentApplication();
app.includeStandardAdditions = true;
path = app.pathTo(this);
app.doShellScript('dirname \'' + path + '\'') + '/';
请注意path
在doShellScript中使用单引号引起来的空格等路径
编辑由于使用了不安全的路径引用方法而被@foo打了手之后,我想用以下方法修改此答案:
ObjC.import("Cocoa");
app = Application.currentApplication();
app.includeStandardAdditions = true;
thePath = app.pathTo(this);
thePathStr = $.NSString.alloc.init;
thePathStr = $.NSString.alloc.initWithUTF8String(thePath);
thePathStrDir = (thePathStr.stringByDeletingLastPathComponent);
thePathStrDir.js + "/";
当然,如果要使用此字符串,则仍然必须处理它是否包含可疑字符。但是至少在现阶段这不是问题。这还演示了JXA用户可用的一些概念,例如使用ObjC桥以及.js
将字符串“强制”转换为JavaScript字符串(来自NSString)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句