簡體   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