window自动化添加资源管理器相关操作

This commit is contained in:
fofolee 2025-02-28 17:21:10 +08:00
parent 70a2fb68bb
commit 2ae602d416
5 changed files with 302 additions and 0 deletions

View File

@ -0,0 +1,161 @@
using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
public class ExplorerManager
{
public static void Main(string[] args)
{
if (args.Length == 0 || args[0] == "-h" || args[0] == "--help")
{
ShowHelp();
return;
}
try
{
string type = GetArgumentValue(args, "-type");
if (string.IsNullOrEmpty(type))
{
throw new Exception("必须指定操作类型 (-type)");
}
switch (type.ToLower())
{
case "list":
ListExplorerWindows();
break;
case "navigate":
string handle = GetArgumentValue(args, "-handle");
string path = GetArgumentValue(args, "-path");
if (string.IsNullOrEmpty(handle) || string.IsNullOrEmpty(path))
{
throw new Exception("必须指定窗口句柄和目标路径");
}
NavigateToPath(long.Parse(handle), path);
break;
default:
throw new Exception(string.Format("不支持的操作类型: {0}", type));
}
}
catch (Exception ex)
{
Console.Error.WriteLine(string.Format("Error: {0}", ex.Message));
}
}
private static void ListExplorerWindows()
{
var explorerWindows = new List<Dictionary<string, object>>();
try
{
dynamic shellApp = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));
dynamic windows = shellApp.Windows();
foreach (dynamic window in windows)
{
try
{
string locationUrl = window.LocationURL;
if (string.IsNullOrEmpty(locationUrl)) continue;
string path = new Uri(locationUrl).LocalPath;
explorerWindows.Add(new Dictionary<string, object>
{
{ "handle", window.HWND },
{ "title", window.LocationName },
{ "path", path },
{ "class", "CabinetWClass" }
});
}
catch
{
// 忽略获取信息失败的窗口
}
}
}
catch (Exception ex)
{
Console.Error.WriteLine(string.Format("Error: {0}", ex.Message));
return;
}
var serializer = new JavaScriptSerializer();
Console.WriteLine(serializer.Serialize(explorerWindows));
}
private static void NavigateToPath(long handle, string path)
{
try
{
dynamic shellApp = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));
dynamic windows = shellApp.Windows();
foreach (dynamic window in windows)
{
try
{
if (window.HWND == handle)
{
window.Navigate(path);
Console.WriteLine("true");
return;
}
}
catch
{
// 忽略单个窗口的错误
}
}
throw new Exception("未找到指定的窗口");
}
catch (Exception ex)
{
throw new Exception(string.Format("导航失败: {0}", ex.Message));
}
}
private static string GetArgumentValue(string[] args, string key)
{
int index = Array.IndexOf(args, key);
if (index >= 0 && index < args.Length - 1)
{
return args[index + 1];
}
return null;
}
private static void ShowHelp()
{
Console.WriteLine(@"
Windows 使
==========================
:
explorer.exe -type <> [...]
:
--------
1. list -
2. navigate -
:
-handle
-path
:
------
list: JSON格式的窗口信息数组
navigate: true表示成功
使:
--------
1.
explorer.exe -type list
2.
explorer.exe -type navigate -handle 12345 -path ""C:\Windows""
");
}
}

View File

@ -0,0 +1,49 @@
const { runCsharpFeature } = require("../../csharp");
/**
* 执行资源管理器操作
* @param {string} type - 操作类型, 可选值: "list"|"navigate"
* @param {Object} params - 参数对象
* @returns {Promise<Object[]|boolean>} - 操作结果
*/
async function runExplorer(type, params = {}) {
const args = ["-type", type];
// 添加导航相关参数
if (type === "navigate") {
if (!params.handle || !params.path) {
throw new Error("导航操作需要指定窗口句柄和目标路径");
}
args.push("-handle", params.handle.toString());
args.push("-path", params.path);
}
try {
const result = await runCsharpFeature("explorer", args);
if (result) {
if (result.trim() === "true") return true;
return JSON.parse(result);
}
} catch (err) {
console.error(err);
return type === "list" ? [] : false;
}
return type === "list" ? [] : false;
}
module.exports = {
/**
* 获取所有打开的资源管理器窗口信息
* @returns {Promise<Array>} 资源管理器窗口信息数组
*/
list: () => runExplorer("list"),
/**
* 导航到指定路径
* @param {number} handle - 窗口句柄
* @param {string} path - 目标路径
* @returns {Promise<boolean>} 是否成功
*/
navigate: (handle, path) => runExplorer("navigate", { handle, path }),
};

View File

@ -8,6 +8,7 @@ const software = require("./software");
const utils = require("./utils");
const automation = require("./automation");
const browser = require("./browser");
const explorer = require("./explorer");
module.exports = {
window,
@ -20,4 +21,5 @@ module.exports = {
utils,
automation,
browser,
explorer,
};

View File

@ -472,6 +472,70 @@ export const windowsCommands = {
],
asyncMode: "await",
},
// 资源管理器
{
value: "quickcomposer.windows.explorer.list",
label: "资源管理器操作",
icon: "folder",
asyncMode: "await",
subCommands: [
{
value: "quickcomposer.windows.explorer.list",
label: "获取所有已打开路径",
icon: "folder",
outputs: {
label: "已打开路径列表",
suggestName: "explorerList",
structure: [
{
handle: { label: "窗口句柄", suggestName: "windowHandle" },
title: { label: "窗口标题", suggestName: "windowTitle" },
path: { label: "当前路径", suggestName: "windowPath" },
class: { label: "窗口类名", suggestName: "windowClass" },
},
],
},
},
{
value: "quickcomposer.windows.explorer.navigate",
label: "导航到指定路径",
icon: "folder",
config: [
{
component: "VariableInput",
label: "窗口句柄",
icon: "window",
width: 12,
placeholder: "输入要导航的窗口句柄",
defaultValue: newVarInputVal("var"),
disableToggleType: true,
width: 4,
},
{
component: "VariableInput",
label: "路径",
icon: "folder",
options: {
dialog: {
type: "open",
options: {
title: "选择路径",
properties: ["openDirectory"],
},
},
},
width: 8,
placeholder: "输入要导航的路径",
},
],
outputs: {
label: "是否成功",
suggestName: "isSuccess",
typeName: "布尔值",
},
},
],
},
// automation
{
value: "quickcomposer.windows.automation.click",

View File

@ -1721,6 +1721,32 @@ interface quickcomposerApi {
): Promise<{ success: boolean; error?: string }>;
};
/**
*
*/
explorer: {
/**
*
* @returns
*/
list(): Promise<
{
handle: number; // 窗口句柄
title: string; // 窗口标题
path: string; // 当前路径
class: string; // 窗口类名
}[]
>;
/**
*
* @param handle
* @param path
* @returns
*/
navigate(handle: number, path: string): Promise<boolean>;
};
/**
*
*/