Macos 如何获取当前脚本的POSIX路径';JavaScript中的文件夹是否用于自动化?

Macos 如何获取当前脚本的POSIX路径';JavaScript中的文件夹是否用于自动化?,macos,applescript,osx-yosemite,osascript,javascript-automation,Macos,Applescript,Osx Yosemite,Osascript,Javascript Automation,在AppleScript中,可以使用以下行获取当前脚本所在文件夹的POSIX路径: POSIX path of ((path to me as text) & "::") 示例结果:/Users/亚伦/Git/test/ JavaScript的等价物是什么?这里有一种方法[注意:我不再推荐这种方法。请参阅下面的编辑]: 请注意,在doshell脚本中,围绕path的单引号用于处理带空格的路径等 编辑 在@foo因为使用了一种相当不安全的路径引用方法而责骂我之后,我想用以下内容来修正这个

在AppleScript中,可以使用以下行获取当前脚本所在文件夹的POSIX路径:

POSIX path of ((path to me as text) & "::")
示例结果:
/Users/亚伦/Git/test/

JavaScript的等价物是什么?

这里有一种方法[注意:我不再推荐这种方法。请参阅下面的编辑]:


请注意,在doshell脚本中,围绕
path
的单引号用于处理带空格的路径等

编辑 在@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)。

使用
-[NSString stringByDeletingLastPathComponent]
,它已经知道如何安全地删除最后一个路径段:

ObjC.import('Foundation')

path = ObjC.unwrap($(path).stringByDeletingLastPathComponent)
或者,如果您喜欢更危险的生活,可以使用正则表达式从POSIX路径字符串中删除最后一个路径段。在我的脑海中(注意,买主等):


(请注意,第二个
replace()
是使用处理路径所必需的,因此为了总结我现在所做的工作,我在回答我自己的问题。使用@foo和@CRGreen的出色回答,我得出了以下结论:

ObjC.import('Foundation');
var-app,path,dir;
app=Application.currentApplication();
app.includeStandardAdditions=真;
path=app.pathTo(此);
dir=$.NSString.alloc.initWithUTF8String(路径).stringByDeletingLastPathComponent.js+'/';

这与@CRGreen的响应非常接近,但是,它有点简洁,我正在导入
Foundation
而不是
Cocoa
。另外,我正在声明我使用的变量以避免意外全局变量。

我想我找到了一种更简单的方法来获取父文件夹,而无需调用ObjC

var-app=Application.currentApplication();
app.includeStandardAdditions=真;
thePath=app.pathTo(this);
路径(路径+'/../../')

不涉及ObjC的纯JXA代码:

App = Application.currentApplication()
App.includeStandardAdditions = true
SystemEvents = Application('System Events')

var pathToMe = App.pathTo(this)
var containerPOSIXPath = SystemEvents.files[pathToMe.toString()].container().posixPath()

通过提供自包含的实用功能来补充现有的有用答案:

//返回承载此脚本/应用程序的文件夹的POSIX路径。
//例如,从“/foo/bar.scpt”中返回“/foo”。
函数myPath(){
var app=Application.currentApplication();app.includeStandardAdditions=true
返回$(app.pathTo(this.toString()).stringByDeletingLastPathComponent.js
}
//返回此脚本/应用程序的根文件名(文件名不带扩展名)。
//例如,从“/foo/bar.scpt”中返回“bar”。
//(如果要保留扩展名,请删除“.stringByDeletingPathExtension”。)
函数myName(){
var app=Application.currentApplication();app.includeStandardAdditions=true
返回$(app.pathTo(this.toString()).lastPathComponent.stringByDeletingPathExtension.js
}
注意:这些函数使用ObjC桥的快捷语法形式(
$(…)
用于
ObjC.wrap()
.js
用于
ObjC.unwrap()
,并且还利用了
基础
框架的符号默认可用的事实-请参阅


很容易对这些函数进行泛化,以提供POSIX
dirname
basename
实用程序的等价物:

//返回指定文件系统路径的父路径。
//输入路径中的尾随“/”将被忽略。
//与POSIX dirname实用程序等效。
//示例:
//目录名('/foo/bar')/->'/foo'
//dirname('/foo/bar/')//同上
函数名称(路径){
返回$(path.toString()).stringByDeletingLastPathComponent.js
}
//返回指定文件系统路径的文件名组件。
//输入路径中的尾随“/”将被忽略。
//如果指定了可选选项:
//-如果它是一个字符串,如果它在
//结尾(区分大小写)-一定要包括“.”
//-否则(布尔值或数字),任何真值都会导致任何扩展
//(后缀)现在要删除。
//与POSIX basename实用程序等效;的真实语义
//第二个参数是一个扩展。
//示例:
//basename('/foo/bar')//>“bar”
//basename('/foo/bar/')//同上
//basename('/foo/bar.scpt',1)/->“bar”
//basename('/foo/bar.scpt','.scpt')/->“bar”
//basename('/foo/bar.jxa','.scpt')/->'bar.jxa'
函数basename(路径,extToStrip){
path=path.toString()
if(path[path.length-1]='/'){path=path.slice(0,-1)}
如果(extToStrip的类型==='string'){
返回path.slice(-extToStrip.length)==extToStrip?$(路径).lastPathComponent.js.slice(0,-extToStrip.length):$(路径).lastPathComponent.js
}else{//假定为数字:如果为truthy,则去掉任何扩展名
返回extToStrip?$(路径).lastPathComponent.stringByDeletingPathExtension.js:$(路径).lastPathComponent.js
}
}

上面有很多有趣的解决方案

这是我的,它不需要ObjC,并返回一个具有可能需要的属性的对象

'use strict';
var oScript = getScriptProp(this);

/*oScript Properties
    Path
    Name
    ParentPath
    Folder
*/

oScript.ParentPath;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function getScriptProp(pRefObject) {

  var app = Application.currentApplication()
  app.includeStandardAdditions = true

  var pathScript = app.pathTo(pRefObject).toString();
  var pathArr = pathScript.split("/")

  var oScript = {
    Path: pathScript,
    Name: pathArr[pathArr.length - 1],
    ParentPath: pathArr.slice(0, pathArr.length - 1).join("/"),
    Folder: pathArr[pathArr.length - 2]
  };

  return oScript
}

很好!我没有意识到
这个
“注意路径周围的单引号,以便使用带空格的路径”…如果路径字符串包含单引号,则会完全崩溃。在未完全清理输入之前,切勿生成代码:这是完全不安全的。在使用
do shell script
时,请始终使用
函数quotedForm{return'+s.replace(“,“\\”)+“}”
在连接任意文本之前正确引用任意文本,例如
app.doShellScript('dirname'+q
App = Application.currentApplication()
App.includeStandardAdditions = true
SystemEvents = Application('System Events')

var pathToMe = App.pathTo(this)
var containerPOSIXPath = SystemEvents.files[pathToMe.toString()].container().posixPath()
'use strict';
var oScript = getScriptProp(this);

/*oScript Properties
    Path
    Name
    ParentPath
    Folder
*/

oScript.ParentPath;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function getScriptProp(pRefObject) {

  var app = Application.currentApplication()
  app.includeStandardAdditions = true

  var pathScript = app.pathTo(pRefObject).toString();
  var pathArr = pathScript.split("/")

  var oScript = {
    Path: pathScript,
    Name: pathArr[pathArr.length - 1],
    ParentPath: pathArr.slice(0, pathArr.length - 1).join("/"),
    Folder: pathArr[pathArr.length - 2]
  };

  return oScript
}