Site logo

Functions for File & Folder

Get folder. If doesn't exist, created it

function GetFolder(path) {
	var folder = new Folder(path);
	if (!folder.exists) {
		if (!folder.create(path)) folder = null;
	}
	return folder;
}

Get file name without extension

function GetFileName(fileName) {
	var str = "";
	var res = fileName.lastIndexOf(".");
	if (res == -1) {
		str = fileName;
	}
	else {
		str = fileName.substr(0, res);
	}
	return str;
}

Get extension without file name

function GetExtension(name) {
	var idx = name.lastIndexOf(".");
	var ext = "";
	if (idx > -1) {
		ext = name.substr((idx + 1));
	}
	return ext;
}

Remove the path and file extension to return the file name

function getFileName(value) {
	var path = value.toString();
	var lastIndex = path.lastIndexOf("/");
	var file = path.slice(lastIndex + 1);
	var lastIndexPeriod = file.lastIndexOf(".");
	return file.slice(0, lastIndexPeriod);
}

Write to file

function WriteToFile(text) {
	file = new File("~/Desktop/Log.txt");
	file.encoding = "UTF-8";
	if (file.exists) {
		file.open("e");
		file.seek(0, 2);
	}
	else {
		file.open("w");
	}
	file.write(text); 
	file.close();
}

    

Verify folder based on the Bob Stucky's library

function VerifyFolder(folder) {
	if (!folder.exists) {
		var folder = new Folder(folder.absoluteURI);
		var arr1 = new Array();
		while (!folder.exists) {
			arr1.push(folder);
			folder = new Folder(folder.path);
		}
		var arr2 = new Array();
		while (arr1.length > 0) {
			folder = arr1.pop();
			if (folder.create()) {
				arr2.push(folder);
			} else {
				while (arr2.length > 0) {
					arr2.pop.remove();
				}
				throw "Folder creation failed";
			} 
		}
	}
}
    

GetScriptsFolder by Harbs

function GetScriptsFolder() {
          var scriptsFolder = null;
          do{
          // On Mac this is a folder inside the app package
                    var appFolder = Folder.startup;
                    if (! appFolder.exists){break;}
                    scriptsFolder = Folder(appFolder + "/Scripts");
                    while (appFolder.exists && ! scriptsFolder.exists){
                              appFolder = appFolder.parent;
                              scriptsFolder = Folder(appFolder + "/Scripts");
                    }
                    if (! scriptsFolder.exists){
                              scriptsFolder = null;
                              break;
                    }
          }
          while (false);
          return scriptsFolder;
}

Get script path

function GetScriptPath() {
    try{
        return app.activeScript;
    }
    catch(err){
        return new File(err.fileName);
    }
}

function getScriptFolderPath

Get Filepath from current script
returns {Folder} script folder

function getScriptFolderPath() {
    var skriptPath;
    try {
        skriptPath = app.activeScript.parent;
    }
    catch (e) {
        /* We're running from the VSC */
        skriptPath = File(e.fileName).parent;
    }
    if (skriptPath.toString().match(/\/lib$/)) {
        skriptPath = skriptPath.parent;
    }
    return skriptPath;
}

Move file by Kasyan

Unlike AS or VB, JS has no move method for file object — a workaround is to first copy a file and then remove the original. But this approach has a shortcoming: files lose their original creation and modification dates, labels, etc. This function uses native AS or VB Move command/method depending on the platform the script is run, thus preserving this information. Click here to download

Set label to a file by Kasyan

Set label to a file on Mac by JavaScript.

How to copy a file to another folder without changing DateModified using Javascript in Windows

Main();

function Main() {
	if (File.fs == "Windows")  {
		var file = new File("/E/Temp_1/Test.txt");
		var destinationFolderPath = "/D/Temp_2/";
		var destinationFolder = new Folder(destinationFolderPath);
		if (!destinationFolder.exists) destinationFolder.create();
		
		var vbScript = 'Set fs = CreateObject("Scripting.FileSystemObject")\r';
		vbScript +=  'fs.CopyFile "' + file.fsName.replace("\\", "\\\\") + '", "' + destinationFolder.fsName.replace("\\", "\\\\") + "\\" + file.name + '"';

		app.doScript(vbScript, ScriptLanguage.visualBasic);
	}
}

Get startup disk — JavaScript function that gets the name of startup disk Both on Mac and Windows

How to find all indd-files inside subfolders

var files;
var folder = Folder.selectDialog("Select a folder with InDesign documents");
if (folder != null) {
	files = GetFiles(folder);
	if (files.length > 0) {
		alert("Found " + files.length + " InDesign documents");
	}
	else {
		alert("Found no InDesign documents");
	}
}

function GetFiles(theFolder) {
	var files = [],
	fileList = theFolder.getFiles(),
	i, file;
	
	for (i = 0; i < fileList.length; i++) {
		file = fileList[i];
		if (file instanceof Folder) {
			files = files.concat(GetFiles(file));
		}
		else if (file instanceof File && file.name.match(/\.indd$/i)) {
			files.push(file);
		}
	}

	return files;
}

Open all indd-files in the selected folder

app.open(Folder(Folder.selectDialog( "Select a folder with InDesign files")).getFiles(function(f) { return f instanceof File && !f.hidden && (f.name.match(/\.indd$/i) || f.type.match(/^IDd/)); } ));

Get subfolders

function GetSubfolders(theFolder) {
	var files = [],
	fileList = theFolder.getFiles(),
	i, file;
	
	for (i = 0; i < fileList.length; i++) {
		file = fileList[i];
		if (file instanceof Folder) {
			files.push(file);
		}
	}

	return files;
}

Write the contents of the folder

var myFolder = Folder.selectDialog ('Choose a Folder');
$.writeln('-------------------------------\r' + myFolder.displayName + '\r-------------------------------\r');
if(myFolder != null){
var myFiles = myFolder.getFiles('*.*');
for (i = 0; i < myFiles.length; i++){
var myFile = myFiles[i];
$.writeln(myFile.displayName);
}
}

FileFilter function for Mac

var xmlFile = File.openDialog("Choose an XML file", (File.fs == "Macintosh") ? FileFilter : "XML files:*.xml;All files:*.*");
	
function FileFilter(file) {
	var extention = ".xml";
	var lowerCaseName = file.name;
	lowerCaseName.toLowerCase();
	
	if (lowerCaseName.indexOf(extention) == file.name.length - extention.length) {
		return true;
	}
	else if (file instanceof Folder) {
		return true;
	}
	else {
		return false;
	}
}

function getFileFilter

Returns a File-Filter for a File-Dialog
@param {String} filter The File filter string in Windows Syntax: A filter expression such as "Javascript files:*.jsx;All files:*.*".
@return {String|Function} The Filter String for Windows, the Filter Function for MacOS

function getFileFilter(fileFilter) {
    if (fileFilter == undefined || File.fs == "Windows") {
        return fileFilter;
    }
    else {
        // Mac
        var extArray = fileFilter.split(":")[1].split(",");
        return function fileFilter(file) {
            if (file.constructor.name === "Folder") return true;
            if (file.alias) return true;
            for (var e = 0; e < extArray.length; e++) {
                var ext = extArray[e];
                ext = ext.replace(/\*/g, "");
                // log.writeln(ext);
                // log.writeln(file.name);
                // log.writeln(file.name.slice(ext.length * -1) === ext);
                // log.writeln("---")
                if (file.name.slice(ext.length * -1) === ext) return true;
            }
        }
    }
}

Trim path

function TrimPath(path) {
	path = decodeURI(path);
	var trimPath = ((path.split("/").length > 3) ? ".../" : "") + path.split("/").splice(-3).join("/");
	return trimPath;
}

How to open a file of specific type(s) both on Mac and Windows:

if (isOSX()) {  
    var csvFile = File.openDialog('Select a CSV File', function (f) { return (f instanceof Folder) || f.name.match(/\.csv$/i);} );  
} else {  
    var csvFile = File.openDialog('Select a CSV File','comma-separated-values(*.csv):*.csv;');
}

if (File.fs == "Windows") {
	scriptFile = File.openDialog("Pick a script", "JavaScript files:*.jsx, JavaScript files: *.js, Binary JavaScript files: *.jsxbin, Visual Basic Script files:*.vbs, All Files:*.*");
}
else {
	scriptFile = File.openDialog("Pick a script", function(file) { return file instanceof Folder || (!(file.hidden) && (file.name.match(/\.scpt|jsx*(bin)*$/i))); }, false);
}

Sort file names function for Mac (workaround for a Mac OSx bug)

Save dialog — function written by Trevor — opens save dialog on Mac and makes sure the file gets saved with the correct extension.

See also tips here