topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Tuesday September 29, 2020, 4:20 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: AutoHotkey template to act on selected files in active Explorer window  (Read 749 times)

Nod5

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,060
    • View Profile
    • Donate to Member
AutoHotkey script template to take action on selected files in active File Explorer window in Windows 10.

Background
FARR can find and launch files, but also do much more. This AutoHotkey script template helps you use FARR to quickly take action on selected files in the active Explorer window. Similar to a context menu action in Explorer, but started from a FARR alias.

AutoHotkey script template
Code: Autohotkey [Select]
  1. SetWorkingDir %A_ScriptDir%
  2.  
  3. ; Template: take action on selected files in active Explorer window
  4. ; - AutoHotkey helper script template for FindAndRunRobot (FARR)
  5. ; - by Nod5 2020-04-25
  6.  
  7. ; Get active the window's handle (Hwnd) from parameter passed by FARR
  8. vHwnd := A_Args[1]
  9.  
  10. ; verify that the window is Explorer
  11. WinActivate, ahk_id %vHwnd%
  12. If !WinActive("ahk_class CabinetWClass ahk_id " vHwnd)
  13.  
  14. ; get filepaths for selected files
  15. aFiles := ExplorerHwndSelectedFilepaths(vHwnd)
  16.  
  17. ; ----------------------------------------------------
  18. ; add code to take action on the files here
  19.  
  20. ; example: show each filename in a messagebox popup
  21. For Key, vFile in aFiles
  22. {
  23.   SplitPath, vFile, vFilename
  24.   MsgBox % vFilename
  25. }
  26.  
  27. ; ----------------------------------------------------
  28.  
  29. ; exit script when finished
  30.  
  31.  
  32. ; required function
  33. ; function: ExplorerHwndSelectedFilepaths()
  34. ; return array of filepaths for selected files in Explorer window identified via HWND
  35. ExplorerHwndSelectedFilepaths(vHwnd)
  36. {
  37.   if !vHwnd or !WinExist("ahk_class CabinetWClass ahk_exe Explorer.exe ahk_id " vHwnd)
  38.     return
  39.   for window in ComObjCreate("Shell.Application").Windows
  40.   {
  41.     if (window.HWND != vHwnd)
  42.       continue
  43.     aFiles := []
  44.     sfv   := window.Document
  45.     ; note: gets items in alphanum ascending sort order
  46.     ; https://docs.microsoft.com/en-us/windows/desktop/shell/shellfolderview-selecteditems
  47.     items := sfv.SelectedItems
  48.     for item in items
  49.       aFiles.push(item.Path)
  50.     break
  51.   }
  52.   window := item := items := sfv := ""
  53.   return aFiles
  54. }

Preparation
Install AutoHotkey, https://www.autohotkey.com/

Create a new action
- Download the template to a file, for example "C:\some folder\timestamp action.ahk"
- Edit the template to add code for the action you want to perform on the selected files
- Create a FARR user alias to launch the script and pass the parameter %LASTHWND%

Example FARR alias
- alias keyword: timestamp now
- regular expression pattern: ^(stamp)$
- result: C:\some folder\timestamp action.ahk %LASTHWND%

Use the action
- Open Explorer and select some files
- Open FARR, type to show the alias action (for example "stamp") and press Enter.

Note
The regular expression pattern is optional in FARR aliases, but useful to more quickly get to a single result.
For max speed use short patterns like ^(sta|stam|stamp)$ to show only the alias result when you type "sta", "stam" or "stamp" but still let FARR show regular file search results if you continue typing for example "stamp collection".

Action example
Code: Autohotkey [Select]
  1. ; set "time modified" for the file to the current time
  2. For Key, vFile in aFiles
  3. {
  4.   FileSetTime, % A_Now, % vFile, M
  5. }


List of action ideas
- add to music/video app playlist
- copy to some other folder
- create file shortcuts in some other folder
- create a companion note file ("filename.jpg -- notes.txt")
- compare selected files in WinMerge
- scale image files 50%
- rotate image files 90 degrees
- change file date modified/created timestamp to current time
- create backup file copy with a timestamp suffix ("filename_20200423103546.txt")
(Useful for basic versioning when Git is overkill but you want some order to avoid a mess like "text.doc", "text2 final.doc", "text 2 final FIXED.doc")
- calculate and save file hashes (sha1, sha256, ...)

Ideas for more advanced actions
Asymmetric action on two selected files: copy/clone some feature (image size, timestamp, filename pattern, ...) from first file onto second file. For example clone a date modified timestamp.

Why use this rather than regular Explorer context menu actions?
- easy to create and use if you're already using FARR and AutoHotkey
- very quick if you set up short regex alias patterns
- quick to toggle aliases on/off
- you can add and show context/instructions to alias text to remind you what the action does
- avoid browsing a cluttered context menu



Example with screenshots
Use case: We want a quick way to to create .txt files to write notes about some files, for example some images.

1.png

Edit the template and add an action to create .txt note files. Save, for example to "C:\test\create txt note.ahk"

Code: Autohotkey [Select]
  1. ; create companion text file for notes
  2. For Key, vFile in aFiles
  3. {
  4.   FileAppend, , % vFile " -- notes.txt"
  5. }

Create a user alias in FARR

2.png

Select files in Explorer, Open FARR and use the alias

3.png

Text files are created.

4.png

Take important notes.

5.png
« Last Edit: April 25, 2020, 06:07 AM by Nod5 »

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,364
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
awesome  :up:

anandcoral

  • Honorary Member
  • Joined in 2009
  • **
  • Posts: 698
    • View Profile
    • App, to help you : Overlap Wallpaper, Park Cursor Aside, Stick A Note, Merge CSV and Text and many more.
    • Donate to Member
Great for learning features of Farr and Autohotkey too.
Thanks a lot  Nod5, :Thmbsup:

Regards,

Anand