Isotropix Forums

API to get native window ID?

Clarisse Scripting related topics

API to get native window ID?

Unread postby briansilva » Wed Oct 02, 2019 12:43 pm

Hello! I'm setting up some automated screen grabbing, and it would be very useful to get the native window ID for the Clarisse window. Is there any way to get this from the Clarisse API? A process number might also be enough to roll a solution.

Thanks!
briansilva
 
Posts: 49
Joined: Mon Mar 25, 2019 12:43 pm

Re: API to get native window ID?

Unread postby anemoff » Wed Oct 02, 2019 1:39 pm

To get Clarisse process ID, assuming the script is running in Clarisse, it's as simple as this

python code

import os
os.getpid()


There's no Clarisse API to get the Clarisse window ID.
But you should be able to get it from the PID using platform-specific code or tools.

Here is an example, for Linux, based on the 3rdparty tool "wmctrl":

python code

import os
import subprocess

def get_clarisse_winid():
"""
Gets the window ID (as an integer) of the current Clarisse instance.
"""

# get Clarisse PID to find the corresponding window ID
clarisse_pid = os.getpid()
#print('[debug] Clarisse PID: {}'.format(clarisse_pid))

winid = None
try:
# list all window IDs with their PID
# you need to instal "wmctrl" on your system
output = subprocess.check_output("wmctrl -l -p", shell = True)

# keep only the lines for Clarisse app
# there might be multiple matches if there are multiple Clarisse instances running
clarisse_title = 'Isotropix Clarisse'
matches = [line for line in output.splitlines() if clarisse_title in line]

# find the window that matches Clarisse PID
for line in matches:
# each output line is formatted as follows:
# "window_id desktop_number pid window_name"
parts = line.split()
if len(parts) >= 3 and int(parts[2]) == clarisse_pid:
# the window ID is in hexadecimal, convert to decimal
winid = int(parts[0], 0)
#print('[debug] Clarisse window ID: hex = {}, dec = {}'.format(parts[0], winid))
break

except subprocess.CalledProcessError as e:
print(e)
except Exception as e:
print(e)

return winid


Cheers,
Anthony Nemoff
Isotropix
R&D Engineer
User avatar
anemoff
 
Posts: 272
Joined: Wed Jan 13, 2016 10:10 am

Re: API to get native window ID?

Unread postby briansilva » Wed Nov 20, 2019 12:10 pm

That's great Anthony, thanks for the example!
briansilva
 
Posts: 49
Joined: Mon Mar 25, 2019 12:43 pm


Return to Scripting