Skip to content

Commit fda248a

Browse files
committed
Enhancement: new builtin shortcuts: active_office_file
Fixes Flow-Launcher#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).
1 parent 009f71a commit fda248a

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

Flow.Launcher.Infrastructure/Helper.cs

+42-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
#nullable enable
1+
#nullable enable
22

33
using System;
44
using System.IO;
55
using System.Text.Json;
66
using System.Text.Json.Serialization;
7+
using System.Diagnostics;
8+
using System.Runtime.InteropServices;
9+
using System.Linq;
10+
using System.Collections.Generic;
11+
using win32api = Microsoft.Win32;
712

813
namespace Flow.Launcher.Infrastructure
914
{
@@ -86,5 +91,41 @@ public static string Formatted<T>(this T t)
8691

8792
return formatted;
8893
}
94+
95+
public static string GetActiveOfficeFilePath()
96+
{
97+
var pid = GetActiveWindowProcessId();
98+
var handle = win32api.OpenProcess(win32api.PROCESS_QUERY_INFORMATION | win32api.PROCESS_VM_READ, false, pid);
99+
var exePath = win32api.GetModuleFileNameEx(handle, 0);
100+
if (exePath.ToLower().Contains("winword.exe"))
101+
{
102+
return Path.GetFullPath(new win32api.Dispatch("Word.Application").ActiveDocument.FullName);
103+
}
104+
else if (exePath.ToLower().Contains("powerpnt.exe"))
105+
{
106+
return Path.GetFullPath(new win32api.Dispatch("PowerPoint.Application").ActivePresentation.FullName);
107+
}
108+
else if (exePath.ToLower().Contains("excel.exe"))
109+
{
110+
return Path.GetFullPath(new win32api.Dispatch("Excel.Application").ActiveWorkbook.FullName);
111+
}
112+
else
113+
{
114+
return null;
115+
}
116+
}
117+
118+
private static int GetActiveWindowProcessId()
119+
{
120+
var window = GetForegroundWindow();
121+
var threadProcessId = GetWindowThreadProcessId(window, out var processId);
122+
return processId;
123+
}
124+
125+
[DllImport("user32.dll")]
126+
private static extern IntPtr GetForegroundWindow();
127+
128+
[DllImport("user32.dll", SetLastError = true)]
129+
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
89130
}
90131
}

Flow.Launcher.Infrastructure/UserSettings/Settings.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ public SearchPrecisionScore QuerySearchPrecision
231231
public ObservableCollection<BuiltinShortcutModel> BuiltinShortcuts { get; set; } = new()
232232
{
233233
new BuiltinShortcutModel("{clipboard}", "shortcut_clipboard_description", Clipboard.GetText),
234-
new BuiltinShortcutModel("{active_explorer_path}", "shortcut_active_explorer_path", FileExplorerHelper.GetActiveExplorerPath)
234+
new BuiltinShortcutModel("{active_explorer_path}", "shortcut_active_explorer_path", FileExplorerHelper.GetActiveExplorerPath),
235+
new BuiltinShortcutModel("{active_office_file}", "shortcut_active_office_file", Helper.GetActiveOfficeFilePath)
235236
};
236237

237238
public bool DontPromptUpdateMsg { get; set; }

0 commit comments

Comments
 (0)