javascript - 通过脚本将特定文件格式从文件夹导入 Illustrator

标签 javascript import adobe-illustrator extendscript

我在网上找到了这个脚本,这几乎就是我正在寻找的,但它需要修改,而且我似乎无法让它工作。

    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!

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

最佳答案

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

var imageList = selectedFolder.getFiles();

接受mask参数 - 它本质上是一个正则表达式模式。

如果将该行更改为;

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

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

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

关于javascript - 通过脚本将特定文件格式从文件夹导入 Illustrator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58527699/

相关文章:

javascript - 使用 php 从 ajax 检索发布的数据

带参数的 JavaFX JavaScript 上行调用

javascript - 在Reactjs中根据条件导入组件(Conditional based import)

python - ImportError:无法导入名称 ModuleA

javascript - 旋转椭圆形 SVG 对象

javascript - 即使在清理后,Angular 2 iFrame 也会出现错误

javascript - 使用 jquery 操作 css "content: attr(data-tooltip)"

import - 如何在 Inno Setup 脚本中有条件地从 DLL 导入函数?

adobe - Illustrator 和 Javascript - 将 pathItem 移动到新层

javascript - Adobe Illustrator - 自动将 TrueType 字体更改为 OpenType 版本