BobbyH

First Script Woes...

11 posts in this topic

Hello AutoIt users! My name is Bobby, and I'm extremely new to all that is AutoIt. After browsing at least 50+ topics, various websites, YouTube videos and trying to apply and understand what's in the help file, I still found myself stuck...

Granted for a first timer, I think what have isn't too shabby (if I might be so bold lol) so far, but like everything else I work on, this started out as a simple "I just need it to do this" project. However, after putting some thought into it, I came up with all these neat features I'd like to implement. Some of which may or may not happen now, depending on how much help you good folks can provide me. :)

Anyway, now to what this thing I'm working is: I bought a Digital Loggers Web Power Switch 7 not too long ago. And I was intrigued with the idea of remote controlling it via not only through the internal webserver, but also integrated scripting using BASIC. I had no idea what that was, and up until yesterday, the most I'd have messed with were Windows command line and HTML (at beginner levels).

So, I spent all day yesterday learning how that worked, setting it up and picking up a few other neat programs along the way (cURL, Koda, and of course AutoIt). So, once I set up the web power switch scripts, I decided to use cURL in a batch file to run those scripts. The problem was, I'd have to have a bunch of .bat files in a folder to do what I wanted. I didn't want to deal with that as it just wasn't as "clean" of a setup as I'd like. So that's when I turned to Koda and AutoIt to see about creating a GUI that would allow me to run those scripts from one location. This is what I've come up with so far (to be honest most of this is just adapted copy and pasting, cause I'm still very new to this.. :sweating: :

#include <INet.au3>
#include <GuiButton.au3>
#include <GuiToolBar.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <IE.au3>

Opt("TrayOnEventMode", 1) ; Use event trapping for tray menu
Opt("TrayMenuMode", 3) ; Default tray menu items will not be shown.

TrayCreateItem("Fail-Safe PC Power ON")
TrayItemSetOnEvent(-1, "Fail-Safe PC Power ON")
TrayCreateItem("")
$hTray_Show_Item = TrayCreateItem("Hide")
TrayItemSetOnEvent(-1, "To_Tray")
TrayCreateItem("")
TrayCreateItem("Exit")
TrayItemSetOnEvent(-1, "On_Exit")



; Use the Autoit window as a parent to prevent appearing on the taskbar
$hGUI = GUICreate("Digital Loggers Web Power 7 Script Initializer", 410, 379, 205, 355, Default, Default, WinGetHandle(AutoItWinGetTitle()))




#Region ### START Koda GUI section ### Form=c:\users\bobby hurman\documents\autoit projects\forms\test.kxf
;(original values)$_1_1 = GUICreate("Digital Loggers Web Power 7 Script Initializer", 410, 379, 205, 355)
$Pic = GUICtrlCreatePic("C:\Users\Bobby Hurman\Documents\AutoIt Projects\Forms\background-dark.jpg", -1, 0, 411, 379)
GUICtrlSetState(-1, $GUI_DISABLE)
GUISetBkColor(0xFFFFFF)
$Button1 = GUICtrlCreateButton("Fail-Safe PC Power ON", 144, 8, 129, 33, $BS_DEFPUSHBUTTON)
GUICtrlSetTip(-1, "Automatically Reboots a device, displays the results, and sends a SYSLOG report (if configured) ")
$Button2 = GUICtrlCreateButton("Power Cycle PC", 152, 48, 113, 33)
GUICtrlSetTip(-1, "Power cycles Bobby Hurman-PC")
$Button3 = GUICtrlCreateButton("Power OFF PC", 152, 80, 113, 33)
GUICtrlSetTip(-1, "Powers off Bobby Hurman-PC")
$Button4 = GUICtrlCreateButton("Display Date and Time", 152, 120, 113, 33)
GUICtrlSetTip(-1, "Displays date and time")
$Button5 = GUICtrlCreateButton("Displays Outlet States", 152, 152, 113, 33)
GUICtrlSetTip(-1, "Displays outlet states in numberical form ")
$Button6 = GUICtrlCreateButton("Monitor ON", 152, 192, 113, 33)
GUICtrlSetTip(-1, "Powers on monitor")
$Button7 = GUICtrlCreateButton("Monitor OFF", 152, 224, 113, 33)
GUICtrlSetTip(-1, "Powers off monitor")
$Button8 = GUICtrlCreateButton("Public Listening ON", 88, 264, 113, 33)
GUICtrlSetTip(-1, "Powers on mixer, and then each monitor sequentially with a 2 second delay")
$Button9 = GUICtrlCreateButton("Public Listening OFF", 88, 296, 113, 33)
GUICtrlSetTip(-1, "Powers off mixer, and then each monitor sequentially with a 2 second delay")
$Button10 = GUICtrlCreateButton("Private Listening ON", 216, 264, 113, 33)
GUICtrlSetTip(-1, "Powers on only mixer for (headphone use)")
$Button11 = GUICtrlCreateButton("Private Listening OFF", 216, 296, 113, 33)
GUICtrlSetTip(-1, "Powers off only mixer for (headphone use)")
$Button12 = GUICtrlCreateButton("Outlet Control", 16, 60, 120, 40, $BS_BITMAP)
GUICtrlSetImage(-1, "C:\Users\Bobby Hurman\Documents\AutoIt Projects\Forms\digitalloggers_logo_2.bmp", -1)
GUICtrlSetTip(-1, "Web Power Switch 7 internal web server")
GUICtrlSetCursor (-1, 0)
$Label1 = GUICtrlCreateLabel("Created by:          Bobby Hurman", 0, 336, 136, 19)
GUICtrlSetFont(-1, 8, 400, 0, "Arial Narrow")
GUICtrlSetBkColor(-1, 0x808080)
$Label2 = GUICtrlCreateLabel("Copyright 2014: Bobby Hurman", 0, 353, 136, 19)
GUICtrlSetFont(-1, 8, 400, 0, "Arial Narrow")
GUICtrlSetBkColor(-1, 0x808080)
$Label3 = GUICtrlCreateLabel(" v.0.1.6", 375, 353, 37, 19)
GUICtrlSetFont(-1, 8, 400, 0, "Arial Narrow")
GUICtrlSetBkColor(-1, 0x808080)



GUISetState()
;(original values)GUISetState(@SW_HIDE)
#EndRegion ### END Koda GUI section ###


While 1
    $nMsg = GUIGetMsg()
    If $nMsg=$Button1 Then Button1 ()
       If $nMsg=$Button2 Then Button2 ()
          If $nMsg=$Button3 Then Button3 ()
             If $nMsg=$Button4 Then Button4 ()
                If $nMsg=$Button5 Then Button5 ()
                   If $nMsg=$Button6 Then Button6 ()
                      If $nMsg=$Button7 Then Button7 ()
                         If $nMsg=$Button8 Then Button8 ()
                            If $nMsg=$Button9 Then Button9 ()
                               If $nMsg=$Button10 Then Button10 ()
                                  If $nMsg=$Button11 Then Button11 ()
                                     If $nMsg=$Button12 Then Button12 ()

    Switch $nMsg
    ;(This command was slow to respond) Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $GUI_EVENT_MINIMIZE
            To_Tray()
        Case $Button1
            Fail_Safe_PC_Power_ON()
    EndSwitch
 WEnd

;Commands for application while minimized to tray


Func To_Tray()

    If TrayItemGetText($hTray_Show_Item) = "Hide" Then
        GUISetState(@SW_HIDE, $hGUI)
        TrayItemSetText($hTray_Show_Item, "Show")
    Else
        GUISetState(@SW_SHOW, $hGUI)
        GUISetState(@SW_RESTORE, $hGUI)
        TrayItemSetText($hTray_Show_Item, "Hide")
    EndIf

EndFunc

Func Fail_Safe_PC_Power_ON()
    If $nMsg=$Button1 Then Button1 ()

EndFunc

Func On_Exit()
    Exit
EndFunc

;Button functions that coorespond to cURL batch files

Func Button11 ()
Local $iPID = Run ("C:\Users\Bobby Hurman\Documents\AutoIt Projects\Forms\Batch Files\Private Listening OFF.bat", "", @SW_HIDE)
EndFunc

Func Button6 ()
Local $iPID = Run ("C:\Users\Bobby Hurman\Documents\AutoIt Projects\Forms\Batch Files\Monitor ON.bat", "", @SW_HIDE)
EndFunc

Func Button8 ()
Local $iPID = Run ("C:\Users\Bobby Hurman\Documents\AutoIt Projects\Forms\Batch Files\Public Listening ON.bat", "", @SW_HIDE)
EndFunc

Func Button9 ()
Local $iPID = Run ("C:\Users\Bobby Hurman\Documents\AutoIt Projects\Forms\Batch Files\Public Listening OFF.bat", "", @SW_HIDE)
EndFunc

Func Button5 ()
Local $iPID = Run ("C:\Users\Bobby Hurman\Documents\AutoIt Projects\Forms\Batch Files\Displays Outlet States.bat", "", @SW_HIDE)
EndFunc

Func Button7 ()
Local $iPID = Run ("C:\Users\Bobby Hurman\Documents\AutoIt Projects\Forms\Batch Files\Monitor OFF.bat", "", @SW_HIDE)
EndFunc

Func Button10 ()
Local $iPID = Run ("C:\Users\Bobby Hurman\Documents\AutoIt Projects\Forms\Batch Files\Private Listening ON.bat", "", @SW_HIDE)
EndFunc

Func Button1 ()
Local $iPID = Run ("C:\Users\Bobby Hurman\Documents\AutoIt Projects\Forms\Batch Files\Fail-Safe PC Power ON.bat", "", @SW_HIDE)
EndFunc

Func Button4 ()
Local $iPID = Run ("C:\Users\Bobby Hurman\Documents\AutoIt Projects\Forms\Batch Files\Display Date and Time.bat", "", @SW_HIDE)
EndFunc

Func Button3 ()
Local $iPID = Run ("C:\Users\Bobby Hurman\Documents\AutoIt Projects\Forms\Batch Files\Power OFF PC.bat", "", @SW_HIDE)
EndFunc

Func Button2 ()
Local $iPID = Run ("C:\Users\Bobby Hurman\Documents\AutoIt Projects\Forms\Batch Files\Power Cycle PC.bat", "", @SW_HIDE)
EndFunc

Func Button12 ()
Local $iPID = Run ("C:\Users\Bobby Hurman\Documents\AutoIt Projects\Forms\Batch Files\Outlet Control.bat", "", @SW_HIDE)
EndFunc

Don't laugh... :D

Anyway, the way I have it setup, I'll be using 5 out of the 8 IP switched ports to do the following:

1. Wake-On-Lan doesn't work with my i7 990x and eVGA x58 motherboard, so as makeshift alternative, I turned AC Recovery on the in the BIOS and will use the power cycle feature to trick the PC into "waking up" this way. There is also a script I have running to ping my desktop to make sure it comes back on in the event of a power failure or system freeze. And to power it off entirely.

2. Sequentially power up my home studio audio equipment for either speaker monitor or headphone use.

3. Power on and off my monitor.

 

So on to my current problems.

There were two things I was working on last that ended up being a roadblock:

1. I just added code that allowed me to minimize my application to the tray, but I couldn't figure out how to restore it with a double-click. Instead I have to click the tray icon. When it's restored, the taskbar item doesn't appear either. Then there's right-clicking it to bring up the tray menu items. I did manage to figure out how to add a new menu item, but I couldn't get it to send the command to the existing button. 

2. Though I'm able to run my batch files through the buttons I setup, I'd prefer to have the code in the batch file run in the .au3 script itself. But I couldn't figure out how to run cURL.exe with parameters. It would just error out. The command I'm trying to run is:  

curl.exe "http://username:password@webaddress/script?run013=run"

This command allows me to send pre-authenticated commands to the switch, so I don't have log in to run them.

Right now, because I'm using these batch files, this application is very restricted to my setup, and would difficult to port to someone else's setup without reconfiguring all of them. I'm hoping at least this way, I can just make a few changes in an .au3 script and be on my way.

Now, those are my two biggest issues at the moment, that I'd be ever so grateful for assistance with. I will say, as much as I enjoy having answers (especially with how frustrating some of this has been... lol), I would really appreciate some layman explanations too as to why what I did was wrong, or how I could have done something more efficient. I really do want to learn this stuff, but I'm having a difficult time connecting the dots, and so that would go much, much further for me down the road.  

I have a couple of sticky notes with a bunch of other things I want to implement, but I don't want to wear out my welcome with my first post (considering this book I just wrote here already...).

Well, I really appreciate your time and assistance with those questions above. I'm hoping to learn a lot and share a lot here, and maybe do something incredible in-between!

Thanks! :D

Share this post


Link to post
Share on other sites



I left lots of comment notes, hopefully it helps seeing where your issues are.

#include <INet.au3>
#include <GuiButton.au3>
#include <GuiToolBar.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <IE.au3>

Opt("TrayOnEventMode", 1) ; Use event trapping for tray menu
Opt("TrayMenuMode", 3) ; Default tray menu items will not be shown.

TrayCreateItem("Fail-Safe PC Power ON")
;TrayItemSetOnEvent(-1, "Fail-Safe PC Power ON"); this cannot be a function!
                                               ; I'll comment out and put what I
                                               ;  I think you're trying to do
TrayItemSetOnEvent(-1, "Fail_Safe_PC_Power_ON") ; check this function, it requres a GUI call $nMsg
                                                ;  which means you will never have anything fire this
                                                ;  function that calls Button1() function
TrayCreateItem("")
$hTray_Show_Item = TrayCreateItem("Hide")
TrayItemSetOnEvent(-1, "To_Tray")
TrayCreateItem("")
TrayCreateItem("Exit")
TrayItemSetOnEvent(-1, "On_Exit")



; Use the Autoit window as a parent to prevent appearing on the taskbar
$hGUI = GUICreate("Digital Loggers Web Power 7 Script Initializer", 410, 379, 205, 355, Default, Default, WinGetHandle(AutoItWinGetTitle()))


 ; we are using a lot of functions that are calling specific functions
 ; we can use an array to make things easier, I'll avoid the normal naming sequences
 ;  I use, but not my declarations of the variables (eg, Global, Local, Dim, etc)

 Global $gi_MaxButtonSlots = 12
 Global $Button[$gi_MaxButtonSlots + 1] ; now e have 13 slots available
                                        ;  to store 12 buttons, slots [0] - [12]
                                        ;  increase $gi_MaxButtonSlots to 13 or more
                                        ;  if you're going to add more buttons
                                        ; note below how we changed things like
                                        ;  $Button1 to $Button[1] etc

#Region ### START Koda GUI section ### Form=c:\users\bobby hurman\documents\autoit projects\forms\test.kxf
;(original values)$_1_1 = GUICreate("Digital Loggers Web Power 7 Script Initializer", 410, 379, 205, 355)
$Pic = GUICtrlCreatePic("C:\Users\Bobby Hurman\Documents\AutoIt Projects\Forms\background-dark.jpg", -1, 0, 411, 379)
GUICtrlSetState(-1, $GUI_DISABLE)
GUISetBkColor(0xFFFFFF)
$Button[1] = GUICtrlCreateButton("Fail-Safe PC Power ON", 144, 8, 129, 33, $BS_DEFPUSHBUTTON)
GUICtrlSetTip(-1, "Automatically Reboots a device, displays the results, and sends a SYSLOG report (if configured) ")
$Button[2] = GUICtrlCreateButton("Power Cycle PC", 152, 48, 113, 33)
GUICtrlSetTip(-1, "Power cycles Bobby Hurman-PC")
$Button[3] = GUICtrlCreateButton("Power OFF PC", 152, 80, 113, 33)
GUICtrlSetTip(-1, "Powers off Bobby Hurman-PC")
$Button[4] = GUICtrlCreateButton("Display Date and Time", 152, 120, 113, 33)
GUICtrlSetTip(-1, "Displays date and time")
$Button[5] = GUICtrlCreateButton("Displays Outlet States", 152, 152, 113, 33)
GUICtrlSetTip(-1, "Displays outlet states in numberical form ")
$Button[6] = GUICtrlCreateButton("Monitor ON", 152, 192, 113, 33)
GUICtrlSetTip(-1, "Powers on monitor")
$Button[7] = GUICtrlCreateButton("Monitor OFF", 152, 224, 113, 33)
GUICtrlSetTip(-1, "Powers off monitor")
$Button[8] = GUICtrlCreateButton("Public Listening ON", 88, 264, 113, 33)
GUICtrlSetTip(-1, "Powers on mixer, and then each monitor sequentially with a 2 second delay")
$Button[9] = GUICtrlCreateButton("Public Listening OFF", 88, 296, 113, 33)
GUICtrlSetTip(-1, "Powers off mixer, and then each monitor sequentially with a 2 second delay")
$Button[10] = GUICtrlCreateButton("Private Listening ON", 216, 264, 113, 33)
GUICtrlSetTip(-1, "Powers on only mixer for (headphone use)")
$Button[11] = GUICtrlCreateButton("Private Listening OFF", 216, 296, 113, 33)
GUICtrlSetTip(-1, "Powers off only mixer for (headphone use)")
$Button[12] = GUICtrlCreateButton("Outlet Control", 16, 60, 120, 40, $BS_BITMAP)
GUICtrlSetImage(-1, "C:\Users\Bobby Hurman\Documents\AutoIt Projects\Forms\digitalloggers_logo_2.bmp", -1)
GUICtrlSetTip(-1, "Web Power Switch 7 internal web server")
GUICtrlSetCursor (-1, 0)
$Label1 = GUICtrlCreateLabel("Created by:          Bobby Hurman", 0, 336, 136, 19)
GUICtrlSetFont(-1, 8, 400, 0, "Arial Narrow")
GUICtrlSetBkColor(-1, 0x808080)
$Label2 = GUICtrlCreateLabel("Copyright 2014: Bobby Hurman", 0, 353, 136, 19)
GUICtrlSetFont(-1, 8, 400, 0, "Arial Narrow")
GUICtrlSetBkColor(-1, 0x808080)
$Label3 = GUICtrlCreateLabel(" v.0.1.6", 375, 353, 37, 19)
GUICtrlSetFont(-1, 8, 400, 0, "Arial Narrow")
GUICtrlSetBkColor(-1, 0x808080)



GUISetState()
;(original values)GUISetState(@SW_HIDE)
#EndRegion ### END Koda GUI section ###


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    ;(This command was slow to respond) Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $GUI_EVENT_MINIMIZE
            To_Tray()
        Case $Button[1] To $Button[$gi_MaxButtonSlots]; see how the variable $gi_MaxButtonSlots
                                                      ;  comes in handy further in code
            ; now we can do a loop to find the actual
            ;  button that was selected and since our
            ;  function naming convention is the same
            ;  this is convenient
            For $i = 1 To $gi_MaxButtonSlots
                If $nMsg = $Button[$i] Then
                    ; we found our selected button
                    ; Not a fan of call(), but this is the type
                    ;  of reason it's to be used
                    Call("Button" & $i)
                    ; now we'll check other calls for button 1
                    ; but you should put any function calls for button 1
                    ;  all in 1 function (eg. Button1())
                    If $i = 1 Then ; Button1 was pressed
                        Fail_Safe_PC_Power_ON() ; this should really go in
                                                ;  in function Button1()
                                                ; everything you want to accomplish
                                                ;  for a control should be in their
                                                ;  perspective function calls, you can
                                                ;  always call those other functions there
                    EndIf
                EndIf
            Next
    EndSwitch
 WEnd

;Commands for application while minimized to tray


Func To_Tray()

    If TrayItemGetText($hTray_Show_Item) = "Hide" Then
        GUISetState(@SW_HIDE, $hGUI)
        TrayItemSetText($hTray_Show_Item, "Show")
    Else
        GUISetState(@SW_SHOW, $hGUI)
        GUISetState(@SW_RESTORE, $hGUI)
        TrayItemSetText($hTray_Show_Item, "Hide")
    EndIf

EndFunc

Func Fail_Safe_PC_Power_ON()
    ; we are in side a function, and you didn't declare $nMsg or $Button1 as global above
    ;  as I did mine, so you have an error here
    ; But, why are we doing this?  We are already going to call it
    ; I'm going to just return another msgbox so you can see that
    ;  the way you have it setup, pressing Button1 will actually
    ;  return 2 calls to Button1() function
    ;  fairly sure that's not your intent
    ;  If you're trying to just use this for the tray function
    ;  remove "If $nMsg=$Button[1] Then " and then remove the call to it from
    ;  the Switch Statement for the GUI above
    If $nMsg=$Button[1] Then Button1 ()

EndFunc

Func On_Exit()
    Exit
EndFunc

;Button functions that coorespond to cURL batch files

Func Button11 ()
    ; we have a variable collecing the PID
    ;  but I'm unsure what you're trying to do with it
    ; as a local variable, $iPID will be destroyed after we
    ;  leave the function
Local $iPID = Run ("C:\Users\Bobby Hurman\Documents\AutoIt Projects\Forms\Batch Files\Private Listening OFF.bat", "", @SW_HIDE)
EndFunc

Func Button6 ()
Local $iPID = Run ("C:\Users\Bobby Hurman\Documents\AutoIt Projects\Forms\Batch Files\Monitor ON.bat", "", @SW_HIDE)
EndFunc

Func Button8 ()
Local $iPID = Run ("C:\Users\Bobby Hurman\Documents\AutoIt Projects\Forms\Batch Files\Public Listening ON.bat", "", @SW_HIDE)
EndFunc

Func Button9 ()
Local $iPID = Run ("C:\Users\Bobby Hurman\Documents\AutoIt Projects\Forms\Batch Files\Public Listening OFF.bat", "", @SW_HIDE)
EndFunc

Func Button5 ()
Local $iPID = Run ("C:\Users\Bobby Hurman\Documents\AutoIt Projects\Forms\Batch Files\Displays Outlet States.bat", "", @SW_HIDE)
EndFunc

Func Button7 ()
Local $iPID = Run ("C:\Users\Bobby Hurman\Documents\AutoIt Projects\Forms\Batch Files\Monitor OFF.bat", "", @SW_HIDE)
EndFunc

Func Button10 ()
Local $iPID = Run ("C:\Users\Bobby Hurman\Documents\AutoIt Projects\Forms\Batch Files\Private Listening ON.bat", "", @SW_HIDE)
EndFunc

Func Button1 ()
    Return MsgBox(0, "Function", 1) ; using msgbox here because I don't want to run anything
Local $iPID = Run ("C:\Users\Bobby Hurman\Documents\AutoIt Projects\Forms\Batch Files\Fail-Safe PC Power ON.bat", "", @SW_HIDE)
EndFunc

Func Button4 ()
Local $iPID = Run ("C:\Users\Bobby Hurman\Documents\AutoIt Projects\Forms\Batch Files\Display Date and Time.bat", "", @SW_HIDE)
EndFunc

Func Button3 ()
Local $iPID = Run ("C:\Users\Bobby Hurman\Documents\AutoIt Projects\Forms\Batch Files\Power OFF PC.bat", "", @SW_HIDE)
EndFunc

Func Button2 ()
Local $iPID = Run ("C:\Users\Bobby Hurman\Documents\AutoIt Projects\Forms\Batch Files\Power Cycle PC.bat", "", @SW_HIDE)
EndFunc

Func Button12 ()
Local $iPID = Run ("C:\Users\Bobby Hurman\Documents\AutoIt Projects\Forms\Batch Files\Outlet Control.bat", "", @SW_HIDE)
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thanks so much SmOke_N! It's a bit late for me at the moment, but as soon as I wake up and get a chance, I'll definitely be looking at what you wrote. Thanks again!!

Edited by BobbyH

Share this post


Link to post
Share on other sites

So, it's been a couple of years... lol

I decided to mess around with this more since I've learned a bit more about AutoIt. Anyway, I'm stuck again. So the good news, I've gotten a lot of previous problems resolved, but this one is driving me crazy.

Here's what I have:

Opt("TrayOnEventMode", 1) ; Use event trapping for tray menu
Opt("TrayMenuMode", 3) ; Default tray menu items will not be shown.

TraySetState()
TraySetClick(16) ; Only show the menu when right-clicking
TraySetOnEvent($TRAY_EVENT_PRIMARYUP, "_Click")
;TraySetOnEvent($TRAY_EVENT_PRIMARYDOUBLE, "_DblClick")

Func _Click()
If WinExists ("App") Then
   GUISetState(@SW_SHOW, $hGUI)
   TrayItemSetText($hTray_Show_Item, "Hide")
Else
   WinExists ("App") Then
   GUISetState(@SW_HIDE, $hGUI)
   TrayItemSetText($hTray_Show_Item, "Show")
   EndIf
EndFunc

;Func _DblClick()
;
;EndFunc

You can ignore the scratchpad-esque commented out stuff since I was playing around with other possibilities. 

My problem is, I want left-clicking once on the tray icon to both hide and restore the GUI window. Basically, it works to show it by left-clicking with the "if" statement, but the "else" doesn't seem to follow suite. I want it to act similarly to how Teamviewer responds when you click it from the tray, i.e. left click it once, it restores the window; left click it again, it sends it to the tray.

I'm not sure how check the GUI's state to tell it to minimize/hide when it's showing.

As always, any guidance is greatly appreciated!

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

You are asking for if "App" exits twice... Try this

 

Func _Click()
    If WinExists("App") Then
        GUISetState(@SW_SHOW, $hGUI)
        TrayItemSetText($hTray_Show_Item, "Hide")
    ElseIf Not WinExists("App") Then
        GUISetState(@SW_HIDE, $hGUI)
        TrayItemSetText($hTray_Show_Item, "Show")
    Else
        MsgBox(0, "ERROR", "The App Window was not found")
    EndIf
EndFunc   ;==>_Click

NOT TESTED

BTW:  You should have started a new thread.

8)

Edited by Valuater

NEWHeader1.png

Share this post


Link to post
Share on other sites

Thanks for the rapid response Valuater. Unfortunately, it didn't work for me. I suppose the good news is, I didn't receive an error message. But yea, so far the only way to hide the window is by using the tray item I created or the minimize button on the GUI window itself. 

Thinking about it, I'm not sure if this other part has any bearing, but in case it does, here is the rest of my tray item related code

Func To_Tray()

    If TrayItemGetText($hTray_Show_Item) = "Hide" Then
        GUISetState(@SW_HIDE, $hGUI)
        TrayItemSetText($hTray_Show_Item, "Show")
        TrayTip("DLWP 7 Script Initializer is still running...", "Restore DLWP 7 Script Initializer by double-clicking the tray icon.", 0, $TIP_ICONASTERISK)
                                    ;^Displays traytip text when minimized to tray
                                    ; and standard Windows info icon.
                                    ; Must have title for $options to work...
    Else
        GUISetState(@SW_SHOW, $hGUI)
        GUISetState(@SW_RESTORE, $hGUI)
        TrayItemSetText($hTray_Show_Item, "Hide")

    EndIf


EndFunc

 

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

I have a template that does what you want.. if i got it right, maybe you can take what you need, ideas.

#Region ;Wrapper
#pragma compile(UPX, false)
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_UseX64=n
#AutoIt3Wrapper_Run_Tidy=y
#AutoIt3Wrapper_Res_SaveSource=y
;#AutoIt3Wrapper_Icon=
;#AutoIt3Wrapper_Res_Icon_Add=
;#AutoIt3Wrapper_Outfile=
;#AutoIt3Wrapper_Res_Comment=
;#AutoIt3Wrapper_Res_Description=
#AutoIt3Wrapper_Res_Fileversion=1.0
#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7
#EndRegion ;Wrapper
;=============================================================================
#Region ;FileInstall
;FileInstall("file", @ScriptDir & "\file")
#EndRegion ;FileInstall
;=============================================================================
#Region
#include <GUIConstants.au3>
#include <TrayConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#EndRegion
;=============================================================================
#Region
OnAutoItExitRegister("Quit")
Opt("TrayMenuMode", 1)
Opt("TrayIconHide", 0)
Opt("GUICoordMode", 1)
Opt("GUIResizeMode", 1)
Opt("TrayIconDebug", 1)
Opt("TrayAutoPause", 0)
Opt("MouseCoordMode", 2)
Opt("GUIOnEventMode", 1)
Opt("MustDeclareVars", 0)
Opt("GUIEventOptions", 1)
Opt("TrayOnEventMode", 1)
Opt("ExpandEnvStrings", 1)
Opt("WinDetectHiddenText", 1)
#EndRegion
;=============================================================================
#Region
Global $GUI, $totray, $restore, $exititem, $WinState

$GUI = GUICreate("Test Window", 180, 75, 0, 0)
GUISetState()
TraySetState(1)
TraySetClick(16)
$WinState = 1

TraySetOnEvent($TRAY_EVENT_PRIMARYDOUBLE, "Window")
GUISetOnEvent($GUI_EVENT_CLOSE, "Quit")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "Minimize")
GUISetOnEvent($GUI_EVENT_RESTORE, "Restore")

$totray = TrayCreateItem("Tray")
TrayItemSetOnEvent(-1, "ToTray")
$restore = TrayCreateItem("Restore")
TrayItemSetOnEvent(-1, "RestoreT")
$exititem = TrayCreateItem("Close")
TrayItemSetOnEvent(-1, "Quit")
#EndRegion
;=============================================================================
Func Window()
    If $WinState = 0 Then
        GUISetState(@SW_SHOW, $GUI)
        $WinState = 1
    Else
        GUISetState(@SW_HIDE, $GUI)
        $WinState = 0
    EndIf
EndFunc   ;==>Window
;=============================================================================
Func ToTray()
    TrayItemSetState($totray, $TRAY_UNCHECKED)
    GUISetState(@SW_HIDE)
    $WinState = 0
EndFunc   ;==>ToTray
;=============================================================================
Func RestoreT()
    TrayItemSetState($restore, $TRAY_UNCHECKED)
    GUISetState(@SW_SHOW)
    $WinState = 1
EndFunc   ;==>RestoreT
;=============================================================================
Func Minimize()
    WinSetState($GUI, '', @SW_MINIMIZE)
EndFunc   ;==>Minimize
;=============================================================================
Func Restore()
    WinSetState($GUI, '', @SW_RESTORE)
EndFunc   ;==>Restore
;=============================================================================
Func Quit()
    Exit
EndFunc   ;==>Quit
;=============================================================================
Do
Sleep(100)
Until GUIGetMsg() = $GUI_EVENT_CLOSE

 

Edited by careca
1 person likes this

Spoiler

Paster - Main function is to paste text, but has more functions.

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

Renamer - Rename files and folders, remove portions of text from the filename etc.

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Firefox Profile Backup - Backup/restore previously saved profile.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

 

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

That works perfect!!! Thanks Careca! I borrowed a small portion of what you posted and created the below. Before, I couldn't wrap my head around variables like $WinState. But seeing your template made it make much more sense. Just to make sure I am getting it though.. :lol:

$WinState is an arbitrary (but normally appropriate) name for a variable that will be referenced in future functions. So by setting it to = 1, you're giving it a default value that can be changed or referenced by aforementioned functions.

In this code

Global $WinState

$WinState=1
TraySetOnEvent($TRAY_EVENT_PRIMARYUP, "Window")
;==================================================================
#Region
Func Window()
    If $WinState = 0 Then
        GUISetState(@SW_SHOW, $hGUI)
        $WinState = 1
    Else
        GUISetState(@SW_HIDE, $hGUI)
        $WinState = 0
    EndIf
 EndFunc        ;==>Window
 #EndRegion
 ;==================================================================

since $WinState is already 1, the Window function uses the "Else" portion of the statement to then set the GUI state to hide, and then also change the 1 to a 0. When it's 0, the Window function uses "If". Setting the GUI state to show and changing the 0 to a 1 again. And then the cycle continues.

This is great! I'll be a master programmer in no time :D

Well this is another item I can write off on my list of features I want this thing to do. The only thing I really should now is organize it better. I felt bad looking at how clean yours is versus mine.. :lol:

Actually any worthwhile tips on the best way of going about that?

 

Looks like I spoke a little too soon... It works yes, but there was a minor bug in how my previous functions were set up that caused the GUI Window to restore automatically after being minimized from the GUI. This only happened when using my tray item of hide to minimize it. Below is what I did to fix it

;==================================================================
#Region
Func Window()
    If $WinState = 0 Then
        GUISetState(@SW_SHOW, $hGUI)
        GUISetState(@SW_RESTORE, $hGUI)
        TrayItemSetText($hTray_Show_Item, "Hide") ; Added this. Changed the tray item to reflect its shown/restored state
        $WinState = 1
    Else
        GUISetState(@SW_HIDE, $hGUI)
        TrayItemSetText($hTray_Show_Item, "Show") ; Added this. Changed the tray item to reflect its hidden state
        $WinState = 0
    EndIf
 EndFunc        ;==>Window
 #EndRegion
 ;==================================================================
 Func To_Tray()

    If TrayItemGetText($hTray_Show_Item) = "Hide" Then
        GUISetState(@SW_HIDE, $hGUI)
        TrayItemSetText($hTray_Show_Item, "Show")
        TrayTip("DLWP 7 Script Initializer is still running...", "Restore DLWP 7 Script Initializer by clicking the tray icon.", 0, $TIP_ICONASTERISK)
                                    ;^Displays traytip text when minimized to tray
                                    ; and standard Windows info icon.
                                    ; Must have title for $options to work...
    Else
        GUISetState(@SW_SHOW, $hGUI)
        GUISetState(@SW_RESTORE, $hGUI)
        TrayItemSetText($hTray_Show_Item, "Hide")


    EndIf
EndFunc
;=====================================================================

 

Edited by BobbyH
Minor minimize bug fix

Share this post


Link to post
Share on other sites

I often group things together, if i can, like buttons and other gui items on top within a 

;=============================================================================
#Region

like that.

Glad you solved the issue. :D

 


Spoiler

Paster - Main function is to paste text, but has more functions.

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

Renamer - Rename files and folders, remove portions of text from the filename etc.

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Firefox Profile Backup - Backup/restore previously saved profile.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

 

Share this post


Link to post
Share on other sites

#10 ·  Posted

Ah ok, thanks! I've cleaned this script up quite a bit with that. It's still messy, but not as bad as it was.. :lol:

So, I've hit another road block. I'm trying to use _Singleton to not only close my script if there's an existing instance, but also either activate the existing instance, or if it's hidden, restore and activate its GUI window.

I've tried so many variations, I can't even remember what all I've done... :lol:

In which case, I'll just write out what I thought should have worked last:

If _Singleton("DLWP7SI.exe",1) = 0 And Not WinExists ("Digital Loggers Web Power 7 Script Initializer") Then
      GUISetState(@SW_SHOW, $hGUI)
      GUISetState(@SW_RESTORE, $hGUI)
      TrayItemSetText($hTray_Show_Item, "Hide")
      $WinState = 1
   Else
      If _Singleton("DLWP7SI.exe",1) = 0 And WinExists ("Digital Loggers Web Power 7 Script Initializer") Then
         WinSetState("Digital Loggers Web Power 7 Script Initializer", "", @SW_HIDE)
         TrayItemSetText($hTray_Show_Item, "Show")
         $WinState = 0
      EndIf
   EndIf

I've tried using the $WinState variable too with the same failing result. _Singleton("",1) = 0 does its job fine, but I can't seem to figure out how to interact with the existing instance. Actually, thinking about it... That's probably exactly what the problem is. I'm still at a lost on how to do that though.

On a side note, I tried monitor25's version of Melba23's system tray icon interaction script, and I got it to work on my Win7 x64 machine (after I unhid the icon). But I stopped at the mouse right-clicking the menu since it was still only interacting with the new instances or only the first instance where I couldn't even create a second one without the script exiting.

Thanks for the assist!

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

What you're asking is to change the other process window state if i got it right.

I don't know about changing trayitemtext, didn't look at it even, but here's what i got that i think may suit what you're looking for:

#Region ;Wrapper
#pragma compile(UPX, false)
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_UseX64=n
#AutoIt3Wrapper_Run_Tidy=y
#AutoIt3Wrapper_Res_SaveSource=y
;#AutoIt3Wrapper_Icon=
;#AutoIt3Wrapper_Res_Icon_Add=
;#AutoIt3Wrapper_Outfile=
;#AutoIt3Wrapper_Res_Comment=
;#AutoIt3Wrapper_Res_Description=
#AutoIt3Wrapper_Res_Fileversion=1.0
#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7
#EndRegion ;Wrapper
;=============================================================================
#Region ;FileInstall
;FileInstall("file", @ScriptDir & "\file")
#EndRegion ;FileInstall
;=============================================================================
#Region
#include <GUIConstants.au3>
#include <TrayConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>
#include <WinAPI.au3>

#EndRegion
;=============================================================================
#Region
OnAutoItExitRegister("Quit")
Opt("TrayMenuMode", 1)
Opt("TrayIconHide", 0)
Opt("GUICoordMode", 1)
Opt("GUIResizeMode", 1)
Opt("TrayIconDebug", 1)
Opt("TrayAutoPause", 0)
Opt("MouseCoordMode", 2)
Opt("GUIOnEventMode", 1)
Opt("MustDeclareVars", 0)
Opt("GUIEventOptions", 1)
Opt("TrayOnEventMode", 1)
Opt("ExpandEnvStrings", 1)
Opt("WinDetectHiddenText", 1)
#EndRegion
;=============================================================================
#Region
Global $GUI, $totray, $restore, $exititem, $WinState, $hTray_Show_Item
Local $Exists = 0

Local $aProcessList = ProcessList("single.exe") ;maybe singleton is better, but here's my idea, list processes with said name
For $i = 1 To $aProcessList[0][0] ;loop through all
    If $Exists = 0 Then ;finds first one
        $Exists = 1 ;sets var to 1
    Else
        MsgBox(64, $i, 'Second Instance') ;at the next instance, var is already 1 so it jumps here
        _WinAPI_ShowWindow(WinGetHandle("Test Window"), @SW_SHOW) ;show window with that title/handle.
        Exit ;out because this was the second instance
    EndIf
Next

$GUI = GUICreate("Test Window", 180, 75, 500, 200)
GUISetState()
TraySetState(1)
TraySetClick(16)
$WinState = 1

TraySetOnEvent($TRAY_EVENT_PRIMARYDOUBLE, "Window")
GUISetOnEvent($GUI_EVENT_CLOSE, "Quit")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "Minimize")
GUISetOnEvent($GUI_EVENT_RESTORE, "Restore")

$totray = TrayCreateItem("Tray")
TrayItemSetOnEvent(-1, "ToTray")
$restore = TrayCreateItem("Restore")
TrayItemSetOnEvent(-1, "RestoreT")
$exititem = TrayCreateItem("Close")
TrayItemSetOnEvent(-1, "Quit")
#EndRegion
;=============================================================================
Func Window()
    If $WinState = 0 Then
        GUISetState(@SW_SHOW, $GUI)
        $WinState = 1
    Else
        GUISetState(@SW_HIDE, $GUI)
        $WinState = 0
    EndIf
EndFunc   ;==>Window
;=============================================================================
Func ToTray()
    TrayItemSetState($totray, $TRAY_UNCHECKED)
    GUISetState(@SW_HIDE)
    $WinState = 0
EndFunc   ;==>ToTray
;=============================================================================
Func RestoreT()
    TrayItemSetState($restore, $TRAY_UNCHECKED)
    GUISetState(@SW_SHOW)
    $WinState = 1
EndFunc   ;==>RestoreT
;=============================================================================
Func Minimize()
    WinSetState($GUI, '', @SW_MINIMIZE)
EndFunc   ;==>Minimize
;=============================================================================
Func Restore()
    WinSetState($GUI, '', @SW_RESTORE)
EndFunc   ;==>Restore
;=============================================================================
Func Quit()
    Exit
EndFunc   ;==>Quit
;=============================================================================
Do
    Sleep(100)
Until GUIGetMsg() = $GUI_EVENT_CLOSE

hope it makes sense, to test this, compile it, and run, hide it. run it again, the second one will close and bring the first one up.

As i said, maybe singleton is better, but i don't understand it very well.

 

PS: There's also this:

Local $aProcessList = ProcessList("SomeApp.exe")
If $aProcessList[0][0] >= 2 Then Exit

 

Edited by careca
1 person likes this

Spoiler

Paster - Main function is to paste text, but has more functions.

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

Renamer - Rename files and folders, remove portions of text from the filename etc.

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Firefox Profile Backup - Backup/restore previously saved profile.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now