How to take screenshots of your Ignition windows

We have a client who wanted to get a screenshot of a whole bunch of windows. We decided to write a script to do this.

Define this function in your app scripts.

def takeScreenshot(twindow):
	from java.io import File
	from java.awt.image import BufferedImage
	from javax.imageio import ImageIO
	import system
	
	home = system.util.getProperty("user.home")
	
	window = system.gui.getWindow(twindow)
	
	rec = window.getBounds();
	
	bufferedImage = BufferedImage(rec.width, rec.height,BufferedImage.TYPE_INT_ARGB);
	window.paint(bufferedImage.getGraphics());

	temp = File(home + "/Documents/screenshots/" + twindow + ".png");
	
	temp.mkdirs()
	ImageIO.write(bufferedImage, "png", temp);

Then, define this script somewhere the user can access it. We usually put it in a client menu script

from app.tech import takeScreenshot
import time
currentWindow = system.tag.getTagValue("[System]Client\User\CurrentWindow")

	
windows = ["Put","Some","Windows","Here"]

for row in windows:
	obj = system.nav.openWindow(row)
	time.sleep(2.5)
	takeScreenshot(row)
	system.nav.closeWindow(row)
system.nav.swapTo(currentWindow)

The time.sleep code is only to allow the window to populate its bindings. You may need the increase the length of this delay.

3 Likes

Nice

I want to make a screenshot but for windows that are not open, it is not possible with getWindows (), the objective of this script is to see the window before opening it. Can you help me please

2 Likes

Thanks, good function

Getting This error:
File “”, line 11
temp = File(home + “/Documents/screenshots/” + twindow + “.png”);
^
IndentationError: unindent does not match any outer indentation level

Consistent indentation is very important in python. You can use tabs to indent, you can use space to indent, but you can’t mix them in the same block.

1 Like

Is there a good way to get a screenshot of everything that visible on the screen? For example, we’d like to include a header bar in the screenshot as well. The script in the original post uses the “.paint” on the window pyobject (is there a good place to find documentation for that?).

We have a shared template we drop on the header of all our Vision apps. When you click, in the actionPerfomed script we run the following that captures the screenshot and prompts the user to email the Ignition developers a copy (with the user automatically copied).

import os.path, sys
from java.awt import Window
from javax.swing import SwingUtilities

# Get the containing Swing container for Igniton
window = SwingUtilities.getAncestorOfClass(Window, event.source)

# Setup the To email addresses for the Screenshot
emailTo = ['somoeonethatcares@aol.com']

# Setup a filename and directory for the screenshot
dir = shared.util.selectDirectory('Select a directory to save screenshot...')
if dir is None:
	sys.exit()
filename = '%s_%s.jpg' % (system.date.format(system.date.now(), 'yyyyMMdd_HHmmss'), system.util.getProjectName())
file = os.path.join(dir, filename)

# Generate and save the screenshot
system.print.printToImage(window, file)

# Prompt the user to email the screenshot
if system.gui.confirm('<html>A screenshot has been saved to <strong>%s</strong>. <br><br>Do you want to email a copy to the Ignition Application team?' % file, 'Email Screenshot'):

	# Setup the attachment
	attachmentNames = [filename]
	attachmentData = [system.file.readFileAsBytes(file)]
	
	# Prompt the user to add a message
	msg = system.gui.inputBox('A screenshot of the application has been captued and will be sent to the Ignition developers.  Please enter a short message with any details you wish to provide.', '')
	
	#Read network diag info
	hostname = system.tag.read('[System]Client/Network/Hostname').value
	ipAddress = system.tag.read('[System]Client/Network/IPAddress').value
	gatewayAddress = system.tag.read('[System]Client/Network/GatewayAddress').value

	#Read system diag info
	now = system.date.now()
	editCount = system.tag.read('[System]Client/System/EditCount').value
	fpmiVersion = system.tag.read('[System]Client/System/FPMIVersion').value
	javaVersion = system.tag.read('[System]Client/System/JavaVersion').value
	os = system.tag.read('[System]Client/System/OperatingSystem').value
	systemFlags = system.tag.read('[System]Client/System/SystemFlags').value

	#Read user diag info
	osUser = system.tag.read('[System]Client/User/OSUsername').value
	username = system.tag.read('[System]Client/User/Username').value

	# Construct the email body
	body = """
	<html>
	<body>
	<h2>
	<strong>%s User Screenshot</strong>
	</h2>
	<p>User Message: %s<strong></strong></p>
	<hr>
	<h2>
	<strong>Project Diags : %s</strong>
	</h2>
	<br>
	<h3>
	<strong>Network Diagnostics:</strong>
	</h3>
			 <br> System Hostname : %s
		 <br> IP Address : %s
		 <br> Gateway Address : %s
		 <br>
		 <br>
	<h3>
	<strong>System Diagnostics:</strong>
	</h3>
	<br> Current Date/Time : %s
	<br> Edit Count : %s
	<br> FPMI Version : %s
	<br> Java Version : %s
	<br> Operating System : %s
	<br> System Flags : %s
	<br>
	<br>
		<h3>
	<strong>User Diagnostics:</strong>
	</h3>
	<br> OS Username : %s
		<br> Client Username : %s
	<br>
	</body>
	</html> 
	""" % (system.util.getProjectName(), msg, system.util.getProjectName(), hostname, ipAddress, gatewayAddress, now, editCount, fpmiVersion, javaVersion, os, systemFlags, osUser, username)
	
	# CC The source user if they have email setup
	cc = []
	try:
		contacts = system.user.getUser(system.tag.read('[System]Client/System/UserSource').value, username).getContactInfo()
		for contact in contacts:
			if contact.contactType == "email":
				if contact.value:
					cc.append(contact.value)
	except:
		pass
	
	# Send the email
	system.net.sendEmail(
		smtpProfile='default',
		subject='Screenshot | %s' % system.util.getProjectName(),
		body=body,
		html=True,
		to=emailTo,
		attachmentNames=attachmentNames,
		attachmentData=attachmentData,
		cc=cc
	)
3 Likes

realy nice

i have this error:
NameError: name ‘shared’ is not defined

That’s a function we wrote to select a dir only as the built in system function has you select a file name. Personal preference of how you want to build it. This is just an example. Here is the selectDirectory function if you want to use it.

def selectDirectory(title='Select Folder', startingPath=None):
	"""
	==========================
	Directory Selection Dialog
	==========================
	
	Allows user selection of a directory, and returns the path to the selected directory.
	Utilizes Java Swing JFileChooser dialog window.
	
	parameters:
		-[startingPath] : Optional. Path to starting directory.  Cannot contain symbolic links.
		-[title] : Optional.  Title for dialog window.
		
	output:
		-path : The full path to the selected directory.  None if user cancels the file selection.
	"""
	
	from javax.swing import JFileChooser
	from java.io import File
	import os.path
	
	if not startingPath == None:
		path = startingPath
	else:
		user = system.tag.read('[System]Client/User/HomeFolder').value
		path = os.path.join(user, 'Documents')
	
	chooser = JFileChooser()
	chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY)
	chooser.setAcceptAllFileFilterUsed(True)
	chooser.resetChoosableFileFilters()
	chooser.setDialogTitle(title)
	chooser.setCurrentDirectory(File(path))
	
	if chooser.showOpenDialog(None) == JFileChooser.APPROVE_OPTION:
		path = str(chooser.getSelectedFile()).replace('\\', '/')
	else:
		path = None
	return path

I used;

filename = system.file.saveFile(“myscreenshot”+".jpg")

thakns you