Processing Ajax...

Title
Close Dialog

Message

Confirm
Close Dialog

Confirm
Close Dialog

Search for Window by File Name or Title if File Not Found

Description
This script searches for a window by file name with a fall back to window text.
Language
C#.net
Minimum Version
Created By
Gilad Barnea
Contributors
-
Date Created
May 25, 2018
Date Last Modified
May 25, 2018

Scripted Function (Macro) Code

using System;
using System.Drawing;

public static class DisplayFusionFunction {
    // Based on "Search for Window by Title" by Keith Lammers
    public static void Run (IntPtr windowHandle) {

        // OPTIONAL CUSTOMIZATION PARAMETERS
        // Will focus the window with a matching file rather than window title. If no file was found, will focus based on title.
        // For example, this will focus the actual spotify window if both spotify, and a chrome page about spotify, are running.
        // Set to false to reverse priority
        bool prioritize_file_over_title = false;

        // Set some value to if you want a common app to be available just by pressing enter.
        // Personally I set it to "chrome".
        string default_app = "";

        // END CUSTOMIZATION

        // Prompt for the text to search for
        string searchText = BFS.Dialog.GetUserInput ("Enter part of the window title:", default_app);

        // Find the window with the matching file name
        IntPtr window_by_file = BFS.Application.GetMainWindowByFile ("*" + searchText + "*");

        // Find by window title
        IntPtr window_by_title = BFS.Window.GetWindowByText ("*" + searchText + "*");

        // If file name is prioritzed over window title (i.e. "spotify" over "beatles")
        if (prioritize_file_over_title) {
            FocusIfFound (window_by_file, window_by_title);
            // If window title was prioritized over file name ("beatles" over "spotify")
        } else {
            FocusIfFound (window_by_title, window_by_file);
        }

    }
    // Focus first_option if found. Else, focus second_option
    public static void FocusIfFound (IntPtr first_option, IntPtr second_option) {
        if (first_option.ToString () != "0") {
            BFS.Window.Focus (first_option);
        } else {
            BFS.Window.Focus (second_option);
        }
    }
}