ES3 and Line Number?

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: Kukurykus, Tom, xbytor

Wolf_22
Posts: 14
Joined: Tue Nov 24, 2020 7:00 pm

ES3 and Line Number?

Post by Wolf_22 »

I'm currently working on an ES3 script and one thing I haven't figured out how to do (maybe it's impossible to do?) is output line numbers at controlled locations in the code (such as inside a function that I'm trying to debug).

Ideally, I'd love to do something like the following:

Code: Select all

var line_number = ?;
$.writeln(line_number);
From what I understand, some versions of ECMAScript appear to offer this in some form or fashion. For example, I've come across various examples that appear to make use of try/catch logic which handle the error object to inspect it for a lineNumber property... Sadly, when I try this same thing in ES3, the entire processing of the script halts in ESTK as if I issued a "debugger;" statement (when I would prefer that it just keep running), but any error object I happen to have access to according to the Data Browser only seems to show a "line" property, which also has zero as its value.

So, is it possible to obtain the line number at a specified line within a script?

User avatar
Kukurykus
Posts: 395
Joined: Mon Jul 25, 2016 12:36 pm

Re: ES3 and Line Number?

Post by Kukurykus »

Code: Select all

Error().line

Wolf_22
Posts: 14
Joined: Tue Nov 24, 2020 7:00 pm

Re: ES3 and Line Number?

Post by Wolf_22 »

When I use...

Code: Select all

$.writeln(Error().line);
...the JavaScript Console always reads '0' (zero). Any ideas what might be causing that?

User avatar
Kukurykus
Posts: 395
Joined: Mon Jul 25, 2016 12:36 pm

Re: ES3 and Line Number?

Post by Kukurykus »

For me it's number of the line it's put at. Maybe get latest ESTK 4.0.0.1?

Wolf_22
Posts: 14
Joined: Tue Nov 24, 2020 7:00 pm

Re: ES3 and Line Number?

Post by Wolf_22 »

Weird. Looks like that's the version I'm using according to Help > About ExtendScript Toolkit...: It's showing "4.0.0.1 ExtendScript 4.5.5 ScriptUI 6.2.2"...

User avatar
Kukurykus
Posts: 395
Joined: Mon Jul 25, 2016 12:36 pm

Re: ES3 and Line Number?

Post by Kukurykus »

Then I have no idea what to do. Maybe reinstall it...

Wolf_22
Posts: 14
Joined: Tue Nov 24, 2020 7:00 pm

Re: ES3 and Line Number?

Post by Wolf_22 »

I'll give that a shot and see what happens. I'm about to leave the office but I'll do it tomorrow and follow up with the results.

Wolf_22
Posts: 14
Joined: Tue Nov 24, 2020 7:00 pm

Re: ES3 and Line Number?

Post by Wolf_22 »

Well, I uninstalled and reinstalled but I'm seeing zeros in the JavaScript Console every time I use the "$.writeln(Error().line);" statements... No matter where I put them, it always shows zero for its output. Ugh.

Wolf_22
Posts: 14
Joined: Tue Nov 24, 2020 7:00 pm

Re: ES3 and Line Number?

Post by Wolf_22 »

Okay, I think I'm making some sort of progress here and I think it might be in how I'm using ESTK...

I found the following script after scouring Google and StackOverflow:

Code: Select all

	Error.prototype.toString = function() {
		if (typeof this.stack === "undefined" || this.stack === null) {
			this.stack = "placeholder";
			// The previous line is needed because the next line may indirectly call this method.
			this.stack = $.stack;
		}
		return "Error";
	}

	try {
		var foo = null;
		foo.bar;
	}
	catch (e) {
		$.writeln("Line: " + e.line);
		$.writeln("File: " + e.fileName);
		$.writeln("Message: " + e.message);
		$.writeln("Stack: " + e.stack);
	}
Since this piece of code appeared to expose the line number in the catch block, I went ahead and quickly plopped it into my script to test it out. ESTK halted / broke processing on the line "foo.bar" line due to seeing that "null is not an object."

But here's where it gets interesting...

Normally, I'd press the "STOP" button in ESTK to make processing stop, but when I press the "PLAY" button to keep going without clearing the runtime error (pressing NO on the ESTK "Clear runtime error?" prompt, I mean), I FINALLY see line number information, only the line number reflects the foo.bar line.

So I think I must be using ESTK incorrectly or at least in such a way that prevents me from seeing or exposing the line number information as the above works, just not the way I want.

I need to find a way to basically dump the line number on a line I choose in my script and hopefully without needing to manually tell ESTK to disregard runtime errors, etc. Simply put, I'm looking for the same functionality you have with the "__LINE__" magic constant in PHP, only in this ExtendScript, and hopefully without needing to force ESTK to halt processing and be forced to manually disregard a runtime error just to dump the line info...

User avatar
Kukurykus
Posts: 395
Joined: Mon Jul 25, 2016 12:36 pm

Re: ES3 and Line Number?

Post by Kukurykus »

Try on other machine?