Autohotkey daily macro doesn't work well - autohotkey

I created a macro in autohotkey that is able to copy at 7:40 a.m. the last file created in a shared disk into a dropbox folder. When I launch the macro to try it, setting the "time to meet" 2 minutes later for example, it works perfectly. The problem is that the day after the macro doesn't start. Could you help me please?
Thanks
Marco
SetTimer, Chronos, 59900
Return
Chronos:
FormatTime, TimeToMeet,,HHmm
If TimeToMeet = 740 ; If you wanted the script to start at 7 am put change 1006 to 700
{
run O:\research\
winactivate, research
sleep 1000
MouseClick, left, 289, 586
send {PgDn 6}
clipboard =
Send ^c
clipwait
sleep, 1000
FileCopy, %clipboard%,C:\Dropbox\
sleep 2000
winclose research
return
}
Return

Unattended user interface automation like this may not be the most reliable aproach.
I would recommend using the Windows Task Scheduler to handle launching the process. I think this might be safer than having the script running 24 / 7 waiting to go. Even more importantly, it looks like you are doing very basic file manipulation by automating the UI. This type of work may be better acomplished with a Windows batch file or Autohotkey's functions for files. Note that batch files are less fussy about screensavers and being logged in. I love AutoHotkey, but that seems to be a weak spot. Check out the documentation for each of the functions that start with the word file. I'd be surprised if you couldn't hook some of those up to do what you need. Since you seem to be looking for a file, check this one out:
http://www.autohotkey.com/docs/commands/LoopFile.htm
I think this thread may be of help as it finds the most recent file in a folder:
http://www.autohotkey.com/board/topic/57475-open-most-recent-file-date-created-in-a-folder/
Good luck!

Related

Issue using ControlClick and WinTitle

I'm new to the AHK and trying to make a script to click on a coordinate in a specific window, once per minute.
I already used the WindowSpy to get the coordinates and the WindowTitle, but i can't get to work
Right now the script is:
ControlClick, x469 y363, ahk_pid 11532
Am i missing something?? Thanks!
edit1: I changed the script to this and tested in another window like Excel and it worked, but in the game that it is supposed to work, it doesnt, no idea why
ControlClick, x466 y364, ahk_pid 11532,,,, Pos
sleep 60000
}
Not sure if you just pasted in that code badly, or it really looks like that, but a simple loop can be used to achieve that. (Possibly not the most ideal thing to use, depends on if you want the script to do something else as well)
Loop
{
ControlClick, x466 y364, ahk_pid 11532,,,, Pos NA
Sleep, 60000
}
Also I recommend at least trying out the NA option, it's documented as possibly improving reliability.
And about it not working in some game you're trying, it's very much expected. More often than not games will receive input in some way which makes control clicking not work. For example receiving direct input.
There's not much you can do about that, and it can get really complicated/advanced.
Again, I don't know what what sort of game you're talking about, but going into windowed mode (assuming fullscreen even is a thing for the game) and actually moving the cursor and then clicking might work. But that of course defeats the purpose of even using ControlClick in the first place..
The answer from #0x464e is good if you're only running one process within the script. If want to run multiple processes use the SetTimer function.
SetTimer, ClickOneMin, 60000
return
ClickOneMin:
ControlClick, x466 y364, ahk_pid 11532,,,, Pos NA
return
This will allow you to add additional loops, timers and processes.

AutoHotkey to use clipboard info to open files in Word works in Win8 but not in Win7

I created an AHK macro for a colleague. I have Windows 8; she has Windows 7. She has two "pending work" folders, one for documents and the other for audio files. The (text) file names in the documents folder correspond to the file names in the audio folder. So, for example, if there is an audio file called abc123.mp3 in the audio folder, there will be a file named abc123.txt in the documents folder.
As she opens the audio file in her transcription (media player) app, as part of that process, I want to have an AHK macro automatically open the matching text file (in Word).
I created an AHK macro that picks up from the point of her selecting the file to open from her transcription app. At the point where she selects the file, she invokes the AHK shortcut, which then toggles the rename feature (F2), copies the text to the clipboard (Ctrl+C), opens the file (Alt+O), and then uses the clipboard info (along with path info) to open the appropriate text file in Word.
It's working PERFECTLY on my system. However, when I logged into her system earlier today to demo the macros, the macro would not work. It works to a point - it will rename the file, copy the name of the file, and open the audio file, and file name is in the clipboard. But that's where it dies, with no error message. During troubleshooting, I tried isolating the macro down to one command - to simply open (fixed name) file in WinWord.exe, but that doesn't work, either.
I have been unable to find any research related to this issue. I know there are numerous ways to accomplish this task, but my AHK scripting skills and my free time are limited, so I went with what I knew I could accomplish quickly. I am open to suggestions for how to troubleshoot or tweak this macro to get it working on her system. It's hard to troubleshoot when it works on mine!
NumpadEnter::
Send {F2}{Sleep 100}
Clipboard=
Send ^c
Send {Sleep 100}!o{Sleep 100}
Run, WinWord.exe "C:\DocFiles\"%Clipboard%.txt
Return
Just a guess, but I believe you wanted Sleep command and not a Sleep Key? Be sure that you are using the Latest Version of AutoHotkey you can download it from www.ahkscript.org
NumpadEnter::
Send {F2}
Sleep, 100
Clipboard=
Send, ^c
Run, WinWord.exe "C:\DocFiles\"%Clipboard%.txt
Sleep 100
Send, ^o
Sleep 100
Send, ^v
Sleep 100
Send, !o
Return
Edit:
In the script you posted, you are telling the computer to Send a Sleep Key by placing brackets around the word Sleep. Like so {Sleep}
Furthermore, you are also telling your script to not just Send one key... you are telling it to Send the Sleep Key 100 times by placing the number 100 in the brackets, separated by a space, with the named key. Like so: {Sleep 100}
I fail to see how you would want to have your script simulate 100 Sleep key presses?
What I posted above uses the Sleep Command which delays the script in milliseconds. I believe this is truly what you want.

Autohotkeys: Wait till the previous command is executed fully

What I am trying to do here is open an Excel sheet and do some copy paste here and there. I have created a script which will open the file and then start operations on it. Now I am using a slow computer so it takes time for excel file to open. Is it possible to somehow tell my autohotkey script that the file is opened and now u can start your shit. I know I can do it with sleep function but I was wondering if there is something better.
I would first check if Excel is running, then check if the specific file has been opened.
SetTitleMatchMode, 2
IfWinActive, Microsoft Excel
{
WinGetTitle, title, A
IfInString, title, specific file
{
Sleep, 1000 ; Just in case....
}
}
Actually, you are better of with a WinWait, specific file

Hotkey to restart autohotkey script?

Say I have an autohotkey script C:\path\to\my\script running. Is there a way to define a hotkey that re-starts it?
In order to prevent duplicate instances, I normally do not re-launch a script but use the build-in function Reload. I launch this with Ctrl+Win+Alt+R and use Ctrl+Win+Alt+E to edit the main AHK script.
^#!r::Reload
Actually, my script looks like this:
^#!r::
Send, ^s ; To save a changed script
Sleep, 300 ; give it time to save the script
Reload
Return
^!#e::Edit
As a matter of fact, all the way at the top of my script I have this to give me a visual and audio indication that the script was restarted:
#SingleInstance Force
#installKeybdHook
#Persistent
Menu, Tray, Icon , Shell32.dll, 25, 1
TrayTip, AutoHotKey, Started, 1
SoundBeep, 300, 150
Return
Make a hotkey that runs a script, which in this case is the same script and then exit.
somehotkey::
Run, C:\path\to\my\script.ahk
ExitApp
return
I found this to be the safest option of them all, because it takes care that the correct script is reloaded when you have multiple scripts running simultaneously, which was a recurring issue for me. The combination of the following also ensures that only one instance of a script will ever run at a time. The ScriptFullPath variable includes the name of the script.
#SingleInstance Force ;put this at the top of the script
^r::run, %A_ScriptFullPath%

How to simulate key press on a background window

I need to force Outlook which is in the background, to check for new emails every 2 seconds. So I wrote a following script but it doesn't do it. What's wrong? I don't want the script to disturb what I do and give the focus to the Outlook window. The "ahk_class rctrl_renwnd32" is correct, I checked it with "WinActivate, ahk_class rctrl_renwnd32" and it worked.
Loop
{
ControlSend,, {F9}, ahk_class rctrl_renwnd32
Sleep 2000 ; Wait 2 seconds
}
There is no error in your code. The problem might be in the receiving application. When I test this in Notepad, an F5 (Print time & date) is executed when the Notepad window is open somewhere on my second screen. As soon as I minimize Notepad, it will no longer execute F5, but it still accepts a string like A{Enter}B{Enter}C{Enter} when minimized.
!q::
ControlSend,, {F5}A{Enter}B{Enter}C{Enter}, ahk_class Notepad
Return
Solution, Try if this works when you keep the window on screen somewhere (no need to have the focus).
I have used Outlook in the past and remember that F9 took some time to execute. Running this every 2 seconds looks like overkill.
If getting your e-mail in time is THAT important that you are willing to "kill" the mail server with refresh requests, I would discuss a solution with your IT support.