NAS script

auhagen

macrumors regular
Original poster
May 30, 2010
131
1
Denmark
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
 
Last edited by a moderator:

blaster_boy

macrumors 6502
Jan 31, 2004
282
4
Belgium
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.
 

auhagen

macrumors regular
Original poster
May 30, 2010
131
1
Denmark
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.
Thank you! Worked like a charm!

----------

One of the things you can use is an on idle handler. You can also put in some code to see if a disk is mounted. Put in a few display notification commands to make it more verbose.

Info : http://ctp2nd.com/post/669945321/applescript-to-auto-mount-a-smb-share and Check if a disk is mounted? and idle Handlers
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.
 
Last edited by a moderator: