简体   繁体   中英

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. They loop through an InDesign document and create several PDFs based on it. Previously, they would be stored in a folder called "~/PDFExports". However, this doesn't work with 10.10.

If I change the code to just place the PDFs in "~/", it works fine. 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. I've seen things about making calls to ObjC, or to call Application('Finder'), but neither work - they both return undefined.

Am I just missing something basic here, or is it really this hard to simply move a file with JXA?

EDIT: Some syntax for how I'm creating the folder in question and how I'm attempting to work with 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. It has nothing to do with JXA, and is not compatible with it.

InDesign's JSX documentation:

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.)

Here's the link to Adobe's InDesign Scripting forum, which is the best place to get help with JSX:

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

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
and both sourceFile and destinationLocation must be NSString objects like exportFolder

I will share some of what I learned about JXA move and duplicate methods. 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. Use the System Events move method instead. The duplicate method in Finder works just fine. The duplicate method does not work well in system events. This is a modified snippet from a script I wrote showing move() using System Events.

(() => {
    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});
})();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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