问题描述
在AppleScript的有可能获得当前脚本位于使用该行的文件夹的路径POSIX:
In AppleScript it’s possible to get the the POSIX path of the folder the current script is located in using this line:
POSIX path of ((path to me as text) & "::")
结果举例: /用户/阿龙/混帐/测试/
什么是JavaScript的相同呢?
What’s the JavaScript equivalent?
推荐答案
下面是一个方法的 [注:我不再建议使用此方法。请参阅编辑,下文] 的:
app = Application.currentApplication();
app.includeStandardAdditions = true;
path = app.pathTo(this);
app.doShellScript('dirname \'' + path + '\'') + '/';
请注意单引号周围的路径
与空间,路径等工作,在doShellScript
note the single quotes surrounding path
to work with paths with spaces, etc., in the doShellScript
修改
通过@foo使用一个相当不安全的路径的引用方法在手上拍打而之后,我想修改这个答案有:
EDITAfter being slapped on the hand by @foo for using a fairly unsafe path-quoting method, I'd like to amend this answer with:
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)的字符串。
If you're going to use this string, of course, you still have to deal with whether or not it has questionable characters in it. But at least at this stage this is not an issue. This also demonstrates a few concepts available to the JXA user, like using the ObjC bridge and .js
to get the string "coerced" to a JavaScript string (from NSString).
这篇关于如何获得当前脚本的文件夹的路径POSIX在JavaScript中的自动化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!