繁体   English   中英

使用 JXA 移动创建的文件

[英]Moving created files with JXA

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

如果我更改代码以将 PDF 放在“~/”中,它就可以正常工作。 从那里,我想将文件移动到“~/PDFExports”,但我似乎无法找到如何做到这一点的答案。 我已经看到有关调用 ObjC 或调用 Application('Finder') 的内容,但都不起作用 - 它们都返回未定义。

我只是遗漏了一些基本的东西,还是仅仅使用 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用于Adobe应用程序自动化,因为它具有很多缺少/损坏的功能以及应用程序兼容性问题,并且确实不适合生产工作。)

这是Adobe 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

我将分享一些我学到的有关 JXA 移动和复制方法的知识。 我不是专业程序员,只是对自动化充满热情的律师。 我的评论来自多次试验和错误,阅读我可以在网上找到的任何内容,以及很多斗争。 move 方法不适用于 Finder。 请改用系统事件移动方法。 Finder 中的重复方法工作得很好。 duplicate 方法在系统事件中效果不佳。 这是我编写的显示使用系统事件的 move() 的脚本的修改片段。

(() => {
    const strPathTargetFile = '/Users/bretfarve/Documents/MyFolderA/myFile.txt';
    const strPathFolder = '/Users/bretfarve/Documents/MyFolderB/';
    
    /* System Events Objects */ 
    const SysEvents = Application('System Events');
    const objPathFolder = SysEvents.aliases[strPathFolder];
        
    SysEvents.move(SysEvents.aliases.byName(strPathTargetFile), {to: objPathFolder});
})();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM