When autohotkey inserts a text string into a file, I need the name and path of that file. Can AHK give it to me? - autohotkey

I use autohotkey to insert a text string into a text file.
How can I store the path and the name of that file in a new variable?
Let's say I use this code for inserting a date/time stamp:
::iii:: ; insert a date time stamp
send, ID%A_YYYY%.%A_MM%.%A_DD%.%A_Hour%.%A_Min%.%A_Sec%
return
How can I modify my code to store the path and name of the file I am tagging?
Something like this? :
::iii:: ; insert a date time stamp
send, ID%A_YYYY%.%A_MM%.%A_DD%.%A_Hour%.%A_Min%.%A_Sec%
path = <code for extracting path>
filename = <code for extracting filename>
return

Try
::iii:: ; insert a date time stamp
SendInput, ID%A_YYYY%.%A_MM%.%A_DD%.%A_Hour%.%A_Min%.%A_Sec%{Enter}
SendInput, % GetFilePath_notepad() "`n"
SendInput, % GetFileName_notepad() "`n"
return
GetFilePath_notepad(){
If !WinActive("ahk_class Notepad")
{
MsgBox, Notepad isn't active
return
}
; https://autohotkey.com/docs/commands/ComObjGet.htm
Path := ""
WinGet pid, PID, A
wmi := ComObjGet("winmgmts:")
queryEnum := wmi.ExecQuery(""
. "Select * from Win32_Process where ProcessId=" . pid)
._NewEnum()
If queryEnum[process]
{
Pos := InStr(process.CommandLine, .exe,, 1)
Path := SubStr(process.CommandLine, Pos+6)
}
else
MsgBox, Process not found!
wmi := queryEnum := process := ""
If (Path != "")
return %Path%
else
MsgBox, Path not found!
}
GetFileName_notepad(){
If !WinActive("ahk_class Notepad")
{
MsgBox, Notepad isn't active
return
}
WinGetTitle, WinTitle, A
If (SubStr(WinTitle, -9) = " - Notepad")
FileName := SubStr(WinTitle, 1, -10)
If (SubStr(WinTitle, -8) = " - Editor")
FileName := SubStr(WinTitle, 1, -9)
If (SubStr(FileName, 1, 1) = "*")
FileName := SubStr(FileName, 2)
return %FileName%
}
EDIT:
Insteaf of
SendInput, % GetFilePath_notepad() "`n"
SendInput, % GetFileName_notepad() "`n"
you can use
FilePath := GetFilePath_notepad()
SendInput, %FilePath%{Enter}
FileName := GetFileName_notepad()
SendInput, %FileName%{Enter}
SendInput is faster and more reliable as Send

You can use the WinGet command to get the full path of the currently active window:
WinGet, path, ProcessPath, A
path is the variable in which you store the result of the command.
ProcessPath is a parameter of the command that tells it which information you want to extract, in this case the process path.
A means that you want to get the information of the currently active window.
To get the title of the currently active window you use the WinGetActiveTitle command:
WinGetActiveTitle, thetitle
StringTrimRight is used to remove the " - Notepad" part from the window title.
You can test your code with this:
::iii:: ; insert a date time stamp
send, ID%A_YYYY%.%A_MM%.%A_DD%.%A_Hour%.%A_Min%.%A_Sec%
WinGet, path, ProcessPath, A
WinGetActiveTitle, thetitle
StringTrimRight, thetitle, thetitle ,10
Msgbox, path=%path% `ntitle=%thetitle%
return

Related

Create auto hot key script that copies file paths onto clipboard as python styled list

I would like to create an autohotkey script that produces a python styled list when copying multiple filepaths in explorer.
E.g If I had three files within the C directory like below:
Then pressing the ahk shortcut should copy the following paths to my clipboard in the below style:
["C:\a.txt","C:\b.txt","C:\c.txt"]
However if a single filepath is selected I'd prefer for it to remain in a string format when copied onto the clipboard e.g copying 'a.txt' should copy the path to my clipboard as:
"C:\a.txt"
I've tried to generate my own ahk shortcut but keep getting errors when I try to manipulate the clipboard:
#c::
Send ^c
ClipWait
Clipboard:= Clipboard ""
temp := Clipboard
paths := StrSplit(temp, "`r`n")
num_paths := paths.MaxIndex()
if (num_paths == 1)
{
Clipboard:= `".Clipboard.`"
Return
}
Array := []
For path in paths
{
path := `".path.`"
Array.push(path)
}
Clipboard:= Array ""
Return
Any help fixing this shortcut would be appreciated.
Had to read up a bit on ahk syntax but I figured it out in the end.
Here's my working shortcut:
#c::
Clipboard := ""
Send, ^c
ClipWait , 2
Clipboard:= Clipboard . ""
temp := Clipboard
paths := StrSplit(temp, "`r`n")
num_paths := paths.MaxIndex()
if (num_paths == 1)
{
Clipboard:= "'" . temp . "'"
Return
}
output_string := "["
For key,path in paths
{
path := "'" . path . "'"
output_string := output_string . path . ", "
}
StringTrimRight, output_string, output_string, 2
output_string := output_string . "]"
Clipboard:= output_string
Return
#c::
Send ^c
Email
Email:= Email ""
temp := Email
paths := StrSplit(temp, "`r`n")
num_paths := paths.MaxIndex()
if (num_paths == 1)
{
Email:= `".Email.`"
Return
}

Ahk Script to find in the current folder a file or a folder with the latest change?

I often save files from Chrome to the Downloads folder or My Documents folder.
You have to look for these files later in the folder. Tell me whether it is possible to somehow make it more convenient using the ahk script. By pressing F3 for example, the last file or folder in the current folder is highlighted. How to implement it. Please help me, I'm a new
F3::
SetTitleMatchMode, 2
File := "" ; empty variable
Time := ""
Loop, Files, %A_MyDocuments%\Downloads\*.*, DF ; include files and folders
{
If (A_LoopFileTimeModified >= Time)
{
Time := A_LoopFileTimeModified ; the time the file/folder was last modified
File := A_LoopFileFullPath ; the path and name of the file/folder currently retrieved
}
}
; MsgBox, Last modified file in %A_MyDocuments%\Downloads is`n`n"%File%"
IfWinNotExist Downloads ahk_class CabinetWClass
Run % "explorer.exe /select," . File ; open containing folder and highlight this file/folder
else
{
SplitPath, File, name
MsgBox, Last modified file = "%name%"
WinActivate, Downloads ahk_class CabinetWClass
WinWaitActive, Downloads ahk_class CabinetWClass
; SelectExplorerItem(name) ; or:
SendInput, %name%
}
return
SelectExplorerItem(ItemName) { ; selects the specified item in the active explorer window, if present
; SelectItem -> msdn.microsoft.com/en-us/library/bb774047(v=vs.85).aspx
Window := ""
Static Shell := ComObjCreate("Shell.Application")
For Window In Shell.Windows
try IfWinActive, % "ahk_id " window.HWND
If (Item := Window.Document.Folder.ParseName(ItemName))
Window.Document.SelectItem(Item, 29)
Return (Item ? True : False)
}
https://autohotkey.com/docs/commands/LoopFile.htm

Autohotkey if in clipboard

So I am trying to look for a specific text on the webpage and do a thing if the text was found, here is my current script:
!m::
clipboard =
text = my text here
Send, {Ctrl}+A
Sleep, 100
Send, {Ctrl}+C
var1 = %clipboard%
IfInString, var1, %text%
msgbox found the text
else
msgbox no text found
And regardless if the text is on the webpage or not, it always returns "no text found"
Any help on this?
P.S. I've also tried "if contains" and removing line breaks from the variable but the result is the same :(
StringReplace, var1, var1, `r `n, All
The send commands are not correct.
A command {Ctrl}+A, will press Ctrl, release it, and then press A. The lower case letter should also be used.
You should use either:
Send, {Ctrl down}{a}{Ctrl up}
or
Send, ^{a}
Do this for both Send commands.
A return commend should also be included as the end of the hotkey code
sequence:
...
else
msgbox no text found
return
Try this:
!m::
clipboard := "", MyText := "Hello World"
cmds := ["{Ctrl down}", "a", "c", "{ctrl up}"]
Loop % cmds.MaxIndex() {
Send % cmds[A_Index]
if (A_index == 2)
sleep 100
}
MsgBox % clipboard ~= "i)" MyText ? "Found" : "Not Found"

Autohotkey and Windows 10: How to get current explorer path

I use autohotkey version: 1.0.48.05 (because I stick with activeaid).
The script to read the current path is as follows (and worked until Win 7).
; Get full path from open Explorer window
WinGetText, FullPath, A
; Clean up result
StringReplace, FullPath, FullPath, `r, , all
FullPath := RegExReplace(FullPath, "^.*`nAddress: ([^`n]+)`n.*$", "$1")
How I suspect that while switching to Win10 it seems that I also switched the language.
If I MsgBox out the %FullPath% before cleaning with
WinGetText, FullPath, A
MsgBox %FullPath%
I see amongst other strings (obvoíously separated by CR):
Adresse: V:\Vertrieb\Prospects\MyFile
so how do I need to adjust the regexp to extract that very string!
Best regards
Hannes
#IfWinActive, ahk_class CabinetWClass ; explorer
F1:: MsgBox, % GetActiveExplorerPath()
; or
F2::
ActiveExplorerPath := GetActiveExplorerPath()
MsgBox, % ActiveExplorerPath
return
#IfWinActive
GetActiveExplorerPath() {
; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=69925
explorerHwnd := WinActive("ahk_class CabinetWClass")
if (explorerHwnd)
{
for window in ComObjCreate("Shell.Application").Windows
{
if (window.hwnd==explorerHwnd)
return window.Document.Folder.Self.Path
}
}
}
Try:
f1::MsgBox % Explorer_GetSelection()
Explorer_GetSelection(hwnd="") {
WinGet, process, processName, % "ahk_id" hwnd := hwnd? hwnd:WinExist("A")
WinGetClass class, ahk_id %hwnd%
if (process = "explorer.exe")
if (class ~= "(Cabinet|Explore)WClass") {
for window in ComObjCreate("Shell.Application").Windows
if (window.hwnd==hwnd)
path := window.Document.FocusedItem.path
SplitPath, path,,dir
}
return dir
}
It takes me so much time to find the best solution (for me).
Maybe it will work for you as well.
ControlClick, ToolbarWindow323, A
ControlGetText, path, Edit1, A
Msgbox, %path%

AutoHotkey - Rename image by desired name only by selecting it and pressing shortcut

I have a quite organized workflow and I have an image that always needs to have the same name. It's always a PNG (Portable Network Graphics) and no matter if it's on Desktop or in a folder.
So i just want to select the image and with a one letter shortcut (for example "L") rename it (regardless it's previous name) to "LAYOUT"
F2::
ClipSaved := ClipboardAll ; save the entire clipboard to the variable ClipSaved
clipboard := "" ; empty clipboard
Send, ^c ; copy the selected file
ClipWait, 1 ; wait for the clipboard to contain data
if (!ErrorLevel) ; If NOT ErrorLevel clipwait found data on the clipboard
{
clipboard := clipboard ; convert to text (= copy the path of the selected file)
Sleep, 300
; MsgBox, %clipboard% ; display the path
if (SubStr(clipboard, -2) != "png")
{
MsgBox, No PNG-file selected
clipboard := ClipSaved
return
}
; otherwise:
SplitPath, clipboard, name, dir
FileMove, %clipboard%, %dir%\LAYOUT.png
Sleep, 100
clipboard := ClipSaved ; restore original clipboard
}
else
{
MsgBox, No file selected
clipboard := ClipSaved
}
return