[AHK] dbl-click .xy-find-files to load a saved search

Discuss and share scripts and script files...
Post Reply
autocart
Posts: 1384
Joined: 26 Sep 2013 15:22

[AHK] dbl-click .xy-find-files to load a saved search

Post by autocart »

[EDITED Dec 4th, 2013: added a download option]

Hi,

USAGE COMPLETELY AT ANYBODY'S OWN RISK!!

In order to --> dbl-click on a .xy-find file so that it will load a saved ini-search <-- with the same name in XYplorer, do the following:

Download and save the xy-find.zip (see below) and extract the xy-find.exe to a PATH.
In Tools/List Management/Portable File Associations... connect .xy-find files to xy-find.exe like this:

Code: Select all

xy-find>"PATH...\xy-find.exe"
Create an empty file with the extension ".xy-find" and the same file-name as an ini-saved search file in the <xydata>\FindTemplates folder.
Now, if you dbl-click on the .xy-find file it will load the saved ini-search in XYplorer.

In order to save a search and automatically create the corresponding .xy-find file, you can make a user button and edit the "on left click:" field like this:

Code: Select all

run "PATH...\xy-find.exe" savesearch "PATH2"
PATH2 is optional and states the folder where any automatically created .xy-find files should be saved. If it is not stated or is not valid then they will be saved to the <xydata>\FindTemplates folder.
In the downloadable zip I also packed a simple icon for use with the button.

Here is the ahk-code for the xy-find.exe:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

global XYRETURN

SetTitleMatchMode, 2
IfWinNotExist XYplorer ahk_class ThunderRT6FormDC
  ExitApp
if %0% = 0
  ExitApp
cmdp = %1%
if (cmdp = "savesearch")
{
  savetopath = %2%
  savetopath := RTrim(savetopath, "\""")
  ;~ MsgBox %2%
  if (not (InStr(FileExist(savetopath), "D")))
    savetopath := GetVarFromXYplorer("<xydata>") . "\FindTemplates"
  SaveSearch(savetopath)
  ;~ MsgBox % savetopath
}
SplitPath, cmdp, , , fileextension, filenameNOextension
if (FileExist(cmdp) && fileextension = "xy-find")
  if (FileExist(GetVarFromXYplorer("<xydata>") . "\FindTemplates\" . filenameNOextension . ".ini"))
  LoadSearch(filenameNOextension)
ExitApp

LoadSearch(searchtemplateFilename)
{
  searchtemplatename := ""
  hex := ""
  delim := ""
  Loop, Parse, searchtemplateFilename, "`%"
  {
	if (delim = "`%" and StrLen(A_LoopField) > 1)
    {
      hex := "0x" . SubStr(A_LoopField, 1, 2)
      if hex is xdigit
        hex := Chr(hex) . Substr(A_LoopField, 3)
      else
        hex := delim . A_LoopField
    }
    else
      hex := delim . A_LoopField
    searchtemplatename := searchtemplatename . hex
    delim := "`%"
  }
  MessagetoXYplorer := "::loadsearch " . searchtemplatename . ", rle"
  FunctionMessagetoXYplorer(MessagetoXYplorer)
}

SaveSearch(savetopath)
{
  xyFindTemplatesPath := GetVarFromXYplorer("<xydata>") . "\FindTemplates"
  fileList =
  Loop % xyFindTemplatesPath . "\*"
    fileList = %fileList%%A_LoopFileName%`n
  WinActivate, ahk_class ThunderRT6FormDC
  WinMenuSelectItem, ahk_class ThunderRT6FormDC, , Edit, Search Templates...
  WinWaitActive Search Templates ahk_class ThunderRT6FormDC
  SetControlDelay -1
  Control, Check, , ThunderRT6CheckBox2, Search Templates ahk_class ThunderRT6FormDC
  ;~ BlockInput On
  ControlClick, ThunderRT6CommandButton3, Search Templates ahk_class ThunderRT6FormDC
  ;~ BlockInput Off
  WinWait, Save Settings to Search Template ahk_class ThunderRT6FormDC
  WinGetTitle, title
  WinSetTitle % title . " - XY-FIND"
  WinWaitClose
  ;~ BlockInput On
  ControlClick, ThunderRT6CommandButton4, Search Templates ahk_class ThunderRT6FormDC
  ;~ BlockInput Off
  WinWaitClose, Search Templates ahk_class ThunderRT6FormDC
  
  
  newFile =
  Loop % xyFindTemplatesPath . "\*"
    if (not InStr(fileList, A_LoopFileName))
    {
      newFile := A_LoopFileName
      break
    }
  if newFile
  {
    StringTrimRight, newFile, newFile, 4 ;removes ".ini"
    file := FileOpen(savetopath . "\" . newFile . ".xy-find", "w")
    if file
    {
      file.Close()
      return savetopath . "\" . newFile . ".xy-find"
    }
  }
}

;***************************************************************************************
;** code from others (or based on) **
;***************************************************************************************

;heavily based on
;http://www.xyplorer.com/xyfc/viewtopic.php?f=7&t=9233&p=89555&hilit=messenger#p89555

    FunctionMessagetoXYplorer(MessagetoXYplorer)          ;Send message to XYplorer
    {
    SetTitleMatchMode, 2
    HWND := WinExist("XYplorer ahk_class ThunderRT6FormDC")
    Size := StrLen(MessagetoXYplorer)
      If !(A_IsUnicode)
      {
      VarSetCapacity(Data, Size * 2, 0)
      StrPut(MessagetoXYplorer, &Data, Size, "UTF-16")
      }
      Else
      Data := MessagetoXYplorer

    VarSetCapacity(COPYDATA, A_PtrSize * 3, 0)
    NumPut(4194305, COPYDATA, 0, "Ptr")
    NumPut(Size * 2, COPYDATA, A_PtrSize, "UInt")
    NumPut(&Data, COPYDATA, A_PtrSize * 2, "Ptr")
    SendMessage, 0x4a, 0, &COPYDATA, , ahk_id %HWND%
    }

    GetVarFromXYplorer(varName)          ;Send message to XYplorer
    {
      global XYRETURN
      SenderHWND := A_ScriptHwnd + 0  ;Return this script's hidden hwdn id.  +0 to convert from Hex to Dec
      MessagetoXYplorer := "::CopyData " SenderHWND ", """ varName """, 2"    ;resolved to sth like this:   ::CopyData 7409230, "<curitem>", 2
      SetTitleMatchMode, 2
      HWND := WinExist("XYplorer ahk_class ThunderRT6FormDC")
      Size := StrLen(MessagetoXYplorer)
      If !(A_IsUnicode)
      {
      VarSetCapacity(Data, Size * 2, 0)
      StrPut(MessagetoXYplorer, &Data, Size, "UTF-16")
      }
      Else
      Data := MessagetoXYplorer

      VarSetCapacity(COPYDATA, A_PtrSize * 3, 0)
      NumPut(4194305, COPYDATA, 0, "Ptr")
      NumPut(Size * 2, COPYDATA, A_PtrSize, "UInt")
      NumPut(&Data, COPYDATA, A_PtrSize * 2, "Ptr")
      OnMessage(0x4a, "Function_Receive_WM_COPYDATA")  ; 0x4a is WM_COPYDATA. This onhold and wait for the WM_Copydata from XYplorer then execute Function_Receive_WM_COPYDATA(wParam, lParam) below
      SendMessage, 0x4a, 0, &COPYDATA, , ahk_id %HWND% ;SendMessage waits for the target window to process the message, up until the timeout period expires.
      OnMessage(0x4a, "")
      return XYRETURN
    }

    Function_Receive_WM_COPYDATA(wParam, lParam)
    {
      global XYRETURN
      StringAddress := NumGet(lParam + 2*A_PtrSize) ;lParam+8 is the address of CopyDataStruct's lpData member.
      CopyOfData := StrGet(StringAddress)    ;May also specify CP0 (default) or UTF-8 or UTF-16:   StrGet(StringAddress, NumGet(lParam+A_PtrSize), "UTF-16")
      cbData := NumGet(lParam+A_PtrSize)/2  ;cbData/2 = String length
      StringLeft, Datareceived, CopyOfData, cbData
      XYRETURN := Datareceived
    }
Downloads:

The exe "xy-find_load_results.exe" will load any search results that were saved with the search, instead of actually running the search, but it can be rerun anew manually.
(The only line in code that was changed, is:
MessagetoXYplorer := "::loadsearch " . searchtemplatename . ", rle"
It was changed to:
MessagetoXYplorer := "::loadsearch " . searchtemplatename . ", rlec")

The exe named only "xy-find.exe" will load and run the saved search with its settings anew right away and not load any saved results.
To see the attached files, you need to log into the forum.
Last edited by autocart on 04 Dec 2013 08:21, edited 2 times in total.

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: [AHK] dbl-click .xy-find-files to load a saved search

Post by TheQwerty »

Nice work!

This is especially useful for anyone wanting to set the Windows Explorer file association as well so that opening an xy-find file anywhere on your system would load the search in XY.


I hope you don't mind but I have a couple alternative suggestions for anyone only wanting it to work in XYplorer and hoping to avoid the AHK executable...

1. PFA/POM allow you to use paths in your pattern meaning you could configure XY such that opening an INI file within the FindTemplate directory would perform a LoadSearch script:

Code: Select all

"Load Search" C:\Zen\Apps\XYplorer\Data\FindTemplates\*.ini>::LoadSearch "<curbase>", 'rle';
You'll need to replace C:\Zen\Apps\XYplorer\Data with the real location of your <xydata> since it seems XY-variables don't work here.

EDIT: If you want the PFA to work well when you attempt to open the INI file from the catalog, favorites, or elsewhere in XY you should replace the above with the slightly longer:

Code: Select all

"Load Search" C:\Zen\Apps\XYplorer\Data\FindTemplates\*.ini>::LoadSearch GetPathComponent("<pfaitem>", 'base'), 'rle';
2. You could also go a step further and have a script read the FindTemplates directory and present you with a list of saved searches to select from:

Code: Select all

"Load Saved Search Template||1 : search"
  // Get list of all saved search templates.
  $templates = FolderReport('files:{Basename}', 'return', "<xydata>\FindTemplates\", /*flags*/, /*outfile*/, "<crlf>");
  End $templates == '',, 'No saved search templates were found.';

  // Prompt user to a saved search template.
  $templates = InputSelect('Select a saved search template to load.', $templates, "<crlf>", 4+8, /*cancel*/, /*w*/, /*h*/, 'Load Saved Search');
  End $templates == '';

  // Default LoadSearch options.
  $options = 'cler';

  Global $G_OPTION_PROMPT;
  if ($G_OPTION_PROMPT) {
    // Prompt user for options to use.
    $options = InputSelect('Which options should be used when loading this search template?', <<<#OPTIONS
+l: Load Search Location(s)
+e: Load Excluded Paths
+c: Load Cached Results (Forces other options to enabled.)
+r: Run Search Now
#OPTIONS, "<crlf>", 2+4+8, /*cancel*/, /*w*/, /*h*/, 'Load Saved Search');

    // Extract the flags from the selected captions.
    $options = RegexMatches($options, '^(.)', '');
    
    // Make sure we respect the user selecting nothing.
    if ($options == '') {
      $options = 'x';
    }
  }

  // Load the search.
  LoadSearch $templates, $options;

// You can load this script instead if you want to be prompted to select the search options.
"Load Saved Search Template with Options : _searchOptions"
  Global $G_OPTION_PROMPT = TRUE;
  Sub 'search';
[/size]

autocart
Posts: 1384
Joined: 26 Sep 2013 15:22

Re: [AHK] dbl-click .xy-find-files to load a saved search

Post by autocart »

TheQwerty wrote:Nice work!
Thank you! :D

Also thx for the other useful and interesting information.

Apart from that, I updated my 1st post to provide also a version that will load the saved results.

autocart
Posts: 1384
Joined: 26 Sep 2013 15:22

Re: [AHK] dbl-click .xy-find-files to load a saved search

Post by autocart »

Unfortunately, as I found out, the line
"Load Search" C:\Zen\Apps\XYplorer\Data\FindTemplates\*.ini>::LoadSearch "<curbase>", 'rle';
does not work correctly, if I put the ini-item into the catalog, since "curbase" refers to the currently selected item in the currently active list.
It does not refer to the item passed or triggered by the catalog.

Of course you Qwerty did not have the catalog in mind when you wrote it, but I am just saying.

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: [AHK] dbl-click .xy-find-files to load a saved search

Post by TheQwerty »

autocart wrote:Unfortunately, as I found out, the line
"Load Search" C:\Zen\Apps\XYplorer\Data\FindTemplates\*.ini>::LoadSearch "<curbase>", 'rle';
does not work correctly, if I put the ini-item into the catalog, since "curbase" refers to the currently selected item in the currently active list.
It does not refer to the item passed or triggered by the catalog.

Of course you Qwerty did not have the catalog in mind when you wrote it, but I am just saying.
Good find and one we can fix!

Instead of using <curbase> we can make our one-liner a little longer and use <pfaitem>:

Code: Select all

"Load Search" C:\Zen\Apps\XYplorer\Data\FindTemplates\*.ini>::LoadSearch GetPathComponent("<pfaitem>", 'base'), 'rle';

Post Reply