Autohotkey Setting A_Scriptdir Based On Different Script Locations on Different Computers - autohotkey

I have several different computers that have an extra drive F:\dropbox\autohotkey or c:\dropbox\autohotkey or a remapped b:\ drive.
I want to create a startup script to determine which drive exists, set it to A_Scriptdir, and execute a file.
This is a script created by ChatGPT with an array. It does not work yet, but it added an array to determine which location to execute.
It does not work correctly. any help would be greatly appreciated.
#noenv
#SingleInstance, force
#noenv
#SingleInstance, force
#requires autohotkey v1
setscriptdir()
SetScriptDir()
{
static scriptDir := ""
if (scriptDir != "")
{
scriptDir := A_ScriptDir
;~ A_ScriptDir := scriptDir
return
}
pathArray := ["b:\", "f:\dropbox\autohotkey", "c:\dropbox\autohotkey"]
loop % pathArray.Length()
{
path := pathArray[A_Index]
;~ MsgBox %path%
if FileExist(path . "\" . bbb-1-myahkdefault.ahk)
{
scriptDir := path
scriptDir := A_ScriptDir
MsgBox %scriptDir%
;~A_ScriptDir := scriptDir
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
}

Distinguish source of the clipboard change

I wanted to log clipboard content whenever it changes, so I created this code:
#Persistent
FileEncoding, UTF-8
OnClipboardChange("ClipChanged")
return
ClipChanged(Type) {
now := A_Now
FormatTime, time, now, dd.MM.yyyy HH:mm:ss
FileAppend, % Format("{} '{}'`n", time, Clipboard), D:\clipboard_log.txt
}
Now, I would like to ignore scenarios when clipboard has been changed by me. I want to log clipboard changes made only by external applications. I came up with an idea:
#Persistent
FileEncoding, UTF-8
OnClipboardChange("ClipChanged")
global clipChangedByMe := false
return
ClipChanged(Type) {
if (clipChangedByMe)
{
clipChangedByMe := false
}
else
{
now := A_Now
FormatTime, time, now, dd.MM.yyyy HH:mm:ss
FileAppend, % Format("{} '{}'`n", time, Clipboard), D:\clipboard_log.txt
}
}
^c::
clipChangedByMe := true
Clipboard := ClipboardAll
return
For some reason, ClipChanged event is not fired after line: Clipboard := ClipboardAll. Whats wrong there?
At the first glance I see a couple of issues here. If you want the original function of Ctrl + C preserved you have to use the tilde prefix: ~^c. Secondly, both Clipboard and ClipboardAll are built in variables and you are assigning one to the other for some reason?
Update: Well, the following seems to work:
Global clipChangedByMe := false
OnClipboardChange("ClipChanged")
~^c::clipChangedByMe := true
ClipChanged() {
If (clipChangedByMe)
clipChangedByMe := false
else {
FormatTime, time,, dd.MM.yyyy HH:mm:ss
FileAppend, % Format("{}`n{}`n`n", time, Clipboard), D:\clipboard.log
}
}

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

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

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 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%