Jump to content

Only launch a program once when phone gets plugged in..


Recommended Posts

Heys guys, I wanted to design something that would run in the startup folder always, that would wait till a iphone is plugged into a phone then launch another application (that too is autoit based). The problem that I have right now, is that I can detect when an iphone is plugged in, but how do I get it to stop launching the app if you close the app but want to leave the phone plugged in? I don't want it to run the program again until the phone is unplugged and plugged back in. Ideas?

 

I was thinking of designing the script to only launch the app once, and restart itself if/when a disconnect of the iphone is detected, but I don't know how to detect a disconnected phone from the computer. 

Thanks :)

 

Global $latestname = "/Loaderv2.51.exe"
Global $foundalready = 0


While 1

    Local $vObjWMI = ObjGet("winmgmts:\\" & @ComputerName & "\root\cimv2")
    $vObjItems = $vObjWMI.ExecQuery('SELECT * FROM Win32_USBHub')
    If IsObj($vObjItems) Then
        For $vObjItem In $vObjItems
            If StringInStr($vObjItem.Description, "Apple") Then
                If $foundalready = 0 Then
                    $foundalready = 1
                    IsItStillRunning()
                    Sleep(1000)
                EndIf

            EndIf
        Next
    EndIf

WEnd

Func IsItStillRunning()
    Sleep(1000)
    Local $cc = WinExists("CydiaLoader")
    If $cc = 0 Then
        Local $c = Run(@ScriptDir & $latestname)
        If $c = 0 Then MsgBox(0, 0, "failed to open cydia impactor, did you rename it?")
    EndIf
EndFunc   ;==>IsItStillRunning

 

Link to comment
Share on other sites

I fixed it so it works but the program calls itself and I'm afraid that it will eventually cause nesting errors? Or cause memory issues if you leave the phone connected for too long.. is there a better fix?

 

Global $latestname = "/Loaderv2.51.exe"
Global $Stillconnected = 0


While 1

    Local $vObjWMI = ObjGet("winmgmts:\\" & @ComputerName & "\root\cimv2")
    $vObjItems = $vObjWMI.ExecQuery('SELECT * FROM Win32_USBHub')
    If IsObj($vObjItems) Then
        For $vObjItem In $vObjItems
            If StringInStr($vObjItem.Description, "Apple") Then
                If $Stillconnected = 0 Then
                    $Stillconnected = 1
                    Launcher()
                    StillConnected()
                    Sleep(1000)
                EndIf
            EndIf
        Next
        Sleep(5000)
    EndIf

WEnd

Func Launcher()
    Sleep(1000)
    Local $cc = WinExists("CydiaLoader")
    If $cc = 0 Then
        Local $c = Run(@ScriptDir & $latestname)
        If $c = 0 Then MsgBox(0, 0, "failed to open cydia impactor, did you rename it?")
    EndIf
EndFunc   ;==>Launcher

Func StillConnected()
    Local $vObjWMI = ObjGet("winmgmts:\\" & @ComputerName & "\root\cimv2")
    $vObjItems = $vObjWMI.ExecQuery('SELECT * FROM Win32_USBHub')
    If IsObj($vObjItems) Then
        For $vObjItem In $vObjItems
            If StringInStr($vObjItem.Description, "Apple") Then
                Sleep(10000)
                StillConnected()
            EndIf
        Next
    EndIf
    $Stillconnected = 0
EndFunc   ;==>StillConnected

 

Link to comment
Share on other sites

I think the best approach would be doing something like this :

Opt ("MustDeclareVars", 1)

HotKeySet ("{ESC}","_Exit")

Global $latestname = "/Loaderv2.51.exe"

  While True
    While Not IsAppleConnected ()
      Sleep (100)
    Wend
    Launcher ()
    While IsAppleConnected ()
      Sleep (100)
    Wend
  Wend

Func _Exit ()
  Exit
EndFunc

Func IsAppleConnected ()

    Local $vObjWMI = ObjGet("winmgmts:\\" & @ComputerName & "\root\cimv2")
    Local $vObjItems = $vObjWMI.ExecQuery('SELECT * FROM Win32_USBHub')
    If not IsObj($vObjItems) Then Exit MsgBox (0,"Error","Not an object")
    For $vObjItem In $vObjItems
      If StringInStr($vObjItem.Description, "Apple") Then return True
    Next
    Return False

EndFunc

Func Launcher()
    If not WinExists("CydiaLoader") Then
        Local $c = Run(@ScriptDir & $latestname)
        If $c = 0 Then Exit MsgBox(0, 0, "failed to open cydia impactor, did you rename it?")
    EndIf
EndFunc   ;==>Launcher

 

Link to comment
Share on other sites

2 hours ago, BatMan22 said:

@Nine Thank you, that's exactly what I wanted to do. Simple, clean and easy. WIsh I could have figured that out by myself, also I never knew that Exit Msg was a thing.. Thanks for that too!

Glad you like it !

:thumbsup:

Link to comment
Share on other sites

16 hours ago, Bilgus said:

 

Yeah I stole some code from there already, I was more confused on how to run a program once per plugin and not to launch the program continuously without getting stuck in that infinite loop of a function calling itself. It seems the simplest answer was to use the return command, which I should have known.

Link to comment
Share on other sites

This didn't work for me, I had to use Win32_PnPEntity to detect if my phone was plugged in, also if "Cydia Impactor" has a process name would be better to detect then Window title imho.

Opt ("MustDeclareVars", 1)

HotKeySet ("{ESC}","_Exit")

Global $bAppleConnected = False, $bIsAppleConnected = False
Global $sFilePath = @ScriptDir
Global $sFileName = "Loaderv2.51.exe"

While 1
    $bIsAppleConnected = IsAppleConnected()
    If $bAppleConnected <> $bIsAppleConnected Then
        If $bIsAppleConnected = True Then Launcher()
    EndIf
    $bAppleConnected = $bIsAppleConnected
    Sleep (100)
  Wend

Func _Exit ()
  Exit
EndFunc

Func IsAppleConnected ()
    Local $vObjWMI = ObjGet("winmgmts:\\" & @ComputerName & "\root\cimv2")
    Local $vObjItems = $vObjWMI.ExecQuery('SELECT * FROM Win32_PnPEntity')
    If not IsObj($vObjItems) Then Exit MsgBox (0,"Error","Not an object")
    For $vObjItem In $vObjItems
        If StringInStr($vObjItem.Description, "Apple") Then return True
    Next
    Return False
EndFunc

Func Launcher()
    If ProcessExists($sFileName) Then Return
    If Run($sFilePath & "\" & $sFileName) = 0 Then Exit MsgBox(0, 0, "failed to open cydia impactor, did you rename it?")
EndFunc

 

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...