Improve AHK script

- Include cypress and help folders while ignoring snapshot images/icons
- Increase delay before and after running command
- Add a popup window to show that you can press Esc to abort,
  as well as the current progress within the files list.
- Add a confirmation with some general info about the script
- Optionally copy the file paths to the clipboard if you say No
main
Isaiah Odhner 2024-01-29 03:22:55 -05:00
parent 8fe91573a7
commit 32dc2e3432
1 changed files with 82 additions and 35 deletions

View File

@ -1,3 +1,4 @@
; This script uses AutoHotkey v2
root := A_ScriptDir "\..\*"
ignoreFolders := [
".git",
@ -5,14 +6,30 @@ ignoreFolders := [
"node_modules",
"lib",
"out",
"images",
"localization",
"cypress",
"help",
]
ignoreExtensions := [
"png",
"gif",
"jpg",
"jpeg",
"svg",
"cur",
"ico",
"icns",
"wav",
]
command := "cSpell.addIssuesToDictionary"
beforeRunInfo := "This script is designed to add all spelling issues to the dictionary.`nTo use it to prune the dictionary of no-longer-needed words, you must empty the dictionary first.`n`nThis script will run the VS Code command '" command "' on all files in the directory '" root "'.`n`n"
IsIgnoredPath(path) {
SplitPath path, &name, &dir, &ext, &name_no_ext, &drive
for _, ignoreExtension in ignoreExtensions {
if (StrLower(ext) = ignoreExtension) {
return true
}
}
parts := StrSplit(path, "\")
for index, part in parts {
for _, ignoreFolder in ignoreFolders {
@ -21,54 +38,77 @@ IsIgnoredPath(path) {
}
}
}
return false
}
RunCommandOnFiles() {
; Focus VS Code
WinActivate "ahk_exe Code.exe"
Automate() {
targets := []
; Loop through files (F) recursively (R)
processed := []
Loop Files, root, "FR"
{
; Skip if the file should be ignored
if (IsIgnoredPath(A_LoopFileFullPath)) {
continue
}
; Open file in VS Code using Ctrl+P file switcher
Send "^p"
Sleep 100
Send A_LoopFileFullPath
Sleep 100
Send "{Enter}"
Sleep 1000
; Run command via F1 command palette
Send "{F1}"
Sleep 100
Send command
Sleep 100
Send "{Enter}"
Sleep 1000
; Close the file
Send "^w"
Sleep 100
processed.Push(A_LoopFileFullPath)
targets.Push(A_LoopFileFullPath)
}
if (processed.Length = 0) {
if (targets.Length = 0) {
MsgBox "No files found for pattern: " root
return
}
MsgBox "Processed " processed.Length " files."
; MsgBox "Processed " processed.Length " files:`n" Join("`n", processed)
; A_Clipboard := Join("`n", processed)
; Focus VS Code
WinActivate "ahk_exe Code.exe"
; Ask for confirmation
if MsgBox(beforeRunInfo "Found " targets.Length " files. Continue?", "VS Code Automation", 4) = "No" {
if MsgBox("Copy file paths to clipboard?", "VS Code Automation", 4) = "Yes" {
A_Clipboard := Join("`n", targets)
}
return
}
RunCommandOnFiles(targets)
}
RunCommandOnFiles(targets) {
MyGui := NotifyEscapeHatch()
SB := MyGui.Add("StatusBar")
for index, target in targets {
SB.SetText(index "/" targets.Length)
RunCommandOnFile(target)
}
MyGui.Destroy()
MsgBox "Processed " targets.Length " files."
return
}
RunCommandOnFile(target) {
; Open file in VS Code using Ctrl+P file switcher
Send "^p"
Sleep 100
Send target
Sleep 100
Send "{Enter}"
; Give enough time for spell checker to run
Sleep 2000
; Run command via F1 command palette
Send "{F1}"
Sleep 100
Send command
Sleep 100
Send "{Enter}"
; Give enough time for CSpell to update the dictionary
Sleep 2000
; Close the file (optional)
; Send "^w"
}
Join(sep, items) {
str := ""
for index, item in items {
@ -78,8 +118,15 @@ Join(sep, items) {
}
; Escape hatch
NotifyEscapeHatch() {
MyGui := Gui(, "VS Code Automation")
MyGui.Opt("+AlwaysOnTop +Disabled -SysMenu +Owner") ; +Owner avoids a taskbar button.
MyGui.Add("Text", , "`nPress Esc to stop the script.`n`n")
MyGui.Show("NoActivate") ; NoActivate avoids deactivating the currently active window.
return MyGui
}
Esc:: {
ExitApp
}
RunCommandOnFiles()
Automate()