AppleScript Newbie Needs Help...Trying to Automate SONOS On/Off...

Discussion in 'Mac Programming' started by JoelBC, Nov 16, 2014.

  1. JoelBC macrumors 6502a

    Joined:
    Jun 16, 2012
    #1
    I am trying to write an AppelScript that will i) turn on my SONOS music system when it detects the presence of my iPhone [i.e. when I come home] and ii) turns off my SONOS music system when it does NOT detect the presence of my iPhone [i.e. when I leave the home]*.

    I have written a script that pings my iPhone's IP address to determine its absence or presence but have run into three problems [for now...I am sure I will run into more down the road]:

    1. I cannot for some reason successfully ping my iPhone when wirelessly connected to my LAN. I do not why this is happening as I can ping my Win 7 laptops, etc.

    I attached the results of my ping attempts.

    I would appreciate any and all assistance in solving this because without this working I don't know how else to test the absence / presence of my iPhone.



    2. I don't know how to get the script to continuously run repeatedly in the background to continuously test the absence / presence of my iPhone as I come and go from the house...please help.

    3. If the above discussed script is called iPhonePresences whose output is a variable called iPhoneStatus then how do I call / reference / use iPhoneStatus in another script.

    Thanks for all your help,

    Joel


    * I will sorry about / work on the specific tracks / stations I want played as a second project / step after I get the basis on / off working.
     

    Attached Files:

  2. JoelBC, Nov 16, 2014
    Last edited by a moderator: Nov 17, 2014

    JoelBC thread starter macrumors 6502a

    Joined:
    Jun 16, 2012
    #2
    As as item 1. is concerned here is some added information...

    The script that I have written is as follows:

    try

    Code:
    set IP_address to "192.168.2.18"
    	
    	set ping to do shell script ("ping -c 2 " & IP_address & "| head -2 | tail -1 |cut -d = -f 4")
    	
    	if ping contains "ms" then
    		set iPhoneDetected to "True"
    		display dialog ("iPhone Detected - " & ping) buttons {"OK"} default button 1 with title "iPhone Detection Test"
    		
    	else if ping contains "timeout" then
    		set iPhoneDetected to "False"
    		display dialog "iPhone Not Detected " buttons {"OK"} default button 1 with title "iPhone Detection Test"
    		
    	end if
    end try



    The interesting thing is that:

    a. If my iPhone is connected to WiFi when I open [i.e. first run the script] then the script does may / may not detect the iPhone.

    b. If my iPhone is not connected to WiFi when I open [i.e first run the script] but later turn the WiFi on then the iPhone is always detected...if I then toggle the iPhone's WiFi off / on then it similarly successfully detects the iPhone.

    It seems to be a problem detecting the iPhone when the iPhone's WiFi is on [i.e. not toggled] even though the iPhone's IP address is reserved [i.e there are no lease issues].

    This is incredibly frustrating because I think it should be working in that when I change the IP address to the IP address of my laptop then the script works perfectly when I toggle the WiFi on and off..why not for the iPhone? This is so frustrating...
     

Share This Page