Skip to content

Commit

Permalink
add GetWindowNames, CheckWindowNameExists command
Browse files Browse the repository at this point in the history
  • Loading branch information
rcktrncn committed Jul 24, 2021
1 parent b2fe0d3 commit c400501
Show file tree
Hide file tree
Showing 5 changed files with 340 additions and 0 deletions.
160 changes: 160 additions & 0 deletions taskt/Core/Automation/Commands/CheckWindowNameExistsCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml.Serialization;
using taskt.Core.Automation.User32;
using taskt.UI.Forms;
using taskt.UI.CustomControls;
namespace taskt.Core.Automation.Commands
{
[Serializable]
[Attributes.ClassAttributes.Group("Window Commands")]
[Attributes.ClassAttributes.Description("This command returns a existence of window name.")]
[Attributes.ClassAttributes.UsesDescription("Use this command when you want to active a window by name or bring it to attention.")]
[Attributes.ClassAttributes.ImplementationDescription("This command implements 'FindWindowNative', 'SetForegroundWindow', 'ShowWindow' from user32.dll to achieve automation.")]
public class CheckWindowNameExistsCommand : ScriptCommand
{
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please enter or select the window that you want to check existence. (ex. Notepad, %kwd_current_window%, {{{vWindow}}})")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
[Attributes.PropertyAttributes.InputSpecification("Input or Type the name of the window that you want to check existence.")]
[Attributes.PropertyAttributes.SampleUsage("**Untitled - Notepad** or **%kwd_current_window%** or **{{{vWindow}}}**")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_WindowName { get; set; }

[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Optional - Window title search method (Default is Contains)")]
[Attributes.PropertyAttributes.InputSpecification("")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Contains")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Start with")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("End with")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Exact match")]
[Attributes.PropertyAttributes.SampleUsage("**Contains** or **Start with** or **End with** or **Exact match**")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_SearchMethod { get; set; }

[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Specify the variable to assign the result")]
[Attributes.PropertyAttributes.InputSpecification("")]
[Attributes.PropertyAttributes.SampleUsage("**vSomeVariable**")]
[Attributes.PropertyAttributes.Remarks("If you have enabled the setting **Create Missing Variables at Runtime** then you are not required to pre-define your variables, however, it is highly recommended.")]
public string v_UserVariableName { get; set; }

[XmlIgnore]
[NonSerialized]
public ComboBox WindowNameControl;

public CheckWindowNameExistsCommand()
{
this.CommandName = "CheckWindowNameExistsCommand";
this.SelectionName = "Check Window Name Exists";
this.CommandEnabled = true;
this.CustomRendering = true;
}
public override void RunCommand(object sender)
{
string windowName = v_WindowName.ConvertToUserVariable(sender);
string searchMethod = v_SearchMethod.ConvertToUserVariable(sender);
if (String.IsNullOrEmpty(searchMethod))
{
searchMethod = "Contains";
}

bool targetIsCurrentWindow = windowName == ((Automation.Engine.AutomationEngineInstance)sender).engineSettings.CurrentWindowKeyword;
var targetWindows = User32Functions.FindTargetWindows(windowName, targetIsCurrentWindow, (searchMethod != "Contains"));

bool existResult = false;

//loop each window
if (searchMethod == "Contains" || targetIsCurrentWindow)
{
if (targetWindows.Count > 0)
{
existResult = true;
}
}
else
{
Func<string, bool> searchFunc;
switch(searchMethod)
{
case "Start with":
searchFunc = (s) => s.StartsWith(windowName);
break;

case "End with":
searchFunc = (s) => s.EndsWith(windowName);
break;

case "Exact match":
searchFunc = (s) => (s == windowName);
break;

default:
throw new Exception("Search method " + searchMethod + " is not support.");
break;
}

foreach (var targetedWindow in targetWindows)
{
if (searchFunc(User32Functions.GetWindowTitle(targetedWindow)))
{
existResult = true;
break;
}
}
}

(existResult ? "TRUE" : "FALSE").StoreInUserVariable(sender, v_UserVariableName);
}

public override List<Control> Render(frmCommandEditor editor)
{
base.Render(editor);

//create window name helper control
RenderedControls.Add(UI.CustomControls.CommandControls.CreateDefaultLabelFor("v_WindowName", this));
WindowNameControl = UI.CustomControls.CommandControls.CreateStandardComboboxFor("v_WindowName", this).AddWindowNames(editor);
RenderedControls.AddRange(UI.CustomControls.CommandControls.CreateUIHelpersFor("v_WindowName", this, new Control[] { WindowNameControl }, editor));
RenderedControls.Add(WindowNameControl);

RenderedControls.AddRange(UI.CustomControls.CommandControls.CreateDefaultDropdownGroupFor("v_SearchMethod", this, editor));

RenderedControls.Add(CommandControls.CreateDefaultLabelFor("v_UserVariableName", this));
var VariableNameControl = CommandControls.CreateStandardComboboxFor("v_UserVariableName", this).AddVariableNames(editor);
RenderedControls.AddRange(CommandControls.CreateUIHelpersFor("v_UserVariableName", this, new Control[] { VariableNameControl }, editor));
RenderedControls.Add(VariableNameControl);

return RenderedControls;

}
public override void Refresh(frmCommandEditor editor)
{
base.Refresh();
WindowNameControl.AddWindowNames();
}

public override string GetDisplayValue()
{
return base.GetDisplayValue() + " [Target Window: " + v_WindowName + "]";
}

public override bool IsValidate(frmCommandEditor editor)
{
base.IsValidate(editor);

if (String.IsNullOrEmpty(this.v_WindowName))
{
this.validationResult += "Window is empty.\n";
this.IsValid = false;
}
if (String.IsNullOrEmpty(this.v_UserVariableName))
{
this.validationResult += "Variable is empty.\n";
this.IsValid = false;
}

return this.IsValid;
}
}
}
174 changes: 174 additions & 0 deletions taskt/Core/Automation/Commands/GetWindowNamesCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml.Serialization;
using taskt.Core.Automation.User32;
using taskt.UI.Forms;
using taskt.UI.CustomControls;
using System.Linq;

namespace taskt.Core.Automation.Commands
{
[Serializable]
[Attributes.ClassAttributes.Group("Window Commands")]
[Attributes.ClassAttributes.Description("This command returns window names.")]
[Attributes.ClassAttributes.UsesDescription("Use this command when you want window names.")]
[Attributes.ClassAttributes.ImplementationDescription("This command implements 'FindWindowNative', 'SetForegroundWindow', 'ShowWindow' from user32.dll to achieve automation.")]
public class GetWindowNamesCommand : ScriptCommand
{
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please enter or select the window name that you want to. (ex. Notepad, %kwd_current_window%, {{{vWindow}}})")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
[Attributes.PropertyAttributes.InputSpecification("Input or Type the name of the window name that you want to.")]
[Attributes.PropertyAttributes.SampleUsage("**Untitled - Notepad** or **%kwd_current_window%** or **{{{vWindow}}}**")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_WindowName { get; set; }

[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Optional - Window title search method (Default is Contains)")]
[Attributes.PropertyAttributes.InputSpecification("")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Contains")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Start with")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("End with")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Exact match")]
[Attributes.PropertyAttributes.SampleUsage("**Contains** or **Start with** or **End with** or **Exact match**")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_SearchMethod { get; set; }

[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Specify the variable to assign the window names list")]
[Attributes.PropertyAttributes.InputSpecification("")]
[Attributes.PropertyAttributes.SampleUsage("**vSomeVariable**")]
[Attributes.PropertyAttributes.Remarks("If you have enabled the setting **Create Missing Variables at Runtime** then you are not required to pre-define your variables, however, it is highly recommended.")]
public string v_UserVariableName { get; set; }

[XmlIgnore]
[NonSerialized]
public ComboBox WindowNameControl;

public GetWindowNamesCommand()
{
this.CommandName = "GetWindowNamesCommand";
this.SelectionName = "Get Window Names";
this.CommandEnabled = true;
this.CustomRendering = true;
}
public override void RunCommand(object sender)
{
var engine = (Automation.Engine.AutomationEngineInstance)sender;

string windowName = v_WindowName.ConvertToUserVariable(sender);
string searchMethod = v_SearchMethod.ConvertToUserVariable(sender);
if (String.IsNullOrEmpty(searchMethod))
{
searchMethod = "Contains";
}

bool targetIsCurrentWindow = windowName == engine.engineSettings.CurrentWindowKeyword;
var targetWindows = User32Functions.FindTargetWindows(windowName, targetIsCurrentWindow, true);

List<string> windowNamesList = new List<string>();

//loop each window
if (searchMethod == "Contains" || targetIsCurrentWindow)
{
foreach (var targetedWindow in targetWindows)
{
windowNamesList.Add(User32Functions.GetWindowTitle(targetedWindow));
}
}
else
{
Func<string, bool> searchFunc;
switch(searchMethod)
{
case "Start with":
searchFunc = (s) => s.StartsWith(windowName);
break;

case "End with":
searchFunc = (s) => s.EndsWith(windowName);
break;

case "Exact match":
searchFunc = (s) => (s == windowName);
break;

default:
throw new Exception("Search method " + searchMethod + " is not support.");
break;
}

foreach (var targetedWindow in targetWindows)
{
if (searchFunc(User32Functions.GetWindowTitle(targetedWindow)))
{
windowNamesList.Add(User32Functions.GetWindowTitle(targetedWindow));
}
}
}

Script.ScriptVariable newFilesList = new Script.ScriptVariable
{
VariableName = v_UserVariableName,
VariableValue = windowNamesList
};
//Overwrites variable if it already exists
if (engine.VariableList.Exists(x => x.VariableName == newFilesList.VariableName))
{
Script.ScriptVariable temp = engine.VariableList.Where(x => x.VariableName == newFilesList.VariableName).FirstOrDefault();
engine.VariableList.Remove(temp);
}
engine.VariableList.Add(newFilesList);
}

public override List<Control> Render(frmCommandEditor editor)
{
base.Render(editor);

//create window name helper control
RenderedControls.Add(UI.CustomControls.CommandControls.CreateDefaultLabelFor("v_WindowName", this));
WindowNameControl = UI.CustomControls.CommandControls.CreateStandardComboboxFor("v_WindowName", this).AddWindowNames(editor);
RenderedControls.AddRange(UI.CustomControls.CommandControls.CreateUIHelpersFor("v_WindowName", this, new Control[] { WindowNameControl }, editor));
RenderedControls.Add(WindowNameControl);

RenderedControls.AddRange(UI.CustomControls.CommandControls.CreateDefaultDropdownGroupFor("v_SearchMethod", this, editor));

RenderedControls.Add(CommandControls.CreateDefaultLabelFor("v_UserVariableName", this));
var VariableNameControl = CommandControls.CreateStandardComboboxFor("v_UserVariableName", this).AddVariableNames(editor);
RenderedControls.AddRange(CommandControls.CreateUIHelpersFor("v_UserVariableName", this, new Control[] { VariableNameControl }, editor));
RenderedControls.Add(VariableNameControl);

return RenderedControls;

}
public override void Refresh(frmCommandEditor editor)
{
base.Refresh();
WindowNameControl.AddWindowNames();
}

public override string GetDisplayValue()
{
return base.GetDisplayValue() + " [Target Window: " + v_WindowName + "]";
}

public override bool IsValidate(frmCommandEditor editor)
{
base.IsValidate(editor);

if (String.IsNullOrEmpty(this.v_WindowName))
{
this.validationResult += "Window is empty.\n";
this.IsValid = false;
}
if (String.IsNullOrEmpty(this.v_UserVariableName))
{
this.validationResult += "Variable is empty.\n";
this.IsValid = false;
}

return this.IsValid;
}
}
}
2 changes: 2 additions & 0 deletions taskt/Core/Automation/Commands/ScriptCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ namespace taskt.Core.Automation.Commands

// Window
[XmlInclude(typeof(ActivateWindowCommand))]
[XmlInclude(typeof(CheckWindowNameExistsCommand))]
[XmlInclude(typeof(CloseWindowCommand))]
[XmlInclude(typeof(GetWindowNamesCommand))]
[XmlInclude(typeof(MoveWindowCommand))]
[XmlInclude(typeof(ResizeWindowCommand))]
[XmlInclude(typeof(SetWindowStateCommand))]
Expand Down
2 changes: 2 additions & 0 deletions taskt/UI/CustomControls/CustomControls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,9 @@ public static class Images

// Window
{"ActivateWindowCommand", "taskt.Properties.Resources.command_window"},
{"CheckWindowNameExistsCommand", "taskt.Properties.Resources.command_window"},
{"CloseWindowCommand", "taskt.Properties.Resources.command_window_close"},
{"GetWindowNamesCommand", "taskt.Properties.Resources.command_window"},
{"MoveWindowCommand", "taskt.Properties.Resources.command_window"},
{"ResizeWindowCommand", "taskt.Properties.Resources.command_window"},
{"SetWindowStateCommand", "taskt.Properties.Resources.command_window"},
Expand Down
2 changes: 2 additions & 0 deletions taskt/taskt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@
<ItemGroup>
<Compile Include="Core\ApplicationUpdate.cs" />
<Compile Include="Core\ApplicationSettings.cs" />
<Compile Include="Core\Automation\Commands\GetWindowNamesCommand.cs" />
<Compile Include="Core\Automation\Commands\CheckWindowNameExistsCommand.cs" />
<Compile Include="Core\Automation\Commands\AddDictionaryCommand.cs" />
<Compile Include="Core\Automation\Commands\BeginLoopCommand.cs" />
<Compile Include="Core\Automation\Commands\BeginMultiLoopCommand.cs" />
Expand Down

0 comments on commit c400501

Please sign in to comment.