Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use UIAutomationClient from console project manually referencing UIAutomationClient.dll #8880

Closed
LyndonGingerich opened this issue Mar 5, 2024 · 3 comments
Labels
Question General question, not a problem in source code or documentation (yet)

Comments

@LyndonGingerich
Copy link

Context

I am tired of having to make four clicks to open a specific meeting in Microsoft Teams, so I decided to make a script to do it with a custom key combination. Phind gave me a starting script using System.Windows.Automation:

Phind's script
open System
open System.Windows.Automation

// Initialize the UI Automation framework
let automation = new UIAutomationClient.CUIAutomation()

// Define a function to find a window by its title
let findWindowByTitle title =
    automation.GetRootElement().FindFirst(TreeScope.Descendants, 
        Automation.ControlTypeProperty.PropertyCondition(ControlType.Window), 
        Automation.NameProperty.PropertyCondition(title))

// Define a function to click a button by its name
let clickButtonByName name =
    let button = automation.GetRootElement().FindFirst(TreeScope.Descendants, 
        Automation.ControlTypeProperty.PropertyCondition(ControlType.Button), 
        Automation.NameProperty.PropertyCondition(name))
    button.InvokePattern.Invoke()

// Define a function to move and maximize a window
let moveAndMaximizeWindow (window: AutomationElement) =
    let windowPattern = window.GetCurrentPattern(WindowPattern.Pattern) :?> WindowPattern
    windowPattern.SetWindowVisualState(WindowVisualState.Maximized)
    // Moving the window to a specific monitor would require calculating the screen position
    // This example does not cover moving the window

// Main script logic
let main argv =
    // Attempt to find the Microsoft Teams window
    let teamsWindow = findWindowByTitle "Microsoft Teams"
    if teamsWindow <> null then
        // Attempt to find and click the "Join Now" button
        clickButtonByName "Join Now"
        // Move and maximize the window (example)
        moveAndMaximizeWindow teamsWindow
    else
        printfn "Microsoft Teams window not found."

Current state

After a convoluted sequence of potential solutions tried, I arrived at a .NET project targeting net8.0-windows (in case these utilities are Windows-specific).

I have referenced the DLL directly from my .fsproj:

<ItemGroup>
    <Reference Include="UIAutomationClient">
        <HintPath>[dotnet path]\packs\Microsoft.WindowsDesktop.App.Ref\8.0.2\ref\net8.0\UIAutomationClient.dll</HintPath>
    </Reference>
</ItemGroup>

Now I can open System.Windows.Automation, which is more than I have been able to do using other means so far, but UIAutomationClient is still not defined, despite the DLL reference.

What am I missing?

dotnet/core#3283 suggests that I need to be writing a .NET desktop application, but I specifically wish to avoid clicks, not to open more GUI windows, so I would prefer to use a console application if at all possible.

@ghost ghost added the Untriaged label Mar 5, 2024
@huoyaoyuan
Copy link
Member

You can create a Console application with Windows Desktop SDK, like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="Program.fs" />
  </ItemGroup>

</Project>

Note the OutputType here. UseWPF will bring you the UIAutomation library correctly.

but UIAutomationClient is still not defined, despite the DLL reference.

CUIAutomation looks like the COM based UI automation, not the one in System.Windows.Automation.

@jeffschwMSFT jeffschwMSFT transferred this issue from dotnet/runtime Mar 6, 2024
@singhashish-wpf singhashish-wpf added Question General question, not a problem in source code or documentation (yet) and removed Untriaged labels Mar 8, 2024
@singhashish-wpf
Copy link
Member

@LyndonGingerich Does the answer from @huoyaoyuan solve your issue?

@singhashish-wpf singhashish-wpf added the 📭 waiting-author-feedback To request more information from author. label Mar 8, 2024
@LyndonGingerich
Copy link
Author

Thank you. I will back up and try using a different tutorial. UseWPF seems to be working.

@dotnet-policy-service dotnet-policy-service bot removed the 📭 waiting-author-feedback To request more information from author. label Mar 12, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Apr 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question General question, not a problem in source code or documentation (yet)
Projects
None yet
Development

No branches or pull requests

3 participants