Skip to content

Instantly share code, notes, and snippets.

@perXautomatik
Last active October 19, 2021 07:04
Show Gist options
  • Save perXautomatik/c23bc75abd6001f477521a80dc3b4a83 to your computer and use it in GitHub Desktop.
Save perXautomatik/c23bc75abd6001f477521a80dc3b4a83 to your computer and use it in GitHub Desktop.
AutoHotkey template to act on selected files in active Explorer window - DonationCoder.com
; set "time modified" for the file to the current time
For Key, vFile in aFiles
{
FileSetTime, % A_Now, % vFile, M
}
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%
#SingleInstance force
SetBatchLines, -1
SetWinDelay, -1
SetControlDelay, -1
; Template: take action on selected files in active Explorer window
; - AutoHotkey helper script template for FindAndRunRobot (FARR)
; - by Nod5 2020-04-25
; Get active the window's handle (Hwnd) from parameter passed by FARR
vHwnd := A_Args[1]
; verify that the window is Explorer
WinActivate, ahk_id %vHwnd%
If !WinActive("ahk_class CabinetWClass ahk_id " vHwnd)
ExitApp
; get filepaths for selected files
aFiles := ExplorerHwndSelectedFilepaths(vHwnd)
; ----------------------------------------------------
; add code to take action on the files here
; example: show each filename in a messagebox popup
For Key, vFile in aFiles
{
SplitPath, vFile, vFilename
MsgBox % vFilename
}
; ----------------------------------------------------
; exit script when finished
ExitApp
; required function
; function: ExplorerHwndSelectedFilepaths()
; return array of filepaths for selected files in Explorer window identified via HWND
ExplorerHwndSelectedFilepaths(vHwnd)
{
if !vHwnd or !WinExist("ahk_class CabinetWClass ahk_exe Explorer.exe ahk_id " vHwnd)
return
for window in ComObjCreate("Shell.Application").Windows
{
if (window.HWND != vHwnd)
continue
aFiles := []
sfv := window.Document
; note: gets items in alphanum ascending sort order
; https://docs.microsoft.com/en-us/windows/desktop/shell/shellfolderview-selecteditems
items := sfv.SelectedItems
for item in items
aFiles.push(item.Path)
break
}
window := item := items := sfv := ""
return aFiles
}

🔗

« on: April 25, 2020, 05:22 AM »

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]

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".

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"

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 »

Url: https://www.donationcoder.com/forum/index.php?topic=49845.0

; create companion text file for notes
For Key, vFile in aFiles
{
FileAppend, , % vFile " -- notes.txt"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment