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

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
}

Related

Autohotkey Setting A_Scriptdir Based On Different Script Locations on Different Computers

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

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

Autohotkey - Copy from HTML and paste in Word

I am new to Autohotkey, so need your patience.
I have a folder with 6236 individual HTML files; each contains one sentence.
The names of these files go like this: 001001, 001002, 001003,..., 001007, 002001,002002, 002003 and so on. Of course, with .html file format.
I want to write a script with hotstrings such as:
:*:001001::
:*:001002::
and so on, so that when I type 001001 in Word, have the content of 001001.html pasted into the word. And when I type 001002 it pastes the content of 001002.html into the word. This way for all the 6236 files.
Can you help me with this?
Main code
loop,6236
{
number := 001000 + A_Index
number := "00" . number
temptext1 :=":*:" . number . "::"
temptext2 := "FileRead, orgtext, C:\" . number . ".html"
temptext3 := "SendInput % orgtext"
temptext4 := "return"
temptext := temptext . temptext1 . "`n" . temptext2 . "`n" . temptext3 . "`n" . temptext4 . "`n`n"
}
FileAppend, %temptext%, C:\test.ahk
Loop 1 will create following text
:*:001001::
FileRead, orgtext, C:\001001.html
SendInput % orgtext
return
Loop 2 will create
:*:001002::
FileRead, orgtext, C:\001002.html
SendInput % orgtext
return
And so on until 007236. You need to change where your html files are located. I assumed C:\001001.html. If for example files are in C:\Program Files\Myhtmlfiles\001001.html then edit line 6 into:
temptext2 := "FileRead, orgtext, C:\Program Files\Myhtmlfiles\" . number . ".html"
This is quite a long loop so it takes a couple of seconds to finish. After the loop is finished result gets saved into test.ahk located in C:\

Join lines automatically using autohotkey

Long paragraphs in Adobe Acrobat results in a line break when text is copied to clipboard and pasted in Word.
To deal with this, I manually paste the copied-text in Notepad ++ > select all > ctrl+J (join lines) > select all > Copy...... and then paste it into my Word document.
I would like to automate this join line using autohotkey as I have a large body of documents to go through. I can't seem to find any examples online in dealing with this directly within autohotkey script.
Ex.
Data structures with labeled axes supporting automatic or explicit data alignment.
This prevents common errors resulting from misaligned data and working with
differently-indexed data coming from different sources.
After manually joining in Notepad ++
Data structures with labeled axes supporting automatic or explicit data alignment. This prevents common errors resulting from misaligned data and working with differently-indexed data coming from different sources.
This works:
#Persistent
OnClipboardChange("ClipChanged")
return
ClipChanged() {
If (WinActive("ahk_exe AcroRd32.exe")) {
For e, v in StrSplit(clipboard, "`n", "`r")
x .= v " "
clipboard := trim(x)
}
}
Try this:
#Persistent
return
OnClipboardChange:
If WinActive("ahk_exe AcroRd32.exe")
{
clipboard =
Send, {Ctrl down}c{Ctrl up}{Esc}
ClipWait
clip := RegExReplace(clipboard, "(\S.*?)\R(.*?\S)", "$1 $2") ; strip line breaks and replace them with spaces
clipboard = %clip%
StringReplace clipboard, clipboard, % " ", % " ", A ; replace double spaces with single spaces
ClipWait
ControlClick, x0 y0, A
}
return
EDIT
Or this:
#Persistent
return
OnClipboardChange:
If WinActive("ahk_class AcrobatSDIWindow")
{
clipboard := ""
Send, {Ctrl down}c{Ctrl up}{Esc}
ClipWait
clip := RegExReplace(clipboard, "(\S.*?)\R(.*?\S)", "$1 $2") ; strip line breaks and replace them with spaces
StringReplace clip, clip, % " ", % " ", A ; replace double spaces with single spaces
clipboard := ""
clipboard = %clip%
ClipWait 2
If !(ErrorLevel)
{
ToolTip, lines joined
Sleep 500
ToolTip
}
}
return
I added functionality considering lines ending with a hyphen.
Before:
This occurs in elec-
tricity generation
systems.
After:
This occurs in electricity generation systems.
Code:
#Persistent
return
OnClipboardChange:
If WinActive("ahk_exe AcroRd32.exe")
{
clipboard := ""
Send, {Ctrl down}c{Ctrl up}{Esc}
ClipWait
clip := RegExReplace(clipboard, "(\S.*?)-\R(.*?\S)", "$1$2") ; strip line breaks with hyphen
clip := RegExReplace(clip, "(\S.*?)\R(.*?\S)", "$1 $2") ; strip line breaks and replace them with spaces
StringReplace clip, clip, % " ", % " ", A ; replace double spaces with single spaces
clipboard := ""
clipboard = %clip%
ClipWait 2
If !(ErrorLevel)
{
ToolTip, lines joined
Sleep 500
ToolTip
}
}
return

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