NAS script

Discussion in 'Mac Programming' started by auhagen, Oct 16, 2014.

  1. auhagen, Oct 16, 2014
    Last edited by a moderator: Oct 16, 2014

    auhagen macrumors regular

    Joined:
    May 30, 2010
    Location:
    Denmark
    #1
    Hi guys.

    Im new to apple scripts, and found some small guides on the net to get some of my script going.

    I want to make my script connect to my nas, when it finds it on the network.
    If it doesn't find the nas, it will delay the script and try again in 60min.

    I know where to put it in my script, I just dont know the real command to make it postpone it and retry later.

    My script looks like this so far:

    Code:
    -- Check IP address is reachable
    
    set IP_address to "192.168.0.3"
    set IP_Valid to true
    
    try
    	do shell script ("ping -c 2 " & IP_address)
    on error
    	set IP_Valid to false
    end try
    
    -- IF found.
    
    if IP_Valid then
    	
    	-- MOUNT DRIVE
    	
    	mount volume "smb://192.168.0.3/Bluray"
    	mount volume "smb://192.168.0.3/Bluray2"
    	mount volume "smb://192.168.0.3/DVD"
    	mount volume "smb://192.168.0.3/Dokumentar"
    	mount volume "smb://192.168.0.3/Standup"
    	mount volume "smb://192.168.0.3/TV"
    		
    	set endresult to "win"
    
    	
    else
    	
    	-- DO NOTHING
    	set endresult to "fail"
    	
    end if
     
  2. blaster_boy macrumors 6502

    blaster_boy

    Joined:
    Jan 31, 2004
    Location:
    Belgium
    #2
    A quick google tells me that you could use "delay" and "repeat".

    The command delay is available in AppleScript via Standard Additions:

    delay: Pause for a fixed amount of time
    delay number -- the number of seconds to delay

    But then you still need to relaunch the script from the start. This is where "repeat" comes in.

    You'll probably need to rewrite your script so that
    1 you start out with IP_Valid set to false
    2 then use "repeat while IP_Valid is false" to setup the loop
    3 inside the loop, check if IP_Valid has become true
    4 inside the loop, if true mount your drives
    5 inside the loop, else use "delay x seconds"
    6 the program quits the loop automatically once the IP_Valid is true.

    I don't really know much apple script, but the basics should be correct.

    Another way to solve this would be to leave the script as-is, but run it every x time using another program like cron and crontab.

    Regards,

    Alex from Belgium.
     
  3. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #3
  4. auhagen, Oct 17, 2014
    Last edited by a moderator: Oct 17, 2014

    auhagen thread starter macrumors regular

    Joined:
    May 30, 2010
    Location:
    Denmark
    #4
    Thank you! Worked like a charm!

    ----------

    Thanks for the ideas, but the final script ended up far more simple.

    ----------

    Final script ended up like this:

    Code:
    set IP_Valid to false
    repeat until IP_Valid is true
    	
    	-- Check IP address (nas) is reachable
    	
    	set IP_address to "192.168.0.3"
    	set IP_Valid to true
    	
    	try
    		do shell script ("ping -c 2 " & IP_address)
    	on error
    		set IP_Valid to false
    	end try
    	
    	-- Nas is found
    	
    	if IP_Valid is true then
    		
    		-- MOUNT DRIVE
    		
    		mount volume "smb://192.168.0.3/Bluray"
    		mount volume "smb://192.168.0.3/Bluray2"
    		mount volume "smb://192.168.0.3/DVD"
    		mount volume "smb://192.168.0.3/Dokumentar"
    		mount volume "smb://192.168.0.3/Standup"
    		mount volume "smb://192.168.0.3/TV"
    		
    		set endresult to "win"
    		
    	end if
    	
    	-- Nas not found
    	
    	if IP_Valid is false then
    		
    		delay 600
    		
    	end if
    	
    end repeat

    It works like a charm. If it finds the drives, it connects, and ends.

    If it doesn't it will keep looking until the nas comes online.
     

Share This Page