Hi
I'm a newbie in using AHK...my problem:
I would like to automate the following 3 actions via function-key F2
(1) in windows-explorer open a marked txt-file with notepad++ (or MS-editor)
(2) copy the first line in this TXT-file into clipboard
(3) close n++
I'm sure that must be possible, but i'm do not find the correct script
pls help
greetz
how to automate 3 actions via function-key F2
-
- Posts: 87
- Joined: 25 Jun 2017, 02:18
Re: how to automate 3 actions via function-key F2
You don't need an editor in this cace:
Code: Select all
#If WinActive("ahk_class CabinetWClass") ; explorer
F2::
ClipSaved := ClipboardAll ; save the entire clipboard to the variable ClipSaved
clipboard := "" ; empty the clipboard (start off empty to allow ClipWait to detect when the data has arrived)
Send, ^c ; copy the selected file
ClipWait, 2 ; wait for the clipboard to contain data.
if (!ErrorLevel) ; If NOT ErrorLevel, ClipWait found data on the clipboard
{
clipboard := clipboard ; convert clipboard to plain text (= copy the path of the selected file)
Sleep, 300 ; needs a little time to convert the clipboard
; MsgBox, %clipboard% ; display the path
if (SubStr(clipboard, -2) != "txt") ; if the selected file is no txt-file
{
MsgBox, No txt-file selected
clipboard := ClipSaved ; restore original clipboard
return
}
; otherwise:
FileReadLine, Line1, %clipboard%, 1
; MsgBox, "%Line1%"
clipboard = %Line1%
}
else
{
MsgBox, No file selected
clipboard := ClipSaved ; restore original clipboard
}
return
#If
-
- Posts: 87
- Joined: 25 Jun 2017, 02:18
Re: how to automate 3 actions via function-key F2
Hello
Thank you very much !
the script works very well - but only if I omit the first line in the script:
; #If WinActive (" ahk_class CabinetWClass "), explorer"
...
; #if
i do not understand which function does this line have ?
best regards
M.
Thank you very much !
the script works very well - but only if I omit the first line in the script:
; #If WinActive (" ahk_class CabinetWClass "), explorer"
...
; #if
i do not understand which function does this line have ?
best regards
M.
Re: how to automate 3 actions via function-key F2
Hi Max, welcome to the forum!!Maxsteinfeld wrote:Hello
Thank you very much !
the script works very well - but only if I omit the first line in the script:
; #If WinActive (" ahk_class CabinetWClass "), explorer"
...
; #if
i do not understand which function does this line have ?
best regards
M.
To get information about certain syntax elements, you can click them in the code box above and the relevant documentation will be shown. There is also a basic tutorial in the docs...
That said, the #if directive helps to define context-sensitive hotkeys.
In this case, the hotkey F2 shouldn't fire in all cases, but only when the explorer window is active (because of your point (1) above) ; and "CabinetWClass" is the class name of the windows explorer window (to verify, run your script, right click on its task bar icon, run "Window Spy" and hover with your mouse over an explorer window).
But nevertheless, I am not surprised that it didn't work for you, if you used the line from your quote above. Your #if-directive is much different from GEV's example. Compare
Code: Select all
#If WinActive("ahk_class CabinetWClass") ; explorer
and your version
#If WinActive (" ahk_class CabinetWClass "), explorer"
but to make it clearer: in functions - between function name and the brackets(), there is no space allowed. The additional leading and trailing spaces inside of the brackets will cause more problems because they are not part of the explorer ahk_class name. Then, in the original version, the " ; explorer" part was a comment, not part of the code. By replacing the ";" by a "," you made it part of the syntax, as an additional parameter. Usually, AHK would ignore this syntax error because it doesn't expect any more parameters in an #if directive. But you added an additional " which finally leads to a syntax error becauce there are now three "s... Hope it helps
-
- Posts: 87
- Joined: 25 Jun 2017, 02:18
Re: how to automate 3 actions via function-key F2
Hi Greg,
Thx a lot for the hints and help - you and GEV are right: the hole script works correct !
i'm still a bloody beginner and already i have an addional question to the above script:
how can i replace the characters ":" "/" with "-" in Line1 ?
which line(s) i have to add ?
best regards
max
Thx a lot for the hints and help - you and GEV are right: the hole script works correct !
i'm still a bloody beginner and already i have an addional question to the above script:
how can i replace the characters ":" "/" with "-" in Line1 ?
which line(s) i have to add ?
best regards
max
Re: how to automate 3 actions via function-key F2
Well, I am not really good with string operations, but I would probably use 'StringReplace' twice(see https://autohotkey.com/docs/commands/StringReplace.htm), for simplicity and convenience:Maxsteinfeld wrote:how can i replace the characters ":" "/" with "-" in Line1 ?
which line(s) i have to add ?
Code: Select all
line1 := "Hello:World/How:are/you:today?"
StringReplace, line1, line1, :, -, All ; replace all occurences of : with -
StringReplace, line1, line1, /, -, All ; replace all occurences of / with -
msgbox % line1
For more complex replacement tasks , I would probably look into RegExReplace (https://autohotkey.com/docs/commands/RegExReplace.htm), which is more nifty.
-
- Posts: 87
- Joined: 25 Jun 2017, 02:18
Re: how to automate 3 actions via function-key F2
Hi Greg
Now the script works perfect
Thank you very much
Max
Now the script works perfect
Thank you very much
Max
Who is online
Users browsing this forum: FanaticGuru and 142 guests