DieterHolvoet
3/27/2016 - 4:49 PM

A series of File prototype functions for Adobe ExtendScript

A series of File prototype functions for Adobe ExtendScript

/* Get file extension */
File.prototype.getFileExtension = function() {
    var str = this.fsName;
    return str.substring(str.lastIndexOf(".") + 1);
};
/* Get full path of the folder containing the file */
File.prototype.getFolderPath = function() {
    var str = this.fsName;
    return str.substring(0, str.lastIndexOf("\\"));
};
/* Get full path of file */
File.prototype.getFullPath = function() {
    return this.fsName;
};
/* Get file name */
File.prototype.getFileName = function() {
    var str = this.fsName;
    return str.substring(str.lastIndexOf("\\") + 1, str.lastIndexOf("."));
};
/* Return the corresponding Document object of a given File object, and open the file if necessary. */
File.prototype.asDocument = function() {
    var document = false;
    
    for(var i = 0; i < app.documents.length; i++) {
        if(app.documents[i].getFullPath() === this.getFullPath()) {
            $.writeln("Found at index " + i + " => " + this.getFullPath());
            document = app.documents[i];
            break;
        }
    }
    
    if(!document) {
        app.open(this);
        document = app.documents[0];
    }
    
    return document;
};