使用 JXA 移动创建的文件

Moving created files with JXA

我是 JXA 脚本的新手,但我正在尝试对目前工作中使用的一些旧脚本进行故障排除。他们遍历 InDesign 文档并基于它创建多个 PDF。以前,它们将存储在名为“~/PDFExports”的文件夹中。但是,这不适用于 10.10。

如果我将代码更改为仅将 PDF 放在“~/”中,则效果很好。从那里,我想将文件移动到“~/PDFExports”,但我似乎无法找到如何做到这一点的答案。我看过有关调用 ObjC 或调用 Application('Finder') 的内容,但都不起作用 - 它们都 return 未定义。

我是不是遗漏了一些基本的东西,或者用 JXA 移动文件真的这么难吗?

编辑:关于我如何创建有问题的文件夹以及我如何尝试使用 Finder 的一些语法。

//This is called in the Main function of the script, on first run.

var exportFolder = new Folder(exportPath);
if(!exportFolder.exists) {
    exportFolder.create();
}

//This is called right after the PDF is created. file is a reference to the 
actual PDF file, and destination is a file path string.

function MoveFile(file,destination){
   var Finder = Application("Finder");

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

   alert("File moved");
}

会不会是文件夹不见了?可能是您对文件夹对象的引用无效?要显示的任何语法?

Adobe 应用程序长期以来一直包含自己的嵌入式 JS 解释器、JS API 和 .jsx 文件扩展名。与JXA无关,不兼容

InDesign 的 JSX 文档:

http://www.adobe.com/devnet/indesign/documentation.html#idscripting

(顺便说一句,我也强烈建议不要将 JXA 用于 Adob​​e 应用程序自动化,因为它有很多 missing/broken 功能和应用程序兼容性问题,并且确实不适合生产工作。)

这里是 link Adob​​e 的 InDesign 脚本论坛,这是获得 JSX 帮助的最佳场所:

https://forums.adobe.com/community/indesign/indesign_scripting

您可以使用 Cocoa 创建文件夹

var exportFolder = $.NSHomeDirectory().stringByAppendingPathComponent("PDFExports")
var fileManager = $.NSFileManager.defaultManager
var folderExists = fileManager.fileExistsAtPath(exportFolder)
if (!folderExists) {
    fileManager.createDirectoryAtPathWithIntermediateDirectoriesAttributesError(exportFolder, false, $(), $())
}

并移动文件

var success = fileManager.moveItemAtPathToPathError(sourceFile, destinationLocation, $());
if (success) alert("File moved");

考虑destinationLocation必须是包含文件名的完整路径
sourceFiledestinationLocation 都必须是 NSString 对象,例如 exportFolder