I used ChatGPT to modify the code in the following link.
https://github.com/qgindi/LibreAutomate/...Api_UIA.cs
I attempted to load the assembly using the following two methods:
/*/ com UIAutomationClient 1.0 #b183b9d9.dll; /*/
/*/ nuget -\Interop.UIAutomationClient; /*/
However, after execution, I encountered the same error. The error message is as follows:
System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class factory for component with CLSID {30CBE57D-D9D0-452A-AB13-7AC5AC4825EE} failed due to the following error: 80040154 Class not registered (0x80040154 (REGDB_E_CLASSNOTREG)).
at line 56 in Script7.cs, PowerShellCaretUtil.GetCaretRectInPowerShell(RECT& rect)
at line 7 in Script7.cs
>>
I don't quite understand the execution principle of the code, so there might need to be some changes.
Additionally, I am eager to know how to implement this functionality(GetCaretRectInPowerShell) in QM. I am using the following QM code, but I am still unable to achieve the desired functionality.
Thanks in advance for any suggestions and help
David
Function getISECaretPos
Trigger Ax
ChatGPT Code:
https://github.com/qgindi/LibreAutomate/...Api_UIA.cs
I attempted to load the assembly using the following two methods:
/*/ com UIAutomationClient 1.0 #b183b9d9.dll; /*/
/*/ nuget -\Interop.UIAutomationClient; /*/
However, after execution, I encountered the same error. The error message is as follows:
System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class factory for component with CLSID {30CBE57D-D9D0-452A-AB13-7AC5AC4825EE} failed due to the following error: 80040154 Class not registered (0x80040154 (REGDB_E_CLASSNOTREG)).
at line 56 in Script7.cs, PowerShellCaretUtil.GetCaretRectInPowerShell(RECT& rect)
at line 7 in Script7.cs
>>
I don't quite understand the execution principle of the code, so there might need to be some changes.
Additionally, I am eager to know how to implement this functionality(GetCaretRectInPowerShell) in QM. I am using the following QM code, but I am still unable to achieve the desired functionality.
Thanks in advance for any suggestions and help
David
Function getISECaretPos
Trigger Ax
typelib UIAutomationClient {944DE083-8FB8-45CF-BCB7-C477ACB2F897} 1.0
UIAutomationClient.CUIAutomation automation
UIAutomationClient.IUIAutomationElement element
if automation.GetFocusedElement(&element)=0
,out 1
ChatGPT Code:
// script ""
/*/ com UIAutomationClient 1.0 #b183b9d9.dll; /*/
/*/ nuget -\Interop.UIAutomationClient; /*/
using System;
using System.Runtime.InteropServices;
// Call the method to retrieve the text cursor position
if (PowerShellCaretUtil.GetCaretRectInPowerShell(out var rect))
{
Console.WriteLine($"Caret Position: Left={rect.Left}, Top={rect.Top}, Right={rect.Right}, Bottom={rect.Bottom}");
}
else
{
Console.WriteLine("Failed to get caret position.");
}
public static class PowerShellCaretUtil
{
// Structure for rectangle
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
// UI Automation COM interface
[ComImport, Guid("30cbe57d-d9d0-452a-ab13-7ac5ac4825ee")]
internal class CUIAutomation { }
// UI Automation element interface
[ComImport, Guid("d22108aa-8ac5-49a5-837b-37bbb3d7591e")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IUIAutomationElement
{
// Method to get the current bounding rectangle of an element
[PreserveSig] int get_CurrentBoundingRectangle(out RECT retVal);
}
// UI Automation interface
[ComImport, Guid("30cbe57d-d9d0-452a-ab13-7ac5ac4825ee")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IUIAutomation
{
// Method to get the focused element
[PreserveSig] int GetFocusedElement(out IUIAutomationElement element);
}
// Method to get the text cursor position in PowerShell ISE
public static bool GetCaretRectInPowerShell(out RECT rect)
{
rect = default;
// Create UI Automation instance
var automation = new CUIAutomation() as IUIAutomation;
// Get the focused UI element
if (0 == automation.GetFocusedElement(out var element))
{
// Get the bounding rectangle of the focused element
if (0 == element.get_CurrentBoundingRectangle(out rect))
{
return true;
}
}
return false;
}
}