how to automate 3 actions via function-key F2

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Maxsteinfeld
Posts: 87
Joined: 25 Jun 2017, 02:18

how to automate 3 actions via function-key F2

25 Jun 2017, 02:39

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 :headwall:
pls help
greetz
GEV
Posts: 1002
Joined: 25 Feb 2014, 00:50

Re: how to automate 3 actions via function-key F2

25 Jun 2017, 14:01

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
Maxsteinfeld
Posts: 87
Joined: 25 Jun 2017, 02:18

Re: how to automate 3 actions via function-key F2

25 Jun 2017, 16:03

Hello
Thank you very much ! :D
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.
gregster
Posts: 9061
Joined: 30 Sep 2013, 06:48

Re: how to automate 3 actions via function-key F2

25 Jun 2017, 17:09

Maxsteinfeld wrote:Hello
Thank you very much ! :D
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.
Hi Max, welcome to the forum!!
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"
I see at least four differences in your version - how did you end up with this? You could actually copy the code from GEV's example...
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 ;)
Maxsteinfeld
Posts: 87
Joined: 25 Jun 2017, 02:18

Re: how to automate 3 actions via function-key F2

26 Jun 2017, 06:07

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
gregster
Posts: 9061
Joined: 30 Sep 2013, 06:48

Re: how to automate 3 actions via function-key F2

27 Jun 2017, 08:18

Maxsteinfeld wrote:how can i replace the characters ":" "/" with "-" in Line1 ?
which line(s) i have to add ?
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:

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
Run this little example script and see, if it does what you asked for. If yes, you should probably add the two StringReplace lines to your script just after the FileReadline statement and before line1 is put into the clipboard (clipboard = %Line1%).

For more complex replacement tasks , I would probably look into RegExReplace (https://autohotkey.com/docs/commands/RegExReplace.htm), which is more nifty.
Maxsteinfeld
Posts: 87
Joined: 25 Jun 2017, 02:18

Re: how to automate 3 actions via function-key F2

28 Jun 2017, 09:56

Hi Greg
Now the script works perfect
Thank you very much :D
Max

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww, Tech Stuff and 204 guests