Search code examples
pythonattachmate-extraattachmate-reflection

Win32com interfacing with Reflection Desktop


I run a number of python scripts and programs to aggregate and edit data in Attachmate Extra. My company was on an old version of Attachmate Extra until recently. I'm testing the new version (Reflection Desktop v. 16.2) and my scripts no longer work. I built them with the aid of the helpful advice on this link. I would like to be able to control (scrape, write, etc) screens on the new version.

Here's where I currently am. Running this code creates the a new window:

system = win32com.client.Dispatch("ReflectionIBM.Session")
system.Visible = True

...but then from there I cannot do any of the commands I previously used. Running this, for example

system.MoveCursor(11, 65)

creates a new tab in the emulator that doesn't connect to a session. I've looked all around the Reflection documentation for an answer. This page led me to believe the old session method is no longer necessary but I'm not sure. I think I'm wrapping the correct object, and the documentation says that legacy commands still work, but I haven't figured out how to link them.

For reference, here are the lines I was using previously to connect to Attachmate:

system = win32com.client.Dispatch("EXTRA.System")
sess0 = system.ActiveSession
Screen = sess0.Screen

Any help is appreciated. I've been scouring the win32com browser for a list commands and looking through the registry to find available classes but I don't know what to look for. Thank you!

EDIT:

I previously used a couple of functions to read, write, and move the cursor around within Attachmate. Example:

def write(screen,row,col,text):
    screen.row = row
    screen.col = col
    screen.SendKeys(text)

write(screen, 10, 65, "test")

Is there a way to get this working again in Reflection?


Solution

  • I still do not know why this works the way it does. In VBA the GetObject method works on "Reflection Workspace" but in python that did not produce any usable attributes that I could find. For python, to get the active session object I had to use EXTRA.System:

    from win32com.client.gencache import EnsureDispatch
    screen = EnsureDispatch("EXTRA.System").ActiveSession.Screen
    

    From there the code seems generally the same as VBA with

    screen.GetString(row, col, len)
    screen.PutString(data, row, col)
    screen.SendKeys('<PF1>')
    

    for interacting with the host.

    After more documentation reading and more trial and error I solved it. "EXTRA.System" is kept for legacy reasons so technically will still work. However, to connect to an active session of Reflection this worked:

    system = win32com.client.GetObject('Reflection Workspace')
    

    then to get the active view:

    screen = system.GetObject("Frame").SelectedView.Control.Screen
    

    or a specific numbered view:

    screen = system.GetObject("Frame").view(1).Control.Screen
    

    The code for interacting with Reflection also has changed and now looks like:

    screen.GetText(row, col, len)
    screen.PutText2(data, row, col)
    screen.SendControlKey(ControlKeyCode)
    

    The documentation for ControlKeyCode does not seem to provide the codes for the control keys. However, you can find the definitions in the Visual Basic Object Browser that comes with Reflection. In Reflection, on the macros tab, click Visual Basic, then press F2 and search for ControlKeyCode. A list of them should show up. For example, mine shows ControlKey_F1=10.

    screen.SendKeys("N")
    

    can still be used to send individual key strokes such as the N key but SendControlKey seems to have replaced the command keys such as Enter, Page Up, and the Function keys.