2009-09-22

File Name Without a File Extension

The following is a method for the File object in ExtendScript to allow your ExtendScript (Adobe JavaScript) to get the name of a file without the original file extension. I use this code to create .log files and .xml files with the same name as an existing file in Adobe InDesign, but it is useable for many other things such as getting the base name of an Adobe Photoshop file.


File.prototype.nameWithoutExtension = function() {
//-------------------------------------------------------------------------
//-- N A M E W I T H O U T E X T E N S I O N
//-------------------------------------------------------------------------
//-- Generic: Yes for ExtendScript. Not JavaScript.
//-------------------------------------------------------------------------
//-- Purpose: to return the name of the file without the file extension.
//-------------------------------------------------------------------------
//-- Arguments: None. It is a method.
//-------------------------------------------------------------------------
//-- Calls: Nothing.
//-------------------------------------------------------------------------
//-- Returns: The name without the extenstion or the original name if no
//-- file extension is used.
//-------------------------------------------------------------------------
//-- Sample Use:
//~ var fileRef = File.saveDialog ('Pick an Adobe InDesign File', '*.indd' ) ;
//~ var baseName = fileRef.nameWithoutExtension() ;
//-------------------------------------------------------------------------
//-- Written: 2009.09.21 by Jon S. Winters of electronic publishing support
//-- eps@electronicpublishingsupport.com
//-------------------------------------------------------------------------
//-- Get the original name ;
var fullName = this.name
//-- Locate the final position of the final . before the extension.
var finalDotPosition = fullName.lastIndexOf( "." ) ;
//-- check that position. If it isn't -1 (missing) return the text
//-- up to that position.
//-- Note, there are some odd things that can happen when passing
//-- a file reference that begins with a single dot. Be wary.
if ( finalDotPosition > -1 ) {
return fullName.substr( 0 , finalDotPosition );
}
//-- implied else, return the original name because there is no dot.
return fullName ;
}

1 comment: