diff --git a/Flow.Launcher.Infrastructure/Helper.cs b/Flow.Launcher.Infrastructure/Helper.cs index 864d796c7bb..c1c097fd5c8 100644 --- a/Flow.Launcher.Infrastructure/Helper.cs +++ b/Flow.Launcher.Infrastructure/Helper.cs @@ -1,9 +1,14 @@ -#nullable enable +#nullable enable using System; using System.IO; using System.Text.Json; 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 { @@ -86,5 +91,41 @@ public static string Formatted(this T t) 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); } } diff --git a/Flow.Launcher.Infrastructure/UserSettings/Settings.cs b/Flow.Launcher.Infrastructure/UserSettings/Settings.cs index 0c7de10fd78..f081e91878f 100644 --- a/Flow.Launcher.Infrastructure/UserSettings/Settings.cs +++ b/Flow.Launcher.Infrastructure/UserSettings/Settings.cs @@ -231,7 +231,8 @@ public SearchPrecisionScore QuerySearchPrecision public ObservableCollection BuiltinShortcuts { get; set; } = new() { 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; }