指定したフォルダにあるファイルをぜんぶPNGで出力 - イラストレーター 2013/04/20

イラストレーターです。使ったのはOSX版のCS5でやってます。

所定のディレクトリにごそっとxxx.aiファイルがあり、それをpngファイルして出力したいというわけです。

aiファイルの出力条件として、 アートボードの中におさまっているものとして考えているので、アートボードでクリップされていればOKで、あと画像サイズも縦横おなじの真四角を想定していて、サイズも固定としています。

これで少しは手作業がへるかしら。自動化バンザイ...

参考にしたサイトは、


で コードは以下


// Main Code [Execution of script begins here]

// この一行が重要
// uncomment to suppress Illustrator warning dialogs
app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

// 入力対象のディレクトリを指定
folderIn = new Folder ("/path/path");
fileListIn = folderIn.getFiles("*.ai");

// 出力ディレクトリを指定
destFolder = new Folder ("/path/path");

options = this.getOptions();

// ここをtrycatchで囲む必要があるかわかってないけど...
try{
     for (i=0; i<fileListIn.length; i++) {
        //open  ai
        fileObj = new File(fileListIn[i].fsName);
        open(fileObj);

        //save settings
        saveFile = new File(fileListIn[i]);
        // Get the file to save the document as PNG into
        targetFile = this.getTargetFile(activeDocument.name, '.PNG', destFolder);

        // Save as PNG
        activeDocument.exportFile(targetFile, ExportType.PNG24, options);

        //close [static] [read-only] Do not save changes.
        activeDocument.close(SaveOptions.DONOTSAVECHANGES);
    }
} catch(e) {
    alert( e.message, "スクリプト警告", true);
}

/** Returns the options to be used for the generated files.
    @return ExportOptionsPNG object
*/
function getOptions()
{
    // http://cssdk.host.adobe.com/sdk/1.0/docs/WebHelp/references/csawlib/com/adobe/illustrator/ExportOptionsPNG24.html
    // Create the required options object
    var options = new ExportOptionsPNG24();
    options.antiAliasing = true;//アンチエイリアス処理
    options.artBoardClipping = true;// アートボードでクリップ

    if(false) { // とりあえず
         // スケール
        scale = (120/480)*100;
        options.horizontalScale = scale;
        options.verticalScale = scale;
    }
  
    return options;
}

/** Returns the file to save or export the document into.
    @param docName the name of the document
    @param ext the extension the file extension to be applied
    @param destFolder the output folder
    @return File object
*/
function getTargetFile(docName, ext, destFolder) {
    var newName = "";

    // if name has no dot (and hence no extension),
    // just append the extension
    if (docName.indexOf('.') < 0) {
        newName = docName + ext;
    } else {
        var dot = docName.lastIndexOf('.');
        newName += docName.substring(0, dot);
        newName += ext;
    }
  
    // Create the file object to save to
    var myFile = new File( destFolder + '/' + newName );
  
    // Preflight access rights
    if (myFile.open("w")) {
        myFile.close();
    }
    else {
        throw new Error('アクセスが拒否されました');
    }
    return myFile;
}

: