New User - Starter Questions

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
CPerry
Posts: 14
Joined: 21 Mar 2021, 09:13

New User - Starter Questions

Post by CPerry » 21 Mar 2021, 09:26

Good afternoon,

I have recently downloaded AutoHotKey to automate a trading system I use twice daily (on/off). First of all, am I correct in thinking I will need to create two different files on my server's desktop and use task scheduler to trigger each independently at different times? I need each to run a single time, one at 7:30am and one at 10:00pm.

To achieve what I am looking to do, I will need my code to left-click, right-click, highlight multiple things within a programme (left-click hold and drag), rename files and do this with a naming convention. For example, TradingSystemV1.4 is the master copy and each evening, I want to create one for the following day called TradingSystemV1.4(YYYY/MM/DD) where obviously those letters stand for year, month and day.

I hope all of this is possible and I would be very grateful if any forum member could point me in the right direction so I can start learning how to do all of this :D RE: The left-click, right-click parts... is there like a 'macro recorder' such as in Microsoft Excel? Even if it is a bit of a clunky semi-solution to get me started.

TIA for any replies.

CPerry :)

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: New User - Starter Questions

Post by mikeyww » 21 Mar 2021, 10:00

The documentation contains an introduction. Tutorials are also available here on the forum. Some relevant commands will be Send, Click, and possibly MouseClickDrag. The doc contains examples for each command. This forum is also a good source of information; you can search for topics as you wish.

Although you can use SetTimer to run a timer that checks the current time, I believe that the method you described with the Windows Task Scheduler will be simpler and more efficient.

If you get stuck, you can post your scripts here to get feedback from readers.

CPerry
Posts: 14
Joined: 21 Mar 2021, 09:13

Re: New User - Starter Questions

Post by CPerry » 29 Mar 2021, 16:21

Thank you mikeyww,

I've had a little look already at the areas you have suggested but have a few things I still can't work out, for sure. I'm going to spend a few more days learning before crawling back here asking for some more help, I just wanted to say thank you for your reply.

I am a primary teacher in the UK with two parents evenings this week so it'll be Thursday I next get chance to have a look at this but I'm eager to crack it.

Thank you again,

CPerry :)

CPerry
Posts: 14
Joined: 21 Mar 2021, 09:13

Re: New User - Starter Questions

Post by CPerry » 04 Apr 2021, 05:19

I've now had a look over the introduction in the documentation and watched around half a dozen YouTube videos to get me started. I've managed to get Task Scheduler to launch a custom file and it opens up the programme successfully!

Question 1 - The programme I use takes a few seconds to open so at the moment, I am using Sleep 30000 just to give it thirty seconds before the next stage. Is these a more sophisticated alternative such as 'Wait until Programme is open' available?

I then need to type my password into the programme on this first window.

Question 2 - Do I need to download something else to find the exact co-ordinates of the text box or is there a shortcut way of locating the 'second text field from the top of the current window' option?

I'm hoping there's an easy way of clicking on this text field easily but, to be fair, once I have typed my password and pressed enter (how do you do this?), a new window will appear and I have then have to click a specific icon on the next window which is always in the same place. I've seen from my YouTube research others using additional software to find exact co-ordinates. Is this essential?

Thank you again for helping me get started,

CPerry.

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: New User - Starter Questions

Post by mikeyww » 04 Apr 2021, 07:11

You can use WinWaitActive to wait for the window.

It may be possible to send Tab to get to your target field. In any case, Click will occur with coordinates relative to the active window, so if the window's controls have static positions, that would work, too. If needed, WinGetPos can get the window's position on the screen, but this would probably not be necessary for your needs.

CPerry
Posts: 14
Joined: 21 Mar 2021, 09:13

Re: New User - Starter Questions

Post by CPerry » 07 Apr 2021, 03:38

Thank you again mikeyww,

For any other new user reading this, one thing I wasn’t understanding was how I can find out the exact co-ordinates of things on my screen so I know where to click. I wasn’t aware that AutoHotKey comes with another program called WindowSpy (Local Disk (C:)\Program Files\AutoHotKey).

I’ve managed to achieve quite a lot of what I wanted already so thank you very much for the guidance, I have a few more questions though, if I may.

When I open a file explorer window within my trading program to select a spreadsheet, I click on the first file but then I want to use: ‘Windows button + right key’ to select the bottom file in this list. How do I do a combined keystroke? I’ve had a look but I’ve got myself a bit muddled it seems. I have a list of spreadsheets which grows daily so the co-ordinate will always be different, thus the need for a combined keystroke to always select the bottom workbook.

Also, I’m running another bit of code to close everything down at the end of the day. Please could I have some guidance how to do the following:

Select (then right click) the desired template workbook,
Copy desired workbook
Paste desired workbook to the same folder
Select this new workbook
Rename new workbook by clicking right, deleting the 7 furthest right characters and then adding tomorrows date in the following format (YYYYMMDD)

Example:
‘TradingBot’ > ‘TradingBot – Copy’ > ‘TradingBot(YYYYMMDD)’

TIA

CPerry :)

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: New User - Starter Questions

Post by BoBo » 07 Apr 2021, 04:03

I'd prefer to do this using a command-line approach. If you wanna auto-rename files within a specific folder you can do that by identifying the file alphabetically/by (modification/whatever filter) date/ and rename it using :arrow: FileMove/ :arrow: FormatTime. You could trigger that event with a hotkey or fully automated with a :arrow: SetTimer-routine.
HTH. Good luck. :)

User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: New User - Starter Questions

Post by boiler » 07 Apr 2021, 04:30

CPerry wrote: Question 2 - Do I need to download something else to find the exact co-ordinates of the text box or is there a shortcut way of locating the 'second text field from the top of the current window' option?

I'm hoping there's an easy way of clicking on this text field easily but, to be fair, once I have typed my password and pressed enter (how do you do this?), a new window will appear and I have then have to click a specific icon on the next window which is always in the same place. I've seen from my YouTube research others using additional software to find exact co-ordinates. Is this essential?.
A better approach than clicking in a certain location and sending keystrokes to the text entry box would be to address the text entry control directly using ControlSetText to load it with your password. It’s much more reliable because it doesn’t rely on its location and doesn’t send a string of keystrokes. Use the Window Spy tool to see if your application’s window has named controls that can be addressed directly in this way. If it’s a web-based application, then it most likely won’t have separately named controls.

There are various ways of submitting the text either by using ControlSend/Send or clicking an OK or Submit button using ControlClick, depending on the details of the window.

Instead of clicking on your icon’s location, it may be possible to click on it using its control name (if it has one) using ControlClick.

CPerry
Posts: 14
Joined: 21 Mar 2021, 09:13

Re: New User - Starter Questions

Post by CPerry » 08 Apr 2021, 02:21

Thank you both, I'll research control click and control send this morning. Is there a date command I can use though for the text? I want to rename the file with the last part being tomorrow's date in the format YYYYMMDD and do this automatically so I need my system to detect the date and to turn it into text. Is this possible?

CPerry
Posts: 14
Joined: 21 Mar 2021, 09:13

Re: New User - Starter Questions

Post by CPerry » 08 Apr 2021, 02:51

Also, just for my future learning, what does the fn key correlate to on a windows keyboard? I'm running AHK on a windows server through Microsoft Remote Desktop on a MacBook Pro. Inside a folder, I can select a file and then press fn+right to jump to the file at the very bottom of the list. I've tried !, #, ALT, ^ etc, none work. How can I replicate this fn+right with AHK?


CPerry
Posts: 14
Joined: 21 Mar 2021, 09:13

Re: New User - Starter Questions

Post by CPerry » 09 Apr 2021, 02:45

Ahhh, it seems I've run into the same problem as other forum members. It's strange because if I manually hold the fn key and press the right arrow, my server selects the last file in the file explorer window so it must detect the key. Is there a windows alternative method that works with eligible keys because I can't just press tab?

Alternatively, if the 'wrapping up' code from the previous evening creates a file called exp: 'TradingBot(YYYYMMDD)', is there a way of - once the correct folder is opened - selecting the workbook with today's date and opening it? Today for example is TradingBot20210409. Could somebody share a snippet of code that would open today's please so I can learn from it?

CPerry :)

lexikos
Posts: 9556
Joined: 30 Sep 2013, 04:07
Contact:

Re: New User - Starter Questions

Post by lexikos » 09 Apr 2021, 04:11

My guess: when you physically press Fn+Right, the keyboard sends the key code for End. That keyboard probably doesn't physically have an End key.

In other words, you don't want to send Fn+Right; you want to send {End}, which is typically the right key for moving the selection to the end of a list.

The "Fn" key is not something Windows has any direct support for, even though Microsoft uses it in their own keyboards. It is manufacturer-specific, usually won't produce any directly usable keyboard event, and will usually combine with other keys to cause them to produce a totally different set of events to what they normally produce. For instance, the F3 and F4 keys on my Microsoft keyboard produce Ctrl+Win+Left/Right when the Fn switch is in the "blue" position.

User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: New User - Starter Questions

Post by boiler » 09 Apr 2021, 07:04

CPerry wrote:
09 Apr 2021, 02:45
Alternatively, if the 'wrapping up' code from the previous evening creates a file called exp: 'TradingBot(YYYYMMDD)', is there a way of - once the correct folder is opened - selecting the workbook with today's date and opening it? Today for example is TradingBot20210409. Could somebody share a snippet of code that would open today's please so I can learn from it?
If files of this type can be opened by double-clicking them because of file type associations, the better way to open it would not be by selecting it from a File Explorer window open to the right directory but to use Run to open the file similar to below. I imagine that there is some file extension that these files have that you're not showing, possibly because your setting for File Explorer is to not show the extensions for known file types. Is that true? If so, what is the file extension? The code below shows it as if it were an Excel workbook. Perhaps they actually are Excel files (?).

Code: Select all

Yesterday := A_Now
Yesterday += -1, days
Yesterday := SubStr(Yesterday, 1, 8)
Run, C:\PathToMyFiles\TradingBot(%Yesterday%).xlsx ; need to use the actual path and the correct extension

CPerry
Posts: 14
Joined: 21 Mar 2021, 09:13

Re: New User - Starter Questions

Post by CPerry » 09 Apr 2021, 17:20

Evening lexikos,

Thank you! This works a treat as a workaround. I was hoping there would be some connection from a signal-sense; I have now completed my setup code. My skills may be a bit shabby with a few sleep, 5000’s in there to give my server time to think but I’m chuffed with getting to the half way point!

Hi boiler,

Basically the trading program I am using has a button called ‘Browse Files’ and once clicked it force opens a window for me to select a trading workbook I have created and then connects it to do some live automated trading. I have used lexikos’ workaround successfully to attach the workbook but I’m going to try it your way now for the ‘WrapUp’ code now.

I’ll start with:
Run, C:\Users\Administrator\Desktop\TradingBots

This opens up a window with my daily bots in. At the end of the day, I want to automatically select the file:

TradingBotLiveV1.1.3

Copy it, paste it, select this new copy version and rename it to:

TradingBotLiveV1.1.3(YYYYMMDD) as TOMORROWs date.

I'm afraid I haven't understood your last message? Are you creating a variable called 'Yesterday' (so tomorrow would be +1 instead of -1)? Instead of running the workbook with my end of the day wrap-up code, I want to select a template workbook and rename it as laid out above. Greatly appreciating all the guidance so far on this forum, thank you again!

Little more help with this last part please? Select, copy, paste, select new file, rename with specific naming convention.

TIA.

CPerry :)

User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: New User - Starter Questions

Post by boiler » 09 Apr 2021, 17:46

CPerry wrote: I'm afraid I haven't understood your last message? Are you creating a variable called 'Yesterday' (so tomorrow would be +1 instead of -1)? Instead of running the workbook with my end of the day wrap-up code, I want to select a template workbook and rename it as laid out above.
Yes, I thought you were trying to access a file the following day with a wrap up of yesterday and named with that date. If you want tomorrow, then yes, just change it to +1 (and it would make sense to call the variable Tomorrow).
CPerry wrote: Little more help with this last part please? Select, copy, paste, select new file, rename with specific naming convention.
Again, interfacing with the File Manager to select files, select copy, paste, rename, is not the way to go. Just have the script work with the files directly, not with the window that displays the files for a human to interact with. It sounds like you just want to copy the file and give it a new name, right? You should use the command FileCopy using the name of the template for the "SourcePattern" and the new name with the date you generated for the "DestPattern".

CPerry
Posts: 14
Joined: 21 Mar 2021, 09:13

Re: New User - Starter Questions

Post by CPerry » 11 Apr 2021, 03:44

Morning boiler,

My plan is to access the template file at 10pm, once my automated trading is finished for the day, and then copy it to the same folder with tomorrow’s date added to the end ready for tomorrow’s trading.

I have had a go at using FileCopy but I just get a little error pinging noise when I perform the keystroke and I have checked my test folder, there is nothing there. When I right click in an empty space it doesn’t give me an option to paste either so I have definitely done something wrong. I created a folder on my desktop named ‘FolderTest’ and put an excel workbook in there. I then ran the following code:

Code: Select all

^q:: FileCopy, C:\Users\[MYNAME]\Desktop\FolderTest\TemplateTestFolderTest.xlsm, C:\Users\[MYNAME]\Desktop\FolderTest\TemplateTestFolderTestNEW.xlsm
According to the FileCopy link you sent, this should match example 2 and copy a file into the same directory with a new name, should it not? I haven’t even got to adding in the tomorrow’s date variable yet. Probably doing something dumb :headwall: .

Also, is there a way of stopping the code mid-routine? I noticed my setup code went buggy this morning and as I was testing I was hitting the esc key (like Microsoft VBA has to cancel) but the script continued.

User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: New User - Starter Questions

Post by boiler » 11 Apr 2021, 05:20

CPerry wrote:

Code: Select all

^q:: FileCopy, C:\Users\[MYNAME]\Desktop\FolderTest\TemplateTestFolderTest.xlsm, C:\Users\[MYNAME]\Desktop\FolderTest\TemplateTestFolderTestNEW.xlsm
According to the FileCopy link you sent, this should match example 2 and copy a file into the same directory with a new name, should it not?
Yes, that exact script works when I create the same file/folder and run it when I put my username in in place of [MYNAME]. Since it didn't work, I have to ask you: Did you press Ctrl+Q after you ran the script to invoke the hotkey you assigned?

What does the following script display when you run it?

Code: Select all

MsgBox, % FileExist(A_Desktop "\FolderTest\TemplateTestFolderTest.xlsm") ? "File found" : "File not found"

CPerry wrote: Also, is there a way of stopping the code mid-routine? I noticed my setup code went buggy this morning and as I was testing I was hitting the esc key (like Microsoft VBA has to cancel) but the script continued.
Yes, add the following line to the bottom of your script, and you can end the script at any time by pressing Esc:

Code: Select all

Esc::ExitApp
You are still able to end the script by right-clicking on the script's icon in the system tray and selecting "Exit", right?

CPerry
Posts: 14
Joined: 21 Mar 2021, 09:13

Re: New User - Starter Questions

Post by CPerry » 23 Apr 2021, 17:34

Good evening boiler,

Hmmm. The code you wrote for me returns, “File Found” and then strangely, when I ran the snippet from my last message it started working all of a sudden. Perhaps restarting my machine?

I’m a primary school teacher so have been incredibly busy this week, apologies for not thanking you sooner for your last message. Somehow, it now works so tomorrow I need to have a look at where I left off and will probably add some more to this thread a little later.

Again, thank you for your help so far.

User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: New User - Starter Questions

Post by boiler » 23 Apr 2021, 19:01

Glad to help a teacher. I come from a family of teachers (although not one myself).
CPerry wrote: Hmmm. The code you wrote for me returns, “File Found” and then strangely, when I ran the snippet from my last message it started working all of a sudden. Perhaps restarting my machine?
Often, we want to blame computer error for things like this, but there isn’t much that a computer restart would fix in that code if run exactly the same before and after. It may have been something like pressing the wrong key combination for the hotkey or running the script file before saving the latest changes. The important thing is it’s working now.

Post Reply

Return to “Ask for Help (v1)”