Run a program automatically on script start-up - autohotkey

I am trying to make an AHK script open another program everytime it is started. The problem is, I don't want this to happen if that other program is already opened.
Here's what seems to be supposed to be working, but isn't : (this section is placed at the very top of my script)
SetTitleMatchMode, 2
#IfWinNotExist, Microsoft Excel - myExcelFile.xls
Run C:\myExcelFile.xls
#IfWinExist
;REST OF MY SCRIPT GOES HERE
What should be happening :
If the window "Microsoft Excel - myExcelFile.xls" is not opened, run it. If not, do not.
What is happening :
Whether it is opened or not, it will try to run it again.

So yeah, even though I had read the documentation, I had understood that the difference between #IfWin and IfWin was whether they were used inside or out of a specific hotkey. To my understanding, "creates context-sensitive hotkeys and hotstrings" also included "context-sensitive auto-execution" (when code is not inside a hotkey)
Indeed I was wrong and the solution is to remove the #.

Related

How to install/run autohotkey without admin privileges?

I followed this tutorial: http://www.thenickmay.com/articles/how-to-install-autohotkey-without-admin/
At Step #6, I opened AutoHotkeyU64.exe and it opened up the help file for AutoHotKey.
Then I went through the tutorial and tried out the script they give:
^j::
Send, My First Script
return
I saved this as tutorial.ahk, then opened a new notepad file and pressed ctrl+j. Nothing happened.
Anyone know what's going on? I've never used autohotkey before, btw.
Since I was able to install it normally, I can't say for certain, but I believe there is a skipped step (or two) in the instructions.
After step 5, run (double-click) your .ahk script. If you get a message asking
"How do you want to open this file", choose "More apps", then "Look for another app on this PC" (see image below), then navigate to and choose AutoHotkey.exe or AutoHotkeyU64.exe. Be sure to keep the checkbox checked to "Always use this app to open .ahk files" so that you only need to do this once on your machine.
I am late to the party and dunno if anyone's left drinking here, but I want to run ahk just to fix that darn copy/paste function on cmd/erp ... Hope this works

" #SingleInstance force " is being ignored

SingleInstance force
is supposed to prevent the popup when I tell the script to reload... but it doesn't. I'm still getting the warning that a copy of the script is already running. Thoughts?
Without seeing your code, there are a few things I can think of that might be interfering. The first is that there may be a typo. Please verify that it is exactly #SingleInstance force. Another thing could be that it is not placed near the beginning of the script; possibly after a Return. Finally, from the help file,
AutoHotkey relies on the title of the script's main window to
identify other running instances of the script.
Is this script calling another script or is there anything the might affect the script name? If none of these things helps, it might be a good idea to post your code or at least a portion of it so we can better see what's going on.

Script runs fine in ISE, but not in Powershell Console

I wrote an extremely lengthy script that creates a form. The main purpose of it it to connect to vCenter servers, total up hosts/vms within different datacenters and also identify ESXi host versions. Running the script in ISE the form loads fine and when I click the Button to start the many various functions to update the fields, it runs. I have Foreach() fields throughout and added in a little StatusLabel to the bottom of the form to show me where in the script it is currently scanning.
Once all was done and working, I saved the PS1 file and moved it to a shared server to be run. When I run the Powershell console (as Admin) and point to the file, the Form loads ok. Once I click the button to begin generating the info, It just changes to (Not Responding) and grays out. I know it's working and if left there long enough it will return the info.
Why does the console window not function like the ISE? I want to be able to see the information and Status Label update/change as it goes through the script (just like I do in the ISE).
Appreciate the feedback. The script is several thousand lines long and would need to be scrubbed before I upload. Was hoping to just get some general info before doing that.
I updated to v4 and verified that my command window was running as STA using the command from Jan Chrbolka. Script runs properly now.
I was also facing the same issue.(Btw, I cannot upgrade Powershell to V4 right now since it is my company owned one.)
The Browse button that I incorporated in my form was not working when I am lauching it from Powershell command line. It went to Not Responding state when I click the browse button.
I took the advise from Jan ChrbolkaMay about the STA and MTA.
I checked for the threads related to that and changed the script execution format to the below one which fixed my issue.(adding the word -STA)
powershell -STA -File D:\PS\GUI.ps1
For checking the current state. Use the below query in Commandshell as well as ISE.

How can I know whether process has a windows or not?

Sometimes my program is finished incorrectly. GUI is gone but processes still present in the system. So I need to verify whether the program finishes correctly or not by checking GUI presence. Could anyone help me with it?
It looks like what you need is WASP.
You can use WASP's Select-Window to list all window handles.
You can then use Select-Window Myapp* to set focus on the window, if required.

Is there an AutoHotkey REPL?

I know that you can run and live-reload ahk scripts. And I've seen the scintilla-based editor that provides debugging. But, is there any kind of command-line-based REPL?
I was testing the statement to get the active window's process name and thought it would really helpful!
ahk> WinGet, active, ProcessName, A
powershell.exe
This may also be something like what you're looking for
Forum Topic link
GitHub link
Hope it helps
I open my AHK script NotePad (or Vim, or whatever). Then I add my test code as the first line. I have a hotkey defined to reload the script.
MsgBox, Hello World
^+r::
Reload
Return
So I edit, then Ctrl-s, then Ctrl-Shift-R, test code runs. Works pretty well.