Clear output console contents from script

Is it possible to clear the content of the output console from a script? Or to simulate clicking the Clear button to do the same? Both in the designer and the client?

from com.inductiveautomation.ignition.client.util.gui import OutputConsole
OutputConsole.getInstance().getComponent(0).getComponent(1).doClick()

Works in the designer, not the client. In the client, you’d have to find the separate window that holds the ‘Diagnostics’, then step into it, find the button, and click it.

2 Likes

Is this still possible?

OutputConsole.getInstance().getComponent(0).getComponent(1).doClick()

Tells me:

NameError: name 'OutputConsole' is not defined

Anything I would need to import?

The formatting got messed up in some forum migration; how about with the import on the first line?

Also, a better/less fragile way to do the same thing:

from com.inductiveautomation.ignition.client.util.gui import OutputConsole
from java.awt.event import ActionEvent

console = OutputConsole.getInstance()
console.actionPerformed(ActionEvent(console, 0, "clear"))
1 Like

Actually neither of them seemed to do it, maybe a good clarification is that I an running this within the script console itself, would that affect things?

Oh, the script console is totally different. This is clearing the ‘Output Console’ you get in the designer. I don’t see any good way to trigger a programmatic clear of the script console. Though, fun fact, Ctrl + L does work there.

Ahh, read that wrong darn

Was looking for a way to clear it between the running of each script so I could just keep hitting the execute button and monitoring changes, Ctrl+L will have to do! Thanks

Any updates on this? I have some scripts that run for 45 minutes and I would love some way to track the progress without clogging up the output window with thousands of different print messages, ideally I would use some function to clear the previous output before sending out a new one.

I've used the method Paul provides in the above posts in similar scenarios, and it works for clearing the console programmatically, so I'm not sure what you are asking.

1 Like

I'm talking about the script console, not the output console. The above method doesn't work, try this script to see for yourself:

from com.inductiveautomation.ignition.client.util.gui import OutputConsole
from java.awt.event import ActionEvent

console = OutputConsole.getInstance()

num = 0
while num < 10:
	console.actionPerformed(ActionEvent(console, 0, "clear"))
	print(num)
	num += 1

That makes sense. I'll look into this when I get time if nobody else beats me to it.

Interestingly Ctrl-L works when the focus is in the output pane and Ctrl-L is ASCII form-feed control code \x0C. Printing that by Script Console script generates a blank line but doesn't clear the window.

I'm not in front of my computer, but a quick search from my phone turned up this class that looks like it could be it:
https://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.20/com/inductiveautomation/ignition/designer/gui/tools/jythonconsole/JythonConsole.html

Edit: Subsequent testing revealed that the below method only programatically clears any output history. It will work at the start of the script to clear what is already there, but it won't clear outputs in real time.

@Harry_O_Brien
I messed around with this, and I was able to clear the script console in this way:

from java.awt import Window
from com.inductiveautomation.ignition.designer.gui.tools import InteractiveScriptPlayground
from com.inductiveautomation.ignition.designer.gui.tools.jythonconsole import JythonConsole
def clearConsole(window):
	for component in window.getComponents():
		if isinstance(component, JythonConsole):
			component.clear()
			break
		else:
			clearConsole(component)
for window in Window.getWindows():
	if isinstance(window, InteractiveScriptPlayground):
		clearConsole(window)
		break

The above script simply locates the window with the jythonConsole, digs the console out recursively, and clears it.

1 Like

I probably don't use the script console as often as I should, but I like the idea of using it because it has an interrupt button that could potentially save me from having to kill the designer and loose progress when I make some sort of gui locking mistake. Therefore, this seems like a useful idea to me, and it's probably worth taking the time to develop.

The first thing I wanted to do was to try to make what I developed in the above post a little more concise, so I flattened the window search into a single line by converting it to a generator, and I dug around under the hood and found that I could eliminate the recursive function by getting the jythonConsole from the window's 'console' property using reflection.

This all worked as expected, and when I fired the console's clear() method, the console emptied out just like before. At this point, I felt good because I had eliminated an import and reduced the script from 14 lines to 6 lines. Then, I set up a looped print statement and discovered that clear() method only clears the prior outputs [the history]. It doesn't clear any of the print statements from the currently running script. Digging around under the hood some more, I found that the console had an emptyDocument method that can be invoked via reflection, and it clears the text from the console in real time. Once I've obtained the method, I can invoke it at any time during a long running script, and it will clear all of the text that has printed up to that point and then continue printing from there.

Then, I remembered that I can press the interrupt button while the script is running. In fact, I can interact with anything in the designer while the script is running. Since scripts running in the console do not lock the gui, I have to conclude that they are running in an asynchronous thread. Therefore, I have to ask: how unsafe is this? Should invokeLater be used here, and if so, will it behave correctly in this scope?

1 Like

Tricks like this will almost certainly stop working on 8.1.33 with Java 17, just as a heads up. Strong encapsulation prevents this unless you jump through some hoops.

As for safety, yes, you should probably be moving back to the EDT.

1 Like

That's unfortunate because the whole thing would have to go to the EDT for it to work sequentially, which would break the interrupt button. For this functionality, the normal output console seems more appropriate.

Yes. If it's not feasible to add, then maybe create a plugin with vscode so we can write our code in a real IDE and execute them in the script console. This would probably be much much easier than adding all of the functionality of git, formatting, etc from an IDE. It is much easier to program the terminal output in VSCode also, you could hit about 17 birds with 1 stone by adding this feature, getting all of the functionality of VSCode in Ignition.

Ouch.

You do Java in VS?