Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: new builtin shortcuts: active_office_file #3046

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -253,7 +253,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
Loading