Processing Ajax...

Title
Close Dialog

Message

Confirm
Close Dialog

Confirm
Close Dialog

Confirm
Close Dialog

Copy Text and Open/Focus Notepad and Paste

Description
This macro will copy the selected text, open Notepad if it's not open, then paste the text. If there's more than one Notepad window already open, it will prompt you to select the window.
Language
C#.net
Minimum Version
Created By
Keith Lammers (BFS)
Contributors
-
Date Created
May 19, 2021
Date Last Modified
May 26, 2021

Macro Code

using System;
using System.Collections.Generic;

// The 'text' parameter will contain the text from the:
//   - Current Clipboard when run by HotKey
//   - History Item when run from the History Menu
// The returned string will be:
//   - Placed directly on the Clipboard when run as a Macro
//   - Ignored by ClipboardFusion if it is 'null'
//   - Passed along to the next action in a Trigger (null changed to an empty string)
public static class ClipboardFusionHelper
{
	public static string ProcessText(string text)
	{
		// Copy the selected text
		text = BFS.Clipboard.CopyText();
		
		// Check if Notepad is running
		bool isRunning = BFS.Application.IsAppRunningByFile("*Notepad.exe");
		
		// If it's running, focus it, if not, launch it
		if (isRunning)
		{
            // Get a list of all open Notepad windows
            var allNotepadWindowsList = new List<String>();
            foreach (IntPtr window in BFS.Window.GetVisibleAndMinimizedWindowHandles())
            {
               string windowText = BFS.Window.GetText(window);
               if (windowText.EndsWith("Notepad"))
               {
                    allNotepadWindowsList.Add(windowText);
               }
            }
            
            // If there's more than one, prompt to select which one
            if (allNotepadWindowsList.Count > 1)
            {
                allNotepadWindowsList.Add("-- New Notepad Window --");
                string[] allNotepadWindowsArray = allNotepadWindowsList.ToArray();
                string selectedWindowTitle = BFS.Dialog.GetUserInputListViewWithFilter("Select the Notepad window", allNotepadWindowsArray);
                if (selectedWindowTitle == "-- New Notepad Window --")
                {
                    BFS.Application.Start("notepad.exe", "");
                    BFS.General.ThreadWait(1000);
                }
                else
                {
                    IntPtr selectedWindow = BFS.Window.GetWindowByTextExact(selectedWindowTitle);
                    BFS.Window.Focus(selectedWindow);
                }
            }
            // Otherwise, just focus the only one that's open
            else
            {
                IntPtr selectedWindow = BFS.Window.GetWindowByTextExact(allNotepadWindowsList[0]);
                BFS.Window.Focus(selectedWindow);
            }
		}
		// Otherwise, launch it
		else
		{
            uint appID = BFS.Application.Start("notepad.exe", "");
            IntPtr newWindow = BFS.Application.GetMainWindowByAppID(appID);
		}
		
		// Paste the text
		BFS.Input.SendKeys("^({VK_35})");
		BFS.Clipboard.PasteText(Environment.NewLine + text + Environment.NewLine);
		
		return text;
	}
}