AutoHotKey: How to Count number of files in active folder and copy to clipboard? - autohotkey

I have a long list folders that i need to automatically spreadsheet the following two pieces of information into our existing excel document:
Folder Name
File count within that folder
I've written the script up to the point where i now need AHK to get the file count of the active explorer window, copy to clipboard and ALT+TAB/Paste it into the spreadsheet. I plan to loop this script using 'goto' and just monitoring it until it reaches the last folder and use ESC to end the script by eye.
Whats the easiest way to get the numerical value file count of the active window and have it copied to clipboard?
My code so far which is basically using F2 to 'rename' thus copy folder name, alt+tab to spreadsheet, paste it in, move to the file count cell on the spreadsheet, alt+tab back to active explorer window, step into said folder -- and now i'm stuck (where i need to get file count onto clipboard). It's worth noting that i would want the file count to ignore system files like .DS_Store if they're present.
`::
{
Send, {F2}
Sleep, 200
Send, {Ctrl Down}
Sleep, 50
Send, c
sleep, 50
Send, {Ctrl Up}
Sleep, 100
Send, {Alt Down}
Sleep, 50
Send, {Tab}
Sleep, 50
Send, {Alt Up}
Sleep, 100
Send, {Ctrl Down}
Sleep, 50
Send, v
sleep, 50
Send, {Ctrl Up}
Sleep, 100
Send, {Right}
Sleep, 50
Send, {Right}
Sleep, 50
Send, {Right}
Sleep, 100
Send, {Alt Down}
Sleep, 50
Send, {Tab}
Sleep, 50
Send, {Alt Up}
Sleep, 100
Send, {Enter}
^^^^^^^^^^^^^ Need my file count / copy to clipboard here
Esc::ExitApp
}

Maybe have a look at something like this (and follow along in the comments):
; Calculate the number of files in a folder and its subfolders:
SetBatchLines, -1 ; Make the operation run at maximum speed.
FileNum = 0
; FileSelectFolder, WhichFolder ; Ask the user to pick a folder.
WhichFolder := Clipboard ; assumes full path to folder is in clipboard
Loop, Files, %WhichFolder%\*.*, R
{
if A_LoopFileAttrib contains H,R,S ; Skip Hidden, Read-only, or System files
continue ; Skip this file and move on to the next one
FileNum += 1
}
Clipboard := FileNum
ClipWait ; Wait for the clipboard to contain text.
MsgBox %WhichFolder% has %FileNum% files in it (incl. subfolders).
Then, have a look at the following which explains how to read and loop through directories and files:
https://autohotkey.com/docs/commands/LoopFile.htm.
Hth, let us know how you make out . . .

Related

Send Keypress to specific window/programm only

I have a Script that toggle a source in SLOBS (OBS), how can I make it that the keypress get only send to OBS, mostly my games also react to the Hotkeys, maybe it's possible to get the keypress only send to OBS so that the game is not affected by it, is this possible my actual script:
#Persistent
SetTimer, PressTheKey, 1800000
Return
PressTheKey:
Send, {F24 down}
Sleep, 50
Send, {F24 up}
Sleep, 10000
Send, {F24 down}
Sleep, 50
Send, {F24 up}
return
You need to use ControlSendRaw.

how to timeout a script when it runs into an event it can't handle autohotkey

I have a script that opens a browser and saves a webpage and I want it to cancel the job on a time out. If it's waiting for a window that doesn't show up to stop running and start over. How would I go about this? Here's my short script:
Run, www.google.com
WinWaitActive ahk_exe firefox.exe
Sleep, 1000
Send ^s
WinWait, Save As
Send ^{Backspace}
Sleep, 1000
SendRaw www.google.com.html
Sleep, 500
ControlClick, Button2, Save As,, Left, 1, NA
We can use the Timeout parameter of WinWaitActive:
From the docs:
WinWaitActive / WinWaitNotActive
Waits until the specified window is active or not active.
WinWaitActive , WinTitle, WinText, Timeout, ExcludeTitle, ExcludeText
If it takes longer than the value in the Timeout parameter, then the script continues and the variable ErrorLevel is set to 1/True.
By putting this entire script under a Label (here called savePageSubroutine), we can go back to this point with a goto statement.
savePageSubroutine:
Run, www.google.com
WinWaitActive ahk_exe firefox.exe,,1000 ; Tune this value to taste
if (ErrorLevel)
goto savePageSubroutine
Sleep, 1000
Send ^s
WinWait, Save As
Send ^{Backspace}
Sleep, 1000
SendRaw www.google.com.html
Sleep, 500
ControlClick, Button2, Save As,, Left, 1, NA
return

AutoHotkey - capture a hotkey combination in one application only - do not prevent other apps from using same combination

I have created a hotkey that I want to use only in MS Teams (workaround for the lack of 'Reply to message' function).
I assigned it to Ctrl+R, however it seems that it prevents other applications that use the same combination from noticing the hotkey.
The code is below:
^r::
if WinActive("ahk_exe Teams.exe")
{
SoundBeep 200,500
Send, ^c
Send, {Tab}
Send, >
Sleep, 500
Send, ^v
Send, {Enter}
Send, {Enter}
}
return
Is there a way to tell AHK to let the key combination bubble up when the active app is NOT teams?
I tried adding an 'else' clause in which I would Send, ^r but that didn't work.
Actually, I got it. Posting the working solution.
It seems I needed to wrap the hotkey declaration within the #ifwinactive directive.
#IfWinActive("ahk_exe Teams.exe")
^r::
SoundBeep 200,500
Send, ^c
Send, {Tab}
Send, >
Sleep, 500
Send, ^v
Send, {Enter}
Send, {Enter}
return
#IfWinActive
An even better, comprehensive solution and more powerful approach by #ThierryDalon - https://github.com/tdalon/ahk/blob/master/Lib/Teams.ahk
https://tdalon.blogspot.com/2020/11/teams-shortcuts-smart-reply.html

AutoHotKey How to save file as XPS without dialog box

At work I have to take "screen shot" of a 3rd party vendor website 300-400 times a day, and save it as a XPS or PDF file. I already build a program that will send keys to do Ctr+P, Select, Enter, Write File Name, Enter. But I am wondering if there are faster way?
What I have in mind is to execute this command automatically without the need of a Print dialog box, and physically enter the file name, and press enter to save the XPS files.
I am using Window 7 with Internet Explorer.
Send, {CTRLDOWN}p{CTRLUP}
Sleep, 1500
Send, {ENTER}
Sleep, 500
Send, {ENTER}
Sleep, 500
PutWindowInFocus("Save")
if (active) {
Send, %cin% %dateOfService% Eligibility
Sleep, 1000
} else {
Send, %cin% %dateOfService% Eligibility
Sleep, 1000
}
Send, {ENTER}
Sleep, 2000
Combine Purrint's Print to default printer option when hitting printscreen with PDF Creator's automatic-saving feature and you have it reduced to a single keypress. If the screen shots are done on a schedule, you can even configure Purrint to take the screenshot every X seconds, reducing it to zero keystrokes.

Inactivate right click when using it as shortkey

I have successfully made it so that when I hold down the right mouse button I can control the system volume with The scroll wheel. But my problem now is that every time that I release the right button it still right clicks even if I have changed the volume.
I want to retain the normal right click, but not when I have used the right mouse button in a macro (then it should ignore the release of the right mouse button). How do I best add a script to ignore mouse clicks in this situation?
This is what I use at the moment:
~RButton & WheelUp::Send {Volume_Up}
And I like it for its brevity (also, other ways have shown to be buggy), so I hope that there is a simple solution that I have missed.
I think it should be possible to have a timer based solution that works (like for double right click as shortkeys), but I have been unable to use that solution.
Here are solutions I have found that Don't quite solve it:
RButton & WheelUp::Send {Volume_Up} ; inactivates right click
RButton:: click right ; gets right click back, but unable to have right click pressed
;(dragging things with right click, etc would be impossible)
RButton & WheelUp:: Send {Volume_Up}
RButton & WheelDown::Send {Volume_Down}
OK, here is a better solution, I think.
~RButton & WheelUp::
Send {Volume_Up}
SetTimer, CloseContextMenu, 50
return
~RButton & WheelDown::
Send {Volume_Down}
SetTimer, CloseContextMenu, 50
return
CloseContextMenu:
KeyWait, RButton, R
WinGetTitle, active_title, A
WinGetClass, active_class, A
WinActivate, ahk_class Progman ;desktop
WinWaitActive, ahk_class Progman
Send, {ALT Down}{ALT Up} ; try also: Send, {Esc} (remove the Alt command)
SetTimer, CloseContextMenu, off
WinActivate, %active_title% ahk_class %active_class%
return
~RButton & WheelUp::
Send {Volume_Up}
SetTimer, CloseContextMenu, 50
return
~RButton & WheelDown::
Send {Volume_Down}
SetTimer, CloseContextMenu, 50
return
CloseContextMenu:
KeyWait, RButton, R
Send, {Esc}
SetTimer, CloseContextMenu, off
return
Try also this:
#NoEnv
SendMode Input
#SingleInstance Force
Process, Priority, ,High
#InstallKeybdHook
#InstallMouseHook
#UseHook
#MenuMaskKey vk07 ; is used to mask Win or Alt keyup events
; http://ahkscript.org/docs/commands/_MenuMaskKey.htm
return
~RButton & WheelUp::
Send {Volume_Up}
SetTimer, CloseContextMenu, 50
return
~RButton & WheelDown::
Send {Volume_Down}
SetTimer, CloseContextMenu, 50
return
CloseContextMenu:
KeyWait, RButton, R
Send, {ALT Down}{ALT Up}
SetTimer, CloseContextMenu, off
return