I have a variable with a value stored in an AutoHotKey macro. I want the user to be able to change the variable whenever they want and to have the macro use the new value stored in the variable when the macro is launched in the future. Is it possible to change a variable in a running macro and have the macro use the new variable value the next time the macro is launched? Alternatively, is there a way for the user to change a variable in a compiled macro? I'm also wondering about how to the user interface would be like for the user to be able to change the variable when the user chooses to change the variable?
Sure, you can store the variable in a text file. This will be the most simple and reliable method to change a variable and store it permanently.
Here is an example. Create a file "config.txt" in the same directory and put the text there. Pressing F1 will reload the text file into variable v.
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
fname := "config.txt" ; define file name (must be in the same directory)
loadvar(fname) ; load file
return ; end main
; loadvar function
loadvar(fname){
global v
FileRead, v, %fname%
tooltip %v%
}
; reload variable
F1::
loadvar(fname)
return
Related
I want define a custom hotkey to rename a selected file in File Explorer. So my new hotkey should behave exactly like the F2 key does by default. That is, when I press the hotkeys, the file name should be editable, allowing me to type a new name. However, I can't use the F2 key to cause windows to do this.
The reason is that I'm using the default hotkeys for something else. I am often running an application (unrelated to AutoHotKey) where the buttons in the UI are triggered using keyhooking for all of the F keys. The only suggestions that I can find on this are to have my custom hotkey use 'send' to raise the default key codes that would be associated with the action. This won't work, because I am using those hotkeys for something else. What I need is a solution that causes a file to be renameable without sending the F2 keycode.
^+!R::
Send {F2} ;This won't work for me
return
Actually the original hotkey can be used, just add $ before the hotkey that you don't want to be fired by the send command. Also using app-specific hotkeys a good idea to minimize possible conflicts.
Try this:
#If winActive("ahk_exe Explorer.EXE")
^+!R::
send {F2}
return
$F2::
send {down}
return
In case you have an application which scans the F2 key globally and unconditionally, and you cannot redefine it, there is not much you can do from within AHK. So ideally you should get rid of that application, and use e.g. AHK for same functionality, or find some workaround.
In this particular case the easiest workaround is alternative way to rename the file:
^+!R::
send {AppsKey}
sleep 100
send {m}
return
I am trying to assign a string to a variable, and then have a hotkey send that variable's value.
I already have a functioning example, where I use f1 to send a text string with some of the automatic Autohotkey variables such as %A_MM% %A_DD% and %A_YYYY% for quick and easy datestamps:
f1::
Send, (WTC %A_MM%/%A_DD%/%A_YYYY% %A_Hour%:%A_Min%) :{space} ; press f1
Return
I tried writing this to test:
v_test := "testing string in v_test"
f2::
MsgBox, %v_test%
RETURN
But the message box come up blank. Why isn't the message box displaying "testing string in v_test"?
The variables were never initialized because the variable was not contained within the auto-execute section of the autohotkey script.
To fix this, I had to put the variable declaration at the top of the script, before any RETURNs, EXITs, hotkeys, or hotscripts. I also could have written the variable as GLOBAL, inside of a function, then called the function.
So I have a program with a Tkinter interface. I used just one class and a few commands but when I change a variable in a command it doesn' t change it for the whole program, but only in the command. How can I make that variable change for the whole program in a command?
Use global to declare a global variable
global myVar
I am not sure how to start this one. I went through the help files of AHK with no result.
I need to establish a construct like this (this is not in either UNIX shell or in AHK scripting language. Just shows the idea I am after)
while true
do
gosub screenscrape
; here, all text on page is on clipboard
; variable "input" is a predefined portion of the clipboard
case $input in
string-1)
gosub subroutione1;;
string-2)
gosub subroutine2;;
...
*)
echo "not found"
sleep 1minute
done
to make the things more complex, the parts noted as string-1, through string-n are also variables, read in from few different files, depending on time of the day or by trigger from an event.
can someone point me to the right direction for this ?
Autohotkey does not have any case statements. What everyone does is to use if and else statements. (I know how it feels. I like case, too)
A few tips for you:
You can't have that $ or a - in your variable name in autohotkey
...but you CAN have an underscore.
You don't need to terminate your lines with a ;.
Here is a translation into autohotkey for you.
gosub screenscrape
; here, all text on page is on clipboard
; variable "input" is a predefined portion of the clipboard
if(string1){ ;if the variable is not blank it will evaluate to `true`
gosub subroutine1
}else if(string2){
gosub subroutine2
}else{
msgbox not found
sleep 60000 ;milliseconds
}
Also, you can use real functions in autohotkey - you don't have to rely on gosubs. If you use gosubs, make sure you place a return at the end of them. You should also read the autohotkey docs concerning the "auto execute" section.
Is there a way to write an autohotkey within an autohotkey? For instance, I have an autohotkey that opens some websites in tabs at work for me & I have an autohotkey that when typed puts in my username & password for some of those sites. Is there a way to put the password autohotkey (actd(at symbol)) inside the IE tabs autohotkey? I did some searching, & it doesn't look like {#} can be sent, so I wasn't sure if there was another way to do it.
Your AutoHotKey script can #Include other scripts, assuming they are not #Persistent. You could loop through your list of tabs, and then call one or more other scripts.
As far as sending the # sign, you should be able to use the Send command without problems. If you do encounter a strange problem, then you can try using the SendRaw command or use the syntax: Send {raw}#
If this doesn't answer your question, please paste some code of what you are trying to get working.
What you can do is write two separate scripts. One does not have an autohotkey assigned, but gets called by the initial script. In your case, you have said that you already have a tabopening hotkey, so I will use a VERY rudimentary example of both scripts, but demonstrate how to send the # symbol and call another script from within a hotkey.
The two scripts I would use are:
tabOpener.ahk, and
passwordEntry.ahk
And they would appear as follows:
tabOpener.ahk
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
; hotkey is set as WinowsKey+t and checks if FireFox exists, if so, it makes it active, if not, it starts FireFox
~#t::
IfWinExist, ahk_class MozillaWindowClass
{ WinActivate
Send ^t
} else run firefox
;Upon opening a new tab in FireFox, the address bar has already got focus,
;so send the web address (You can use COM functions for this, and are highly
;recommended, but out of the scope of this example)
Send, hotmail.com{Enter}
;Waits until page is loaded. Again COM functions are available as they can tell
;when a page is finished loading, but that is a more involved example.
Sleep 5000
;Calls the password entry script.
Run passwordEntry.ahk
return
passwordEntry.ahk
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
;When using send, adding the {Raw} tag will literally interpret every
;character in the remainder of a string. For instance ^ will send ^
;instead of a Control keypress.
Send {Raw}Username#domain.tld
Send {Tab} {Raw}Password
Hopefully that helps. This example showed the use of the {Raw} tag for sending special characters (http://www.autohotkey.com/docs/commands/Send.htm), in addition to calling a separate script from within one's existing hotkey using Run / Runwait (http://www.autohotkey.com/docs/commands/Run.htm).
;~ Alt+1 to send username (email format) and password
!1::Send, myUsername#mydomain.com{tab}myPassword{enter}
depending on you web browser you could use COM objects to handle this very easily. You would find the user id password fields and then for example:
url := "http://yourwebsite.com"
wb := ComObjCreate("InternetExplorer.Application") ; create broswer object
wb.navigate(url)
wb.visible := true ; sets the browser as visible, defaults as not
While (wb.busy || wb.readyState <> 4)
Sleep 100<br>
wb.document.all.username.value := "yourname#wherever.com"
wb.document.all.password.value := "Pa$$word15"
wb.document.all.btnLogin.click()
this however depends on if you are using IE to access your site or not. Look at COM objects in the docs a bit to get a feel for it, and you will learn some really basic things about the DOM, here: Basic DOM MSDN. where I set the "username" & "password" and "btnLogin" control ids in our javascript, would need to be discovered by looking at your page. you should also check out out this tutorial: AHK Basic COM/JavaScript Tutorial