简体   繁体   English

使用 JXA 移动创建的文件

[英]Moving created files with JXA

I'm new to JXA scripting, but I'm attempting to troubleshoot some older scripts currently in place here at work.我是 JXA 脚本的新手,但我正在尝试对目前工作中使用的一些旧脚本进行故障排除。 They loop through an InDesign document and create several PDFs based on it.他们遍历 InDesign 文档并基于它创建多个 PDF。 Previously, they would be stored in a folder called "~/PDFExports".以前,它们将存储在名为“~/PDFExports”的文件夹中。 However, this doesn't work with 10.10.但是,这不适用于 10.10。

If I change the code to just place the PDFs in "~/", it works fine.如果我更改代码以将 PDF 放在“~/”中,它就可以正常工作。 From there, I'd like to move the files to "~/PDFExports", but I can't seem to find an answer on how to do that.从那里,我想将文件移动到“~/PDFExports”,但我似乎无法找到如何做到这一点的答案。 I've seen things about making calls to ObjC, or to call Application('Finder'), but neither work - they both return undefined.我已经看到有关调用 ObjC 或调用 Application('Finder') 的内容,但都不起作用 - 它们都返回未定义。

Am I just missing something basic here, or is it really this hard to simply move a file with JXA?我只是遗漏了一些基本的东西,还是仅仅使用 JXA 移动文件真的这么难?

EDIT: Some syntax for how I'm creating the folder in question and how I'm attempting to work with Finder.编辑:关于我如何创建有问题的文件夹以及我如何尝试使用 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 apps have long included their own embedded JS interpreter, JS API, and .jsx filename extension. 长期以来,Adobe应用程序都包含自己的嵌入式JS解释器,JS API和.jsx文件扩展名。 It has nothing to do with JXA, and is not compatible with it. 它与JXA无关,并且与它不兼容。

InDesign's JSX documentation: InDesign的JSX文档:

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

(BTW, I'd also strongly advise against using JXA for Adobe app automation as it has a lot of missing/broken features and application compatibility problems, and really isn't fit for production work.) (顺便说一句,我也强烈建议您不要将JXA用于Adobe应用程序自动化,因为它具有很多缺少/损坏的功能以及应用程序兼容性问题,并且确实不适合生产工作。)

Here's the link to Adobe's InDesign Scripting forum, which is the best place to get help with JSX: 这是Adobe InDesign脚本论坛的链接,这是获得JSX帮助的最佳位置:

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

Could it be that the folder is missing ? 可能是文件夹丢失了吗? Could be your reference to the folder object not valid ? 您对文件夹对象的引用可能无效吗? Any syntax to show ? 有显示的语法吗?

You could use Cocoa to create the folder 您可以使用Cocoa创建文件夹

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

and to move a file 并移动文件

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

Consider that destinationLocation must be the full path including the file name 考虑destinationLocation必须是完整路径,包括文件名
and both sourceFile and destinationLocation must be NSString objects like exportFolder 并且sourceFiledestinationLocation必须都是NSString对象,例如exportFolder

I will share some of what I learned about JXA move and duplicate methods.我将分享一些我学到的有关 JXA 移动和复制方法的知识。 I am not a professional programmer just an attorney that is passionate about automation.我不是专业程序员,只是对自动化充满热情的律师。 My comments come from much trial and error, reading whatever I could find online, and A LOT of struggle.我的评论来自多次试验和错误,阅读我可以在网上找到的任何内容,以及很多斗争。 The move method does not work well with Finder. move 方法不适用于 Finder。 Use the System Events move method instead.请改用系统事件移动方法。 The duplicate method in Finder works just fine. Finder 中的重复方法工作得很好。 The duplicate method does not work well in system events. duplicate 方法在系统事件中效果不佳。 This is a modified snippet from a script I wrote showing move() using System Events.这是我编写的显示使用系统事件的 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