Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
-
Elermino
- Posts: 114
- Joined: 29 Nov 2021, 17:43
Post
by Elermino » 10 Sep 2023, 16:26
Great, it's working
Apparently there were small extra spaces and that prevented the command from being executed.
Working code:
Code: Select all
destDir := A_ScriptDir
;theseFiles7 := " " ; 7zip
theseFiles := " *.* " ; WinRAR
;app7 := "C:\Program Files\7-Zip\7z.exe"
app := A_ProgramFiles "\WinRAR\WinRAR.exe"
extract := " x "
;to7 := " -o" ; 7zip
to := " " ; WinRAR
#If WinActive("ahk_exe explorer.exe")
!x::
For each, fromArchiveFile in getSelected()
If (fromArchiveFile ~= "i)\.zip$")
RunWait % app " x """ fromArchiveFile """ -o " destDir,, Hide
If (fromArchiveFile ~= "i)\.rar$")
RunWait % app " x """ fromArchiveFile """ *.* " to destDir,, Hide
;~ MsgBox 64, Status, Done!
Return
#If
getSelected() { ; Get the paths of selected files and folders both in Explorer and on the Desktop
; https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256
; GEV: https://www.autohotkey.com/boards/viewtopic.php?p=514288#p514288
Static SWC_DESKTOP := 8, SWFO_NEEDDISPATCH := 1
WinGetClass winClass, % "ahk_id" hWnd := WinExist("A")
If !(winClass ~= "Progman|WorkerW|(Cabinet|Explore)WClass")
Return
shellWindows := ComObjCreate("Shell.Application").Windows, sel := []
If !(winClass ~= "Progman|WorkerW") {
For window in shellWindows
If (hWnd = window.HWND) && (shellFolderView := window.Document)
Break
} Else shellFolderView := shellWindows.FindWindowSW(0, 0, SWC_DESKTOP, 0, SWFO_NEEDDISPATCH).Document
For item in shellFolderView.SelectedItems
sel.Push(item.Path)
Return sel
}
-
lee2key
- Posts: 8
- Joined: 11 Dec 2023, 21:31
Post
by lee2key » 03 Feb 2024, 13:42
Hello! Do you know how to create this script for AHK v2? I'm trying to use it, but there are a lot of errors with this v1 code in the new version. I cannot pass all of them. Can you help me please?
![See-No-Evil Monkey :monkeysee:](./images/smilies/emoji_u1f648.png)
-
mikeyww
- Posts: 27366
- Joined: 09 Sep 2014, 18:38
Post
by mikeyww » 03 Feb 2024, 15:50
Generally speaking:
1.
#If becomes
#HotIf.
2.
Run % becomes
Run.
3.
Hide becomes
'Hide'.
4. Hotkeys become functions.
Code: Select all
#Requires AutoHotkey v2.0
!x:: {
MsgBox
}
5. Here is the v2 function:
viewtopic.php?p=509080#p509080
-
lee2key
- Posts: 8
- Joined: 11 Dec 2023, 21:31
Post
by lee2key » 03 Feb 2024, 19:16
mikeyww wrote: ↑03 Feb 2024, 15:50
Generally speaking:
1.
#If becomes
#HotIf.
2.
Run % becomes
Run.
3.
Hide becomes
'Hide'.
4. Hotkeys become functions.
Code: Select all
#Requires AutoHotkey v2.0
!x:: {
MsgBox
}
5. Here is the v2 function:
viewtopic.php?p=509080#p509080
I'm trying to simplify the code for me and also use only 7zip. I dont have installed WinRar. I have error on line 7: "Missing space or operator before this." But I cannot find the problem. Sorry if I'm a noob
Also I want to extract to the same folder where zip archive was and to delete that archive. Unpack and delete original. Can you help me please? I was searching in manual for v2 and in ChatGPT and cannot find the fix.
Code: Select all
#HotIf WinActive("ahk_exe explorer.exe")
#x::{
For each, fromArchiveFile in getSelected()
If (fromArchiveFile ~= "i)\.zip$")
RunWait 'C:\Program Files\7-Zip\7z.exe' " x """ fromArchiveFile """ -o " 'C:\Users\lee2k\Downloads\'
Return
}
#HotIf
getSelected() {
Static SWC_DESKTOP := 8, SWFO_NEEDDISPATCH := 1
WinGetClass winClass, "ahk_id" hWnd := WinExist("A")
If !(winClass ~= "Progman|WorkerW|(Cabinet|Explore)WClass")
Return
shellWindows := ComObjCreate("Shell.Application").Windows, sel := []
If !(winClass ~= "Progman|WorkerW") {
For window in shellWindows
If (hWnd = window.HWND) && (shellFolderView := window.Document)
Break
} Else shellFolderView := shellWindows.FindWindowSW(0, 0, SWC_DESKTOP, 0, SWFO_NEEDDISPATCH).Document
For item in shellFolderView.SelectedItems
sel.Push(item.Path)
Return sel
}
-
mikeyww
- Posts: 27366
- Joined: 09 Sep 2014, 18:38
Post
by mikeyww » 03 Feb 2024, 20:32
It looks like you missed step #5. Below is an example that you can adapt to your needs.
Code: Select all
#Requires AutoHotkey v2.0
app := 'd:\utils\7zip\7z.exe'
downloads := EnvGet('USERPROFILE') '\Downloads'
#HotIf WinActive('ahk_exe explorer.exe')
#x:: {
For fromArchiveFile in getSelected() {
SplitPath fromArchiveFile,,, &ext
If ext = 'zip' {
commandLine := app ' x "' fromArchiveFile '" -o"' downloads '" *.* -r'
; MsgBox commandLine
RunWait commandLine,, 'Hide'
}
}
MsgBox 'Done!', 'Status', 'Iconi'
}
#HotIf
getSelected() {
; Adapted: https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256 by teadrinker
hwnd := WinExist('A'), selection := []
If WinGetClass() ~= '(Cabinet|Explore)WClass'
For window in ComObject('Shell.Application').Windows {
Try window.hwnd
Catch
Return
If window.hwnd = hwnd
For item in window.document.SelectedItems
selection.Push(item.Path)
}
Return selection
}
-
lee2key
- Posts: 8
- Joined: 11 Dec 2023, 21:31
Post
by lee2key » 05 Feb 2024, 18:52
mikeyww wrote: ↑03 Feb 2024, 20:32
It looks like you missed step #5. Below is an example that you can adapt to your needs.
Code: Select all
#Requires AutoHotkey v2.0
app := 'd:\utils\7zip\7z.exe'
downloads := EnvGet('USERPROFILE') '\Downloads'
#HotIf WinActive('ahk_exe explorer.exe')
#x:: {
For fromArchiveFile in getSelected() {
SplitPath fromArchiveFile,,, &ext
If ext = 'zip' {
commandLine := app ' x "' fromArchiveFile '" -o"' downloads '" *.* -r'
; MsgBox commandLine
RunWait commandLine,, 'Hide'
}
}
MsgBox 'Done!', 'Status', 'Iconi'
}
#HotIf
getSelected() {
; Adapted: https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256 by teadrinker
hwnd := WinExist('A'), selection := []
If WinGetClass() ~= '(Cabinet|Explore)WClass'
For window in ComObject('Shell.Application').Windows {
Try window.hwnd
Catch
Return
If window.hwnd = hwnd
For item in window.document.SelectedItems
selection.Push(item.Path)
}
Return selection
}
Your code is perfect! Thank you very much for your help. I apologize if I'm being very intrusive. But I tried to change the code a bit so that it also works with files on the desktop. And I found that the problem is in the getSelected() function. It takes the window, but it doesn't work if the user is on the desktop. I don't know how I can edit the script to make it take both cases into account?
Code: Select all
getSelected() {
hwnd := WinExist('A'), selection := []
If WinGetClass() ~= 'CabinetWClass' || 'ExploreWClass' || 'Progman'
For window in ComObject('Shell.Application').Windows {
MsgBox 'The script is here?'
Try window.hwnd
Catch
Return
If window.hwnd = hwnd
For item in window.document.SelectedItems
selection.Push(item.Path)
}
Return selection
}
-
boiler
- Posts: 17387
- Joined: 21 Dec 2014, 02:44
Post
by boiler » 05 Feb 2024, 22:33
You're trying to apply AHK expression syntax here:
Code: Select all
If WinGetClass() ~= 'CabinetWClass' || 'ExploreWClass' || 'Progman'
...but that is the "pattern" or "needle" of a
regular expression because the
~= operator is a shortcut for
RegExMatch(). Regular expressions have an totally different syntax and have the specific purpose of pattern matching. This is how you would add the other option:
Code: Select all
If WinGetClass() ~= '(Cabinet|Explore)WClass|Progman'
Or this might be easier to understand:
Code: Select all
If WinGetClas() ~= 'CabinetWClass|ExploreWClass|Progman'
-
lee2key
- Posts: 8
- Joined: 11 Dec 2023, 21:31
Post
by lee2key » 06 Feb 2024, 10:59
boiler wrote: ↑05 Feb 2024, 22:33
You're trying to apply AHK expression syntax here:
Code: Select all
If WinGetClass() ~= 'CabinetWClass' || 'ExploreWClass' || 'Progman'
...but that is the "pattern" or "needle" of a
regular expression because the
~= operator is a shortcut for
RegExMatch(). Regular expressions have an totally different syntax and have the specific purpose of pattern matching. This is how you would add the other option:
Code: Select all
If WinGetClass() ~= '(Cabinet|Explore)WClass|Progman'
Or this might be easier to understand:
Code: Select all
If WinGetClas() ~= 'CabinetWClass|ExploreWClass|Progman'
Thank you! That helps a little bit for understanding. But it still ignores the Desktop
![Sad :(](./images/smilies/icon_e_sad.gif)