Jump to content

Looping Fails When Compiled To .EXE


Recommended Posts

Hi Community

I am no programmer by any means. I am use to making .BAT scripts for the tasks I need but I have taught myself the basics of AutoIT and love it.

I have created the following script that can detect when my iPhone is in my office via WIFI (IE, i'm in the building) and enable or disable my Outlook mail forwarding (needed to get e-mails on my phone from another account).

#include "Misc.au3"
$reply1 = 0
$mailforward = 0
while 1 = 1
$skip1 = 0
$online = 0
    if _Singleton("OutlookiPhone",1) = 0 Then
        ProcessClose("OutlookiPhone.exe")
    EndIf
Ping("192.168.6.105")
$online = @error
;msgbox(48, "Info", "Ping = " & $online);This line is used to check the looping. Debugging only!    
if $online = 0 Then
    if $reply1 = 0 Then
        $reply1 = msgbox(4,"Jasons iPhone Detected", "iPhone Has Been Detected! Would you like to disable Outlook Mail forwarding now?")
        if $reply1 = 6 Then
        ; Disable Auto Forward
            WinSetState("Inbox", "", @SW_MAXIMIZE)
            WinActivate("Inbox")
            $mousecurrentpos = MouseGetPos ()
            Send("{ALT}")
            Send("{t}")
            Send("{l}")
            WinWait("Rules")
            Opt("MouseCoordMode", 2)
            Opt("PixelCoordMode", 2)
            MouseMove(24,89,0)
            sleep(100)
            $pos = MouseGetPos ()
            $chkbox = PixelGetColor ($pos[0] , $pos[1])
                if $chkbox = (000000) Then 
                    MouseClick("left")
                EndIf
                MouseMove(380,400,0)
                MouseClick("left")
                MouseMove($mousecurrentpos[0],$mousecurrentpos[1])
                $mailforward = 0
            Else
                msgbox(48, "Info", "Outlook mail will continue to be forwarded to your mobile. Please disable manually or re-load OutlookiPhone detector!")
            EndIf
        EndIf
    ELSE
    IF $mailforward = 0 Then
IF $online = 1 Then
    $reply2 = msgbox(4,"iPhone Offline", "iPhone Has NOT Been Detected. Would you like to switch to mail forwarding (will auto-forward in 2 minutes)", 120)
    if $reply2 = 7 Then
            msgbox(0, "", "Mail will not be forwaded to your mobile now. Check for iPhone in 45 Minutes")
            sleep(2700000);45 minutes = 2700000
            $skip1 = 1
            Else
            if $skip1 = 0 Then
            ;Enable Auto Fwd
                WinSetState("Inbox", "", @SW_MAXIMIZE)
                WinActivate("Inbox")
                $mousecurrentpos = MouseGetPos ()
                Send("{ALT}")
                Send("{t}")
                Send("{l}")
                WinWait("Rules")
                Opt("MouseCoordMode", 2)
                Opt("PixelCoordMode", 2)
                MouseMove(24,89,0)
                sleep(100)
                $pos = MouseGetPos ()
                $chkbox = PixelGetColor ($pos[0] , $pos[1])
                if $chkbox = (16777215) Then 
                    MouseClick("left")
                EndIf
                $mailforward = 1
            EndIf       
        MouseMove(380,400,0)
        MouseClick("left")
        MouseMove($mousecurrentpos[0],$mousecurrentpos[1])
        $reply1 = 0
        MsgBox(64,"Out Of Office", "Your Mail is forwaded to your mobile. To unforward your mail automatically make sure your phone is on the network and select OK")
        EndIf
    EndIf
EndIf
EndIf
Sleep(10000)
WEnd

The script works well. It will detect when the phone is not in range, ask a question if forwarding is to be enabled and auto enable if cancel isn't selected. I couldn't figure out how to actually check if the checkbox for my forwarding rule is checked or not so I just looked at the colour of the pixle under the mouse pointer to see if it was black or white.

I need this script to run continously and just sit in the sys tray. To restart it you simply launch the .exe again and it kills the prev script and loads again. The issue I am having is that when I create an .exe and it asks the question "Jasons iPhone Detected", "iPhone Has Been Detected! Would you like to disable Outlook Mail forwarding now?" and I select NO, it throws up the info message in the script, then waits for the delay at the end and then terminates???

There is a Wend at the end and a While 1 = 1 at the start so why does it quit? I need it to never quit so if someone can see my error please let me know

Link to comment
Share on other sites

To check the state use GUICtrlGetState (Search the help file...)

You should also add something, when the phone is offline is shouldn't run the code more than once,and if it's online it should again run the code only once, it should check the ip all the time (every 10secs) and if it's online run the code once, if it's offline run the code once.I havn't done that, I just make the code a bit more clear...

Try it to see if it works, I can't try it but the code seems a bit more clear

#include "Misc.au3"

$reply1 = 0
$mailforward = 0
$skip1 = 0
$online = 0

single()

Func single()
    If _Singleton("OutlookiPhone", 1) = 0 Then
        ProcessClose("OutlookiPhone.exe")
    EndIf
EndFunc   ;==>single

While 1
    Ping("192.168.6.105")
    $online = @error
    If $online = 0 Then
        detected()
    Else
        notdetected()
    EndIf
    Sleep(10000)
WEnd

Func detected()
    If $reply1 = 0 Then
        $msgbox1 = MsgBox(4, "Jasons iPhone Detected", "iPhone Has Been Detected! Would you like to disable Outlook Mail forwarding now?")
        If $msgbox1 = 6 Then
            ; Disable Auto Forward
            WinSetState("Inbox", "", @SW_MAXIMIZE)
            WinActivate("Inbox")
            $mousecurrentpos = MouseGetPos()
            Send("{ALT}")
            Send("{t}")
            Send("{l}")
            WinWait("Rules")
            Opt("MouseCoordMode", 2)
            Opt("PixelCoordMode", 2)
            MouseMove(24, 89, 0)
            Sleep(100)
            $pos = MouseGetPos()
            $chkbox = PixelGetColor($pos[0], $pos[1])
            If $chkbox = (000000) Then
                MouseClick("left")
            EndIf
            MouseMove(380, 400, 0)
            MouseClick("left")
            MouseMove($mousecurrentpos[0], $mousecurrentpos[1])
            $mailforward = 0
        Else
            MsgBox(48, "Info", "Outlook mail will continue to be forwarded to your mobile. Please disable manually or re-load OutlookiPhone detector!")
        EndIf
    Else
        If $mailforward = 0 Then
            notdetected()
        EndIf
    EndIf
EndFunc   ;==>detected

Func notdetected()
    If $online = 1 Then
        $msgbox2 = MsgBox(4, "iPhone Offline", "iPhone Has NOT Been Detected. Would you like to switch to mail forwarding (will auto-forward in 2 minutes)", 120)
        If $msgbox2 = 7 Then
            MsgBox(0, "", "Mail will not be forwaded to your mobile now. Check for iPhone in 45 Minutes")
            Sleep(2700000);45 minutes = 2700000
            $skip1 = 1
        Else
            If $skip1 = 0 Then
                ;Enable Auto Fwd
                WinSetState("Inbox", "", @SW_MAXIMIZE)
                WinActivate("Inbox")
                $mousecurrentpos = MouseGetPos()
                Send("{ALT}")
                Send("{t}")
                Send("{l}")
                WinWait("Rules")
                Opt("MouseCoordMode", 2)
                Opt("PixelCoordMode", 2)
                MouseMove(24, 89, 0)
                Sleep(100)
                $pos = MouseGetPos()
                $chkbox = PixelGetColor($pos[0], $pos[1])
                If $chkbox = (16777215) Then
                    MouseClick("left")
                EndIf
                $mailforward = 1
            EndIf
            MouseMove(380, 400, 0)
            MouseClick("left")
            MouseMove($mousecurrentpos[0], $mousecurrentpos[1])
            $reply1 = 0
            MsgBox(64, "Out Of Office", "Your Mail is forwaded to your mobile. To unforward your mail automatically make sure your phone is on the network and select OK")
        EndIf
    EndIf
EndFunc   ;==>notdetected
Edited by AoRaToS

s!mpL3 LAN Messenger

Current version 2.9.9.1 [04/07/2019]

s!mpL3 LAN Messenger.zip

s!mpL3

Link to comment
Share on other sites

Excellent!!! :)

Not only does it work as it should (fixes the loop) but also is easier to read and understand!

I knew there would have to be a way of making the code neater but i'm just beginning and using anything I can find at the moment as examples on how to do it so thank you very much for taking the time to show me the right way. Much appreciated.

My plan was to add in some functions (which I have variables in there for) that will prevent the questions being continously after I have selected to enable or disable the mail forward UNTIL the phones status changes (goes online or offline again). I just needed to get it running first then optomize it and that's what I will do right now.

Edited by jestermgee
Link to comment
Share on other sites

Great, glad it works as I couldn't try it out at the moment! I was making a simple check but since you're on it I'll leave you to it and I'm going to sleep as it's late here,3 after midnight! I'll log in here and check it out tomorrow to see if there's any progress or if there's anything I can help with so post in, nice idea for a program by the way :)

s!mpL3 LAN Messenger

Current version 2.9.9.1 [04/07/2019]

s!mpL3 LAN Messenger.zip

s!mpL3

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...