1

What im trying to do is to detect when an iphone is connected to a Macbook Air(10.11.6), and when it is i need to retrieve the UDID of the device. To that extend i created a Daemon that triggers a script when a new usb is plugged in:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Label</key>
        <string>local.job</string>
        <key>LaunchEvents</key>
        <dict>
                <key>com.apple.iokit.matching</key>
                <dict>
                        <key>com.apple.device-attach</key>
                        <dict>
                                <key>IOMatchLaunchStream</key>
                                <true/>
                                <key>IOProviderClass</key>
                                <string>IOUSBDevice</string>
                                <key>idProduct</key>
                                <string>*</string>
                                <key>idVendor</key>
                                <string>*</string>
                        </dict>
                </dict>
        </dict>
        <key>Program</key>
        <string>/Users/cswtillster/Library/Mine/test2.sh</string>
</dict>
</plist>

This triggers this script:

#!/bin/sh
now="$(date)"
echo  "Current date and time %s\n" "$now" >> /Users/cswtillster/Library/Mine/usb_apple.txt

i=0
 for line in $(system_profiler SPUSBDataType | sed -n -e '/iPad/,/Serial/p' -e '/iPhone/,/Serial/p' | grep "Serial Number:" | awk -F ": " '{print $2}'); do
    UDID=${line}
    echo "$UDID" "%s\n" >> /Users/cswtillster/Library/Mine/usb_apple.txt
    udid_array[i]=${line}
    i=$(($i+1))
 done

 cnt=${#udid_array[@]}
 for ((i=0;i<cnt;i++)); do
    echo ${udid_array[i]}
 done

The script retrives de UDID but my problem is that it never stops, its running every ~10 seconds:

Current date and time %s
 Tue Sep 27 16:06:39 WEST 2016
ffe6abf455a1ca722cbe1e2d970730411d0dc3fe %s

Current date and time %s
 Tue Sep 27 16:06:49 WEST 2016
ffe6abf455a1ca722cbe1e2d970730411d0dc3fe %s

Current date and time %s
 Tue Sep 27 16:06:59 WEST 2016
ffe6abf455a1ca722cbe1e2d970730411d0dc3fe %s

Can anyone help me with this problem? What i need is for the script to only run once for each connection to the usb port.

Nuno
  • 11
  • 2

0 Answers0