Get current desktop handle in an Expression

I have two monitors with independent desktops showing the same window.

I want to create an expression binding on the background color of the window so that the background color changes to Red if I'm on Screen 1, and Blue if im on Screen 2.

I know how to get the current desktop handle via scripting (system.gui.getCurrentDesktop), how do I get this through an expression?

You can use runScript(), preferably with polling turned off. Like so:

runScript('system.gui.getCurrentDesktop()', 0)

Hi, this runs, but not the expected result:

I've added 2 x Text Labels to a Vision window.
I've then opened the same window on 2 x desktops.

The label using runScript('system.gui.getCurrentDesktop()', 0) returns primary on both desktops.
The label using runScript('system.gui.getScreenIndex()', 0) returns 0 on both desktops.

If I remove the Expression, and instead add a script to the mouseClicked event for the text labels:

value = system.gui.getCurrentDesktop()
event.source.text = value

When clicking on the Text Label on both Desktops, I get the expected behavior of the Text on Desktop 1 changing to primary and on Desktop 2 to display_2.

What is the mistake I'm making here?

If it helps, I'm using Version: 8.1.20.

A search on this forum is telling me this issue has been around for a while.

One way I've got around it is by adding a Custom Property to the Vision Window, then have a Script on the visionWindowOpened Event that passes the current desktop handle to the value of the custom property:

value = system.gui.getCurrentDesktop()
system.gui.getParentWindow(event).getComponentForPath('Root Container').Monitor = value

You can then reference this Custom Property's value in an Expression.

Not the most elegant way of solving the issue, but it seems to work.

I wonder if it's some type of scoping issue. I have a script that was written for a much different purpose that uses swing utilities to determine which desktop is in focus from a client event script, and it was easy to adapt for your usage case. If you add the following custom method to your component, you can reference it with the following expression: runScript('self.getDesktop()', 1000) With the poll rate set to zero, this didn't work. However with the poll rate set to 1 second, this switched the text in all of the test labels perfectly as I clicked on the various desktop instances.

#def getDesktop(self):#Custom Method
		self: A reference to the component instance this method is invoked on. This argument
		  is automatic and should not be specified when invoking this method.
	from javax.swing import JFrame,SwingUtilities
	def getTheData(testWindow,selectedDesktop):
		if testWindow.isFocused():
			return selectedDesktop
	header = ["DesktopHeaders"]
	desktopList = system.gui.getDesktopHandles();
	desktopColumn = [[desktop] for desktop in desktopList]
	desktopSet = system.dataset.toDataSet(header, desktopColumn)
	desktopCount = desktopSet.getRowCount()
	for desktop in range(desktopCount):
		selectedDesktop =  desktopSet.getValueAt(desktop, 0)
		locatedDesktop = getTheData(SwingUtilities.getAncestorOfClass(JFrame,system.gui.desktop(desktopSet.getValueAt(desktop, 0)).getWindow(system.nav.desktop(desktopSet.getValueAt(desktop, 0)).getCurrentWindow())),selectedDesktop)
		if locatedDesktop is not None:
			return locatedDesktop
	return system.gui.getCurrentDesktop()

Hmm. That's not good. I'd report it as a bug. runScript() certainly gets a context that connects to the current desktop, but is not passing it to the script environment.