通过脚本将文件夹中的特定文件格式导入 Illustrator
import specific file formats from folder into Illustrator via script
我在网上找到了这个脚本,这几乎是我要找的,但它需要修改,我似乎无法让它工作。
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() );
我希望能够 select 一个文件夹并让它只导入 .tga 或 .png 文件。如果存在其他文件格式,我希望它忽略它们。
此脚本的问题在于它是通过文件名而不是扩展名搜索的。
通常这会工作正常,但我经常收到 .tga 文件以及名为 image_01.tga.jpeg
的 jpeg 副本
这是个问题,因为现在当我使用脚本时,它会导入 tga 和 jpeg!
有谁知道我如何调整此脚本以便它通过扩展名进行专门搜索?
您在以下行中使用的 getFiles()
方法;
var imageList = selectedFolder.getFiles();
接受一个 mask
参数 - 它本质上是一个 RegExp 模式。
如果将该行更改为;
var imageList = selectedFolder.getFiles(/\.(tga|png)$/i);
它将确保分配给 imageList
变量的文件列表只是以 .tga
、.TGA
、.png
或 [=18] 结尾的文件=].
那么您不必在 for
循环中对文件类型进行任何过滤。
我在网上找到了这个脚本,这几乎是我要找的,但它需要修改,我似乎无法让它工作。
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() );
我希望能够 select 一个文件夹并让它只导入 .tga 或 .png 文件。如果存在其他文件格式,我希望它忽略它们。
此脚本的问题在于它是通过文件名而不是扩展名搜索的。 通常这会工作正常,但我经常收到 .tga 文件以及名为 image_01.tga.jpeg
的 jpeg 副本这是个问题,因为现在当我使用脚本时,它会导入 tga 和 jpeg!
有谁知道我如何调整此脚本以便它通过扩展名进行专门搜索?
您在以下行中使用的 getFiles()
方法;
var imageList = selectedFolder.getFiles();
接受一个 mask
参数 - 它本质上是一个 RegExp 模式。
如果将该行更改为;
var imageList = selectedFolder.getFiles(/\.(tga|png)$/i);
它将确保分配给 imageList
变量的文件列表只是以 .tga
、.TGA
、.png
或 [=18] 结尾的文件=].
那么您不必在 for
循环中对文件类型进行任何过滤。