Skip to content

Commit

Permalink
Enhancement: new builtin shortcuts: active_office_file
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
1208nn committed Oct 26, 2024
1 parent 009f71a commit fda248a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
43 changes: 42 additions & 1 deletion Flow.Launcher.Infrastructure/Helper.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -86,5 +91,41 @@ public static string Formatted<T>(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);
}
}
3 changes: 2 additions & 1 deletion Flow.Launcher.Infrastructure/UserSettings/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ public SearchPrecisionScore QuerySearchPrecision
public ObservableCollection<BuiltinShortcutModel> 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; }
Expand Down

0 comments on commit fda248a

Please sign in to comment.