forked from saucepleez/taskt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add GetWindowNames, CheckWindowNameExists command
- Loading branch information
Showing
5 changed files
with
340 additions
and
0 deletions.
There are no files selected for viewing
160 changes: 160 additions & 0 deletions
160
taskt/Core/Automation/Commands/CheckWindowNameExistsCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
174
taskt/Core/Automation/Commands/GetWindowNamesCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters