Processing Ajax...

Title
Close Dialog

Message

Confirm
Close Dialog

Confirm
Close Dialog

Open Selected Files in Notepad++

Description
This script will open the selected files in Notepad++. It supports files on local storage as well as network shares.
Language
C#.net
Minimum Version
Created By
KarolPiechoczek
Contributors
-
Date Created
Sep 2, 2020
Date Last Modified
Sep 10, 2020

Scripted Function (Macro) Code

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Text;
using System.Runtime.InteropServices;
using System.Collections.Specialized;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Diagnostics;

// The 'windowHandle' parameter will contain the window handle for the:
//   - Active window when run by hotkey
//   - Trigger target when run by a Trigger rule
//   - TitleBar Button owner when run by a TitleBar Button
//   - Jump List owner when run from a Taskbar Jump List
//   - Currently focused window if none of these match
public static class DisplayFusionFunction
{
    [DllImport("mpr.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern int WNetGetConnection([MarshalAs(UnmanagedType.LPTStr)] string localName, [MarshalAs(UnmanagedType.LPTStr)] StringBuilder remoteName, ref int length);
    [DllImport("user32.dll")]
    private static extern int GetWindowText(int hWnd, StringBuilder text, int count);
    [DllImport("user32.dll")]
    private static extern int GetWindowTextLength(IntPtr hWnd);
    [DllImport("user32.dll", PreserveSig = true, SetLastError = false, CharSet = CharSet.Unicode)]
    private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, IntPtr lpszWindow);
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
    private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam);
    const int WM_GETTEXT = 13;

    public static void Run(IntPtr windowHandle)
    {
        //save clipboard to restore later
        string oldClip = BFS.Clipboard.GetText();

        //copy the selected file to the clipboard
        BFS.Clipboard.Copy();

        List<string> files = new List<string>();
        foreach (string file in Clipboard.GetFileDropList())
        {
            files.Add(file);
        }
        string originalPath;

        if (files == null || files.Count == 0)
        {
            // Retrieve handle of the Address bar according to class hierarchy
            IntPtr controlHandle;
            controlHandle = FindWindowEx(windowHandle, IntPtr.Zero, "WorkerW", IntPtr.Zero);
            controlHandle = FindWindowEx(controlHandle, IntPtr.Zero, "ReBarWindow32", IntPtr.Zero);
            controlHandle = FindWindowEx(controlHandle, IntPtr.Zero, "Address Band Root", IntPtr.Zero);
            controlHandle = FindWindowEx(controlHandle, IntPtr.Zero, "msctls_progress32", IntPtr.Zero);
            controlHandle = FindWindowEx(controlHandle, IntPtr.Zero, "Breadcrumb Parent", IntPtr.Zero);
            controlHandle = FindWindowEx(controlHandle, IntPtr.Zero, "ToolbarWindow32", IntPtr.Zero);

            StringBuilder builder = new StringBuilder(500);
            SendMessage(controlHandle, WM_GETTEXT, builder.Capacity, builder);
            originalPath = builder.ToString().Remove(0, builder.ToString().IndexOf(' ') + 1);
            //BFS.Dialog.ShowTrayMessage("DIR originalPath=" + originalPath);
        }
        else
        {
            BFS.Clipboard.SetText(oldClip);
            string uncPath;

            foreach (var item in files)
            {
                originalPath = item;
                if (!Regex.IsMatch(originalPath, "^[a-zA-Z]:"))
                {
                    uncPath = originalPath;
                }
                else
                {
                    //set up variables for the WNetGetConnection function
                    StringBuilder sb = new StringBuilder(512);
                    int size = sb.Capacity;

                    //try and get the full path with the WNetGetConnection. if it fails, return what was on the clipboard
                    int hr = WNetGetConnection(originalPath.Substring(0, 2), sb, ref size);
                    if (hr != 0)
                    {
                        uncPath = originalPath;
                    }
                    else
                    {
                        //build full path
                        DirectoryInfo dir = new DirectoryInfo(originalPath);
                        string path = Path.GetFullPath(originalPath).Substring(Path.GetPathRoot(originalPath).Length);

                        //set the clipboard with the full path
                        uncPath = Path.Combine(sb.ToString().TrimEnd(), path);
                    }
                }

                bool ok = BFS.ScriptSettings.WriteValue("UNCPATH", uncPath);
                //BFS.Dialog.ShowTrayMessage("WRITE" + uncPath);
                ok = BFS.ScriptSettings.WriteValueBool("FLAG_UNCPATH_UPDATED", true);
                Process.Start(@"C:\Program Files\Notepad++\notepad++.exe", "\"" + uncPath + "\"");
            }
        }
    }
}