Skip to content

Instantly share code, notes, and snippets.

@DieterHolvoet
Last active March 27, 2016 16:50
Show Gist options
  • Save DieterHolvoet/ea4e6a734ef0949a3e53 to your computer and use it in GitHub Desktop.
Save DieterHolvoet/ea4e6a734ef0949a3e53 to your computer and use it in GitHub Desktop.
A series of File prototype functions for Adobe ExtendScript
/* 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;
};
/* Get file extension */
File.prototype.getFileExtension = function() {
var str = this.fsName;
return str.substring(str.lastIndexOf(".") + 1);
};
/* Get file name */
File.prototype.getFileName = function() {
var str = this.fsName;
return str.substring(str.lastIndexOf("\\") + 1, str.lastIndexOf("."));
};
/* Get full path of file */
File.prototype.getFullPath = function() {
return this.fsName;
};
/* Get full path of the folder containing the file */
File.prototype.getFolderPath = function() {
var str = this.fsName;
return str.substring(0, str.lastIndexOf("\\"));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment