How to show Monitor Number using the Label Display?

Im trying to show the Monitor Number using the Label Display when opening the multi monitor fullscreen. Is there an expression that I can use?

You might find this post useful:

1 Like

There's no expression function solution, it'll require some amount of scripting.

Would you be able to guide me on how to accomplish this?

The scripting documentation here is excellent:

1 Like

I suppose it depends on what the screen index is being used for. When I position windows in multi monitor setups, I want the screens to be sequentially indexed from left to right and top to bottom because my windows get covered up, and it makes it easier to find a specific instance in a specific monitor when tabbing. The problem is, screen indexes don't often work that way.

Here's a variation on a script I developed that will return an index based upon which monitor the parent window is in from left to right, top to bottom:

# Import:
# ...GraphicsEnvironment for mapping the screens as they are in reality
# ...JFrame and SwingUtilities for getting the ancestor JFrame of a given component
from java.awt import GraphicsEnvironment
from javax.swing import JFrame, SwingUtilities

def getScreenIndex(component):
	# Get the screen bounds from the graphics environment and sort them from left to right, top to bottom
	screens = sorted([device.defaultConfiguration.bounds for device in GraphicsEnvironment.getLocalGraphicsEnvironment().screenDevices], key=lambda screen: (screen.x, screen.y))
	parentFrame = SwingUtilities.getAncestorOfClass(JFrame, component)
	for index, screen in enumerate(screens):
		if screen.contains(parentFrame.location()):
			return index

# Call the function from any component in any window and do something with the returned screen index
print getScreenIndex(event.source)
3 Likes

Hi Justin,

Thanks for the reply. I have a newbie question. Where do I actually put this script on. Also when you say print where does it go actually?

Put it in the script library, and call it from anywhere.

Print:
At the top of the designer, there is Tools-->Console, or Help-->Diagnostic and select the console tab.

Either place will display the print statement.

I mean how do you direct the output to a component label text? I got the script and tested in the script console and see the output that i want. But i dont know how to direct that to show it on my Label component.

Put it in the project library, and then use runScript to run it directly in a binding on the text property of your label:

I already have a handle name that shows the correct Monitor Number. I just need to show it on different spot on my header. The reason for it is because when I open the windows full screen on startup the Monitor Number on top left is not visible. hopefully there is way to call the handle name to show as text on Label component.

image

The handle name is simply Monitor 1, Monitor 2, etc? If so, simply modify the return statement like so:

return 'Monitor {}'.format(index)

the script that you provide that goes to the library I cant get it to work to into my intention here. it is giving me an event missing error.

if you are using a runscipt binding instead of an event handler, you will need to use self for the argument instead of event.source

now it is giving a self not defined error.

Can you show your expression on how you are calling the runScript?

runScript("MonitorNumber.getScreenIndex()")

Here is the script in the library.

# Import:
# ...GraphicsEnvironment for mapping the screens as they are in reality
# ...JFrame and SwingUtilities for getting the ancestor JFrame of a given component
from java.awt import GraphicsEnvironment
from javax.swing import JFrame, SwingUtilities

def getScreenIndex(component):
	# Get the screen bounds from the graphics environment and sort them from left to right, top to bottom
	screens = sorted([device.defaultConfiguration.bounds for device in GraphicsEnvironment.getLocalGraphicsEnvironment().screenDevices], key=lambda screen: (screen.x, screen.y))
	parentFrame = SwingUtilities.getAncestorOfClass(JFrame, component)
	for index, screen in enumerate(screens):
		if screen.contains(parentFrame.location()):
			return 'Monitor {}'.format(index)

# Call the function from any component in any window and do something with the returned screen index
print getScreenIndex(self)

You need to pass the "self" into the script.

Try this:

runScript("MonitorNumber.getScreenIndex",0,"self")

Edited the call... removed the ()

1 Like

runScript() cannot pass self as an argument when the first argument is a bare function. You have to use runScript("someScript.someFunction(self)", 0) and not pass any arguments, or use a component custom method (which implicitly gets self).

If you need more complex one-liners, use objectScript() from my Integration Toolkit.