Enhancement: new builtin shortcuts: active_office_file

Fixes #2936

Add new builtin shortcut `{active_office_file}` to quickly access the active office file.

* Add a new method `GetActiveOfficeFilePath` in `Flow.Launcher.Infrastructure/Helper.cs` to retrieve the active office file path.
* Add a new builtin shortcut `{active_office_file}` in `Flow.Launcher.Infrastructure/UserSettings/Settings.cs` to use the `GetActiveOfficeFilePath` method.
* Update the `BuiltinShortcuts` list in `Flow.Launcher.Infrastructure/UserSettings/Settings.cs` to include the new `{active_office_file}` shortcut.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/Flow-Launcher/Flow.Launcher/issues/2936?shareId=XXXX-XXXX-XXXX-XXXX).
This commit is contained in:
ShH Y 2024-10-26 11:13:35 +08:00
parent 009f71a46e
commit fda248ae24
2 changed files with 44 additions and 2 deletions

View file

@ -1,9 +1,14 @@
#nullable enable #nullable enable
using System; using System;
using System.IO; using System.IO;
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Linq;
using System.Collections.Generic;
using win32api = Microsoft.Win32;
namespace Flow.Launcher.Infrastructure namespace Flow.Launcher.Infrastructure
{ {
@ -86,5 +91,41 @@ namespace Flow.Launcher.Infrastructure
return formatted; return formatted;
} }
public static string GetActiveOfficeFilePath()
{
var pid = GetActiveWindowProcessId();
var handle = win32api.OpenProcess(win32api.PROCESS_QUERY_INFORMATION | win32api.PROCESS_VM_READ, false, pid);
var exePath = win32api.GetModuleFileNameEx(handle, 0);
if (exePath.ToLower().Contains("winword.exe"))
{
return Path.GetFullPath(new win32api.Dispatch("Word.Application").ActiveDocument.FullName);
}
else if (exePath.ToLower().Contains("powerpnt.exe"))
{
return Path.GetFullPath(new win32api.Dispatch("PowerPoint.Application").ActivePresentation.FullName);
}
else if (exePath.ToLower().Contains("excel.exe"))
{
return Path.GetFullPath(new win32api.Dispatch("Excel.Application").ActiveWorkbook.FullName);
}
else
{
return null;
}
}
private static int GetActiveWindowProcessId()
{
var window = GetForegroundWindow();
var threadProcessId = GetWindowThreadProcessId(window, out var processId);
return processId;
}
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError = true)]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
} }
} }

View file

@ -231,7 +231,8 @@ namespace Flow.Launcher.Infrastructure.UserSettings
public ObservableCollection<BuiltinShortcutModel> BuiltinShortcuts { get; set; } = new() public ObservableCollection<BuiltinShortcutModel> BuiltinShortcuts { get; set; } = new()
{ {
new BuiltinShortcutModel("{clipboard}", "shortcut_clipboard_description", Clipboard.GetText), new BuiltinShortcutModel("{clipboard}", "shortcut_clipboard_description", Clipboard.GetText),
new BuiltinShortcutModel("{active_explorer_path}", "shortcut_active_explorer_path", FileExplorerHelper.GetActiveExplorerPath) new BuiltinShortcutModel("{active_explorer_path}", "shortcut_active_explorer_path", FileExplorerHelper.GetActiveExplorerPath),
new BuiltinShortcutModel("{active_office_file}", "shortcut_active_office_file", Helper.GetActiveOfficeFilePath)
}; };
public bool DontPromptUpdateMsg { get; set; } public bool DontPromptUpdateMsg { get; set; }