【问题标题】:import specific file formats from folder into Illustrator via script通过脚本将特定文件格式从文件夹导入 Illustrator
【发布时间】:2020-02-19 23:41:13
【问题描述】:

我在网上找到了这个脚本,这几乎就是我想要的,但它需要调整,我似乎无法让它工作。

    if (selectedFolder) {
        myDocument = app.documents.add();

        var firstImageLayer = true;
        var newLayer ;
        var thisPlacedItem;

        // create document list from files in selected folder
        var imageList = selectedFolder.getFiles();

        for (var i = 0; i < imageList.length; i++) {
            // open each document in file list
            if (imageList[i] instanceof File) {
                // get the file name
                var fName = imageList[i].name.toLowerCase();
                // check for supported file formats
                //if( (fName.indexOf(".eps") == -1) ) {
                if( (fName.indexOf(".tga") == -1) && (fName.indexOf(".png") == -1)) {
                    // skip unsupported formats
                    continue;
                } else {
                    if( firstImageLayer ) {
                        newLayer = myDocument.layers[0];
                        firstImageLayer = false;
                    } else {
                        newLayer = myDocument.layers.add();
                    }
                   // Give the layer the name of the image file
                   newLayer.name = fName.substring(0, fName.indexOf(".") );

                   // Place the image on the artboard
                   thisPlacedItem = newLayer.placedItems.add()
                   thisPlacedItem.file = imageList[i];

                   switch( placement9pointAlignment ) {
                        default :
                            break;
                        case "ul" : 
                            thisPlacedItem.top = myDocument.height;
                            thisPlacedItem.left = 0;
                            break;
                        case "ml" : 
                            thisPlacedItem.top = myDocument.height / 2 + thisPlacedItem.height / 2;
                            thisPlacedItem.left = 0;
                            break;
                        case "ll" : 
                            thisPlacedItem.top = thisPlacedItem.height;
                            thisPlacedItem.left = 0;
                            break;
                        case "ur" : 
                            thisPlacedItem.top = myDocument.height;
                            thisPlacedItem.left = myDocument.width - thisPlacedItem.width;
                            break;
                        case "mr" : 
                            thisPlacedItem.top = myDocument.height / 2 + thisPlacedItem.height / 2;
                            thisPlacedItem.left = myDocument.width - thisPlacedItem.width;
                            break;
                        case "lr" : 
                            thisPlacedItem.top = thisPlacedItem.height;
                            thisPlacedItem.left = myDocument.width - thisPlacedItem.width;
                            break;
                        case "um" : 
                            thisPlacedItem.top = myDocument.height;
                            thisPlacedItem.left = myDocument.width / 2 - thisPlacedItem.width / 2;
                            break;
                        case "mm" : 
                            thisPlacedItem.top = myDocument.height / 2 + thisPlacedItem.height / 2;
                            thisPlacedItem.left = myDocument.width / 2 - thisPlacedItem.width / 2;
                            break;
                        case "lm" : 
                            thisPlacedItem.top = thisPlacedItem.height;
                            thisPlacedItem.left = myDocument.width / 2 - thisPlacedItem.width / 2;
                            break;
                   }
                }
            }
        }

        if( firstImageLayer ) {
            // alert("The action has been cancelled.");
            // display error message if no supported documents were found in the designated folder
            alert("Sorry, but the designated folder does not contain any recognized image formats.\n\nPlease choose another folder.");
            myDocument.close();
            importFolderAsLayers(getFolder());
        }

    } else {
        // alert("The action has been cancelled.");
        // display error message if no supported documents were found in the designated folder
        alert("Rerun the script and choose a folder with images.");
        //importFolderAsLayers(getFolder());
    }
}

//Start the script off
importFolderAsLayers( getFolder() );

我希望能够选择一个文件夹并让它只导入 .tga 或 .png 文件。如果存在其他文件格式,我希望它忽略它们。

这个脚本的问题在于它是通过文件名搜索,而不是通过扩展名。 通常这可以正常工作,但我经常收到 .tga 文件以及名为 image_01.tga.jpeg 的 jpeg 副本

这是一个问题,因为现在当我使用脚本时,它会导入 tga 和 jpeg!

有谁知道我可以如何调整这个脚本,以便通过扩展来专门搜索?

【问题讨论】:

    标签: javascript import adobe-illustrator extendscript


    【解决方案1】:

    您在读取的行中使用的getFiles() 方法;

    var imageList = selectedFolder.getFiles();
    

    接受 mask 参数 - 它本质上是一个 RegExp 模式。

    如果您将该行更改为;

    var imageList = selectedFolder.getFiles(/\.(tga|png)$/i);
    

    它将确保分配给imageList 变量的文件列表仅是以.tga.TGA.png.PNG 结尾的文件。

    那么您不必在for 循环中对文件类型进行任何过滤。

    【讨论】:

      猜你喜欢
      • 2016-12-14
      • 1970-01-01
      • 2019-04-22
      • 1970-01-01
      • 1970-01-01
      • 2018-12-27
      • 2016-08-22
      • 1970-01-01
      • 2022-12-04
      相关资源
      最近更新 更多