Choose menu option then exit program Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
blixel
Posts: 2
Joined: 30 Dec 2015, 04:10

Choose menu option then exit program

07 May 2017, 12:52

I'm working on a script that will allow me to choose an option from the menu bar for the purposes of exporting the current page as a PNG image. Once the export is complete, the program will exit. The script I have works about half way, but I can't seem to figure out how to complete it.

The general idea is...
  • When the hotkey is pressed, make sure I'm in the right program. If not, do nothing. This isn't working.
  • Press the ALT key, press the down arrow 11 times, press the right arrow 2 times, press enter twice. This is the part that exports the current page as a PNG file. This is working.
  • After export, press the ALT key, press the down arrow 13 times, press enter once. This will exit the program. This worked a few times, but eventually stopped working and I'm not sure why.
I attached a screenshot showing the menu tree selection. Below is my code. Can someone help me sort this out?

Code: Select all

^j::
   ; Make sure the Fritzing Window is active. If not, just exit so that
   ; we don't send these commands to whichever program happens to have
   ; focus.
   ; -----------
   ; NOT WORKING
   ; -----------   
   if !WinExist("ahk_exe Fritzing.exe") {
	;wrong program is focused, return and do nothing
	return
   }
   
   ; Wait 1 second for the user to let go of the keys
   Sleep, 1000
   
   ; Press ALT to activate the MENU BAR
   send, {alt down}{alt up}
   
   ; Wait 100th of a second 
   Sleep, 10
	
	; Press the DOWN ARROW 11 times to get to the EXPORT SUBMENU
	Loop, 11 {
		send, {Down}
	}
   
   ; Press RIGHT twice to go to the IMAGE SUBMENU
   send, {Right}
   send, {Right}
   
   ; Press ENTER to select the first option, which is PNG
   send, {enter}

   ; Wait a 10th of a second for the menu to come up
   Sleep, 100
   
   ; Press ENTER to save the file using the default file name
   send, {enter}
   
   ; Wait for the computer to save the file
   Sleep, 350
   

   ; If the file does already exist, a warning window will pop up. If that
   ; warning window does NOT popup, then exit the program. If the warning
   ; window does popup, do nothing. The user will have to decide what to do.
   ; -----------
   ; NOT WORKING
   ; -----------   
   if !WinExist("ahk_class #32770") {
   	Sleep, 10
	send, {alt down}{alt up}
	Sleep, 10
	Loop, 13 {
		send, {Down}
	}
	send, {enter}
   }
	
     
return
Attachments
2017-05-07_13-47-15.jpg
2017-05-07_13-47-15.jpg (34.4 KiB) Viewed 1231 times
2017-05-07_13-39-17.jpg
2017-05-07_13-39-17.jpg (66.29 KiB) Viewed 1231 times
theartofx

Re: Choose menu option then exit program  Topic is solved

07 May 2017, 15:24

Code: Select all

   if !WinExist("ahk_exe Fritzing.exe") {
	;wrong program is focused, return and do nothing
	return
Above, you're verifying that the Window does not exist (!WinExist), not that it's not focused. It's good to verify the window actually exists and return if it doesn't, but to verify if it's active needs and extra or alternate step. You should use IfWinActive, IfWinNotActive, or WinActive() to verify it's focused.

Code: Select all

   if !WinActive("ahk_exe Fritzing.exe") {
	;wrong program is [b]focused[/b], return and do nothing
	return
Next, wouldn't it be simpler to just close the program? You could use WinClose to kill the window. You could also use WinKill to forcefully close it. If you want to wait out the dialogue, it might also be to just send Ctrl + Q to Quit the program, based on your screenshot, but here are two simplified versions of what you could do. First is to send Alt then to send Down arrow 13 times. Second is to send Ctrl+Q. You may also want to increase the sleep timer directly before performing these sends to ensure you're not pressing or releasing any other keys, maybe 100-300ms?

Code: Select all

Send {alt}
Send {down 13}
Send {Enter}

Code: Select all

Send ^q ;Performs Ctrl+Q keystroke
User avatar
blixel
Posts: 2
Joined: 30 Dec 2015, 04:10

Re: Choose menu option then exit program

07 May 2017, 20:03

You should use IfWinActive, IfWinNotActive, or WinActive() to verify it's focused.
Ah, I see. I changed it from !WinExist to !WinActive and now it does seem to ignore the script if I'm not in Fritzing. So that part is working now, thanks.

Code: Select all

Send ^q ;Performs Ctrl+Q keystroke
In the part where I had

Code: Select all

if !WinExist("ahk_class #32770") {
... I also changed that to !WinActive, and changed the complex loop into Send ^q, and it is closing the program now. Does WinClose() do the same thing as Send ^q? Seems like it would be more robust to call a function like WinClose() than to send a keystroke.

I also went to the previous section and changed the loop to just Send {down 11}, and that is still working. Much simpler than setting up a loop.

So I think it's working perfectly now.

The next evolution is that I want to see if I can automate this even further. I have about 15 to 30 of these Fritzing files in a given directory, and I currently have about 7 directories. (Each directory contains its own distinct set of schematics.) If I could run 1 script on a given directory that would open each Fritzing file, export the PNG, close Fritzing, open the next Fritzing file, export the PNG, and repeat until every file had opened and exported, that would be the most ideal version. But I'm not sure if that will get too complex for me to handle.

At any rate, I definitely appreciate the quick response to my original post. Very helpful!
User avatar
theartofx
Posts: 15
Joined: 07 May 2017, 15:50

Re: Choose menu option then exit program

07 May 2017, 20:49

WinClose would honestly be the better option, in my opinion. It would be less error prone by all logical accounts. It calls the program to gracefully close without forcefully killing the process. In theory, and I assume in practice, it is effectively the same as sending ^q without sending ^q. From the help file for WinClose:
This command sends a close message to a window. The result depends on the window (it may ask to save data, etc.)

If a matching window is active, that window will be closed in preference to any other matching window beneath it. In general, if more than one window matches, the topmost (most recently used) will be closed.
Edit:
I would also like to note that it is very possible to do what you entailed at the end of your last reply. It would simply require some time spent coding and you parsing the help file for commands that match what you're trying to do. The Run command could handle opening a file, general automation previously discussed would handle exporting it and closing Fritz, and then repeating. This may require a loop and checks on each file, so there would be a need for strict logic involved. I would suggest setting up a faux directory with appropriately named text files, and doing something with them, such as opening the text file, sending a couple of keystrokes, then closing the file. That way, you don't manage to accidentally trash the actual files you're legitimately working on. Once you have the concept down with the text files, you would just make minor modifications to make it work with the Fritz files/program.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Choose menu option then exit program

08 May 2017, 12:32

WinMenuSelectItem might be useful if it's a standard menu bar, otherwise ...

This script might be useful:
[a ToolTip appears showing menu item IDs for context menus]
Get Info from Context Menu - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/1975 ... text-menu/

E.g. the menu item ID for 'Save' in Notepad is 3:
- PostMessage sends a message.
- SendMessage sends a message and waits for a response.

Code: Select all

q:: ;Notepad invoke menu item
WinGet, hWnd, ID, ahk_class Notepad
PostMessage, 0x111, 1, 0,, % "ahk_id " hWnd ;WM_COMMAND := 0x111 ;New
;SendMessage, 0x111, 3, 0,, % "ahk_id " hWnd ;WM_COMMAND := 0x111 ;Save
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 170 guests