Skip to content
Fabian Morón Zirfas edited this page Jun 13, 2025 · 3 revisions

Here is an Example of a Debugger Class

Achtung! Classes work in JavaScript but the File object used is an ExtendScript thing that does not work in JavaScript.

/**
 * This is a debugger class for creating files with infos about stuff
 * @param {Boolean} dbg if true the debugger will be initialized
 * @param {String} title the title in the debug info file
 * @param {Stirng} message An alert message can be set via DEebugger.message = "something"
 *
 *
 * // make a new Debugger
 * var deeBug = new Debugger(true,"MyScript.jsx version 0.1.2","This is a debug version");
 *
 * // alert the message
 * deeBug.message();
 *
 * // add a line to the file
 * deeBug.addLineToInfo("This is a line in the file" + something);
 *
 * // create the file always on the desktop right now always with the name
 * // "debuginfo" can be set via deeBug.filename = "myfile" no extension will be .txt
 * // he will try to execute the file in the std editor
 *
 * deeBug.writeInfos();
 *
 *
 */

function Debugger(dbg, title, message) {
	this.DEBUG = dbg;
	this.filename = "debuginfo";
	this.filepath = "~/Desktop/" + this.filename + ".txt";
	this.debugstrings = [title, message];
	this.messageString = message;

	this.addLineToInfo = function (line) {
		if (this.DEBUG == true) {
			this.debugstrings.push(line);
		}
	};

	this.message = function () {
		if (this.DEBUG == true) {
			alert(this.messageString);
		}
	};

	this.writeInfos = function () {
		if (this.DEBUG == true) {
			// get the textfile
			var write_file = File(this.filepath);

			if (!write_file.exists) {
				// if the file does not exist create one
				write_file = new File(this.filepath);
			} else {
				// if it exsists ask the user if it should be overwritten
				var res = confirm(
					"The file already exists. Should I overwrite it",
					true,
					"titleWINonly",
				);
				// if the user hits no stop the script
				if (res != true) {
					return;
				}
			}

			var out; // our output
			// we know already that the file exist
			// but to be shure
			if (write_file != "") {
				//Open the file for writing.
				out = write_file.open("w", undefined, undefined);
				write_file.encoding = "UTF-8";
				write_file.lineFeed = "Unix"; //convert to UNIX lineFeed
				// txtFile.lineFeed = "Windows";
				// txtFile.lineFeed = "Macintosh";
			}
			// got an output?
			if (out != false) {
				// loop the list and write each item to the file
				write_file.writeln(this.debugstrings.join("\n"));
				// allways close files!
				write_file.close();
				write_file.execute();
			}
		}
	};
}

Usage:

    #include 'Debugger.jsx';// include the file. needs to be next to this script
    var deeBug = new Debugger(true,"MyScript.jsx version 0.1.2","This is a debug version");//     Debugger init global
    main();
    function main(){
      deeBug.message();// this is just an alert that only pops up if the debugger is true
      var something = 5;
      something++;// do something
      deeBug.addLineToInfo("This is a line in the file" + something); // add some content
      deeBug.writeInfos();// writes and opens a file
    }

Home

Clone this wiki locally