LoginSignup
1
2

More than 5 years have passed since last update.

InDesignで、拡張子のないリンクファイルをチェック

Last updated at Posted at 2014-03-15

過去データから引き継いできたデータで、未だに拡張子のないリンクファイルがあるのでチェックするスクリプトを書いた。
InDesign CS5、Mac OS X 10.8.5にて確認。

(function(){
    if(!app.documents.length) return;
    checkExtention(app.activeDocument);
}());

function checkExtention(doc){
    var grapics = doc.allGraphics;
    var checkRegs = {
        'adobe pdf':(/\.(ai|pdf)$/i)
       ,'tiff':(/\.tiff?$/i)
       ,'eps':(/\.eps$/i)
       ,'photoshop':(/\.psd$/i)
    };
    var i = grapics.length;
    var checkList = [];
    while(i--){
        var grapic = grapics[i];
        var link = grapic.itemLink;
        if(link && link.isValid){
            var graphicType = grapic.imageTypeName.toLowerCase();
            var checkReg = checkRegs[graphicType] || new RegExp('\\.'+graphicType+'$','i');
            if(!checkReg.test(link.name)){
                checkList.push('ファイル名:'+link.name+', タイプ:'+graphicType);
            }
        }
    };
    if(checkList.length){
        alert('拡張子のないファイルが' + checkList.length + '個あります\n'+checkList.join('\n'));
    }else{
        alert('OK');
    }
}

ここまでできたら、自動で拡張子をつけてファイルをリンクし直して欲しいと思うけど、とりあえず今はここまで。

2014/05/07 追記
リンクを変更するところまで書いた。
InDesign CS4以降、Macのみで動作。

2014/08/22
リンクが置き換わらない部分を修正。

(function(){
    if(!app.documents.length) return;
    var doc = app.activeDocument;
    var checkMap = checkExtention(app.activeDocument);
    reLinkWithExtention(doc,checkMap);
    alert('OK');
}());

/**ファイルを拡張子付きでリネームして再リンク*/
function reLinkWithExtention(doc,checkMap){
    var exts = {
        'adobe pdf':'.ai'
       ,'tiff':'.tif'
       ,'eps':'.eps'
       ,'photoshop':'.psd'
    };
    for(var i in checkMap){
        var graphics = checkMap[i];
        var ext = exts[graphics[0].imageTypeName.toLowerCase()];
        var linkPath = getPosixPath(i);
        var count = 0;
        for(;;){
            var suffix = count? '_'+count: '';
            var renamePath = linkPath+suffix+ext;
            var renameFile = new File(linkPath+suffix+ext);
            if(!renameFile.exists) break;
            count++;
        }
        var linkFile = new File(linkPath);
        if(linkFile.exists){
            var linkName = linkPath.match(/[^\/]+$/)[0];
            var relinkName = linkName + suffix + ext;
            $.writeln(linkName +' -> '+relinkName);
            linkFile.rename(relinkName);
            for(var g=0,glen=graphics.length;g<glen;g++){
                graphics[g].itemLink.relink(linkFile);
            }
        }
    }
};

/**拡張子のないリンクを集める*/
function checkExtention(doc){
    var grapics = doc.allGraphics;
    var checkRegs = {
        'adobe pdf':(/\.(ai|pdf)$/i)
       ,'tiff':(/\.tiff?$/i)
       ,'eps':(/\.eps$/i)
       ,'photoshop':(/\.psd$/i)
    };
    var i = grapics.length;
    var checkMap = {};
    while(i--){
        var grapic = grapics[i];
        var link = grapic.itemLink;
        if(link && link.isValid){
            var graphicType = grapic.imageTypeName.toLowerCase();
            var checkReg = checkRegs[graphicType] || new RegExp('\\.'+graphicType+'$','i');
            var linkName = link.name;
            if(!checkReg.test(linkName)){
                var linkPath = link.filePath;
                if(!checkMap[linkPath]){
                    checkMap[linkPath] = [];
                }
                checkMap[linkPath].push(grapic);
            }
        }
    };
    return checkMap;
};

/**ローカルパスからPosixパスを得る*/
function getPosixPath(localPath) {
    if (localPath && Folder.fs == "Macintosh") {
        localPath = escapeAppleScript(localPath);
        var posixPath = app.doScript('"' + localPath + '"\'s POSIX path', ScriptLanguage.APPLESCRIPT_LANGUAGE);
        return posixPath.replace(/<([0-9a-fA-F]{4})>/g, function(){
            return String.fromCharCode(parseInt("0x"+arguments[1]));
        });
    }
    return false;
};

/**AppleScript用にエスケープ*/
function escapeAppleScript(str) {
    return str.replace(/(?=["\\])/g, '\\');
};
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2