2

我正在尝试做一件非常简单的事情——使用随 OS X Yosemite 引入的自动化 JavaScript 移动(或复制)文件。

到目前为止,我有这样的事情。

finder = Application("Finder")

finder.move(Path("/Users/user/Source/file.pdf"), {
    to: Path("/Users/user/Destination/file.pdf"),
    replacing: true
})

结果不是很好。

Error -1728: Can't get object.

当然,我可以使用类似的东西,doShellScript("mv source destination")但 Finder + JAX 解决方案似乎更好。

4

3 回答 3

2

Findermove和操作与 JXA对象duplicate一起工作得很好。Path您的代码失败的原因是,当您提供文件路径时to,这些操作所期望的参数是文件路径。这将起作用:

finder = Application("Finder")

finder.move(Path("/Users/user/Source/file.pdf"), {
    to: Path("/Users/user/Destination/"),
    replacing: true
})
于 2016-12-11T12:47:53.660 回答
0

是的,JXA 和 Finder 搞得一团糟。我认为问题在于 Finder 喜欢 Aliasses 等,而不是 JavaScript 中的非类型变量。首先我想,问题是目标文件不存在,然后Path()-call 无法返回变量类型file。但即使您创建一个具有该名称的空目标文件,脚本也会失败(但会出现另一条错误消息......)

我发现的唯一方法是在 JXA 发行说明中使用 JXA-ObjC-Bridge 作为描述符:

ObjC.import('Cocoa')
error = $()
fMa = $.NSFileManager.defaultManager
fileMoved = fMa.moveItemAtPathToPathError('/Users/user/Source/file.pdf','/Users/user/Destination/file.pdf', error)
if (!fileMoved) {
    $.NSBeep();
    // or do something else depending on error.code
}

我认为这是一种比使用 shell 脚本更优雅的方式,但这只是一种感觉;-)

干杯,迈克尔/汉堡

于 2015-03-28T18:19:23.510 回答
-1

此脚本使用带有移动命令的 Finder 对象工作:

var Finder = Application("Finder")
var homeDirectory = Finder.startupDisk.folders["Users"].folders["user"]

var sourceFile = homeDirectory.folders["Source"].files["file.pdf"]
var destinationFolder = homeDirectory.folders["Destination"]

Finder.move(sourceFile, { to: destinationFolder })

它也适用于重复命令。

于 2015-06-29T19:51:00.350 回答