Results 1 to 7 of 7

Thread: VBscript to verify "Apple iPhone" is connected

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2020
    Posts
    3

    VBscript to verify "Apple iPhone" is connected

    First off I have to tell you I am no programmer so please bear with me if you please.

    I am attempting to write a small vbscript routine to assist in copying photos from an iPhone to a Win10 machine. I am using MTPdrive to map the iPhone (P:\DCIM\). I have a couple conditional statements that run the copy routine if the folder exists -- if not, it prompts to connect. However, what would be tremendously helpful is if there is a way to test if the “Apple iPhone” device connection exists (not the mapped drive).

    My question - is it possible to verify an “Apple iPhone” connection via vbscripting or some other coding prior to the iPhone being mapped? That is, I only want to run MTPdrive.exe program if it can first be confirmed the “Apple iPhone” device is connected.

    Thanks,
    Steve

  2. #2
    Fanatic Member
    Join Date
    Feb 2003
    Posts
    1,003

    Re: VBscript to verify "Apple iPhone" is connected

    Hello Skipping, welcome to the forum. Sorry for the late reply, but it took me a while to think of an answer. This vbscript code should be able to detect whether or not an iPhone is connected to your computer:

    Code:
    Option Explicit
    
    wscript.echo iPhoneConnected
    
    Private Function iPhoneConnected()
    Dim colItems
    Dim objItem
    Dim objWMIService
    Dim strComputer
    
       strComputer = "."
    
       Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
       Set colItems = objWMIService.ExecQuery("select * from Win32_PnPEntity",,48)
    
       For Each objItem in colItems
           If LCase(Trim(objItem.Caption)) = "apple iphone" Then
              iPhoneConnected = True
              Exit Function
           End If
       Next
    
       iPhoneConnected = False
    End Function
    .

    This code can also be easily modified to detect/list other devices btw.

    yours,
    Peter Swinkels
    Several projects of mine that used to be posted here can now be found at: https://github.com/PeterSwinkels

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2020
    Posts
    3

    Re: VBscript to verify "Apple iPhone" is connected

    Quote Originally Posted by Peter Swinkels View Post
    Hello Skipping, welcome to the forum. Sorry for the late reply, but it took me a while to think of an answer. This vbscript code should be able to detect whether or not an iPhone is connected to your computer:

    Code:
    Option Explicit
    
    wscript.echo iPhoneConnected
    
    Private Function iPhoneConnected()
    Dim colItems
    Dim objItem
    Dim objWMIService
    Dim strComputer
    
       strComputer = "."
    
       Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
       Set colItems = objWMIService.ExecQuery("select * from Win32_PnPEntity",,48)
    
       For Each objItem in colItems
           If LCase(Trim(objItem.Caption)) = "apple iphone" Then
              iPhoneConnected = True
              Exit Function
           End If
       Next
    
       iPhoneConnected = False
    End Function
    .

    This code can also be easily modified to detect/list other devices btw.

    yours,
    Peter Swinkels
    Peter,

    Thank you for your response. I must note that I am not a programmer so some of my questions may seem frivolous or quite silly.

    This routine appears to be doing what I had in mind. It returns a “-1”, if the iPhone is connected (i.e., an “Apple iPhone” connection exists); and returns a “0” if not connected. However, I’m not sure how this is to be applied. As an example - is there way to call sub1 if connected else call sub2?

    Thanks,
    Steve

  4. #4
    Fanatic Member
    Join Date
    Feb 2003
    Posts
    1,003

    Re: VBscript to verify "Apple iPhone" is connected

    Steve,

    Don't worry about your questions appearing to be silly, they're not for someone with no experience in programming. Your questions however are so basic that it seems you would have to learn vbscript before accomplishing your goal. If you like you could post what you already wrote yourself and I will have a shot at writing the required script. As to your question, you need to use If/Then/Else statements:

    Code:
    If iPhoneConnected Then do something with phone Else report to user there is no iphone connected
    
    [i]The function in the code I posted here needs to be placed here.[i/]
    Here's a tutorial about If/Then/Else statements: https://www.tutorialspoint.com/vbscr..._decisions.htm. You can also learn about other vbscript related stuff over there.

    yours,
    Peter Swinkels
    Last edited by Peter Swinkels; Aug 23rd, 2020 at 09:30 AM.
    Several projects of mine that used to be posted here can now be found at: https://github.com/PeterSwinkels

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2020
    Posts
    3

    Re: VBscript to verify "Apple iPhone" is connected

    Peter,

    Again, thank you for your response. I am familiar with If/Then statements. I just wasn’t sure how to proceed with a function as such. As a test, what I did was.

    If iPhoneConnected then
    MsgBox “ YES . . .”
    Else
    MsgBox “ NOPE . . .”
    End If

    I remarked out the “wscript.echo iPhoneConnected” line to suppress the Windows Script Host message (0 or -1). This works fine. I will incorporate this into the rest of my program.

    Much appreciated,
    Steve

  6. #6
    Fanatic Member
    Join Date
    Feb 2003
    Posts
    1,003

    Re: VBscript to verify "Apple iPhone" is connected

    Okay. :-)
    Several projects of mine that used to be posted here can now be found at: https://github.com/PeterSwinkels

  7. #7

    Re: VBscript to verify "Apple iPhone" is connected

    Thanks for the information. I was looking for this...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width