开发者

Setting expiration time when script tries to mount a network drive

开发者 https://www.devze.com 2022-12-22 04:37 出处:网络
The start up script bellow mounts a network drive in my macbook: try tell application \"Finder\" mount volume \"afp://iMac-01._afpovertcp._tcp.local/Ba开发者_如何学Cckup%20HD\"

The start up script bellow mounts a network drive in my macbook:

try
  tell application "Finder"
     mount volume "afp://iMac-01._afpovertcp._tcp.local/Ba开发者_如何学Cckup%20HD"
  end tell
end try

There is two problems with this script for me:

  • If I am out of the local network that the network drive is located, the script takes a long time trying to connect with it, and makes my macbook slowly in initialization. So, how to setup a maximum time for the script try to connect with the network drive?

  • If the network drive cannot be connected, a message warning me of that pops out. How to ignore this message? i.e, make it not appears.

I'm never made an apple script before of that, so please, if it is possible, help me modifying the original script.

Thanks in advance.


use 'with timout'

try
    with timeout of x seconds
        tell application "Finder"
            mount volume "afp://iMac-01._afpovertcp._tcp.local/Backup%20HD"
        end tell
    end timeout
end try


to answer your questions: 1: use with timeout as mcgrailm states; however try just writing

with timeout of 2 second --no plural
    <your code here>
end timeout

2: the dialog that pops up is a finder popup and as such your script cannot see that, the "try on error" never gets called as the error is never returned to the script, so if you want to automate the click on ok then this code will work. This code will click the button after your timeout seconds.

try
    <your code here>
on error
    tell application "System Events"
        keystroke return
    end tell
end try 

i have also included my version of the same idea which i use at home this script does a check of the network, then tries to mount the volume using a shell mount. i originally used a finder "mount volume" with timeout and that codes exists as inline comments too, but I didn't like the dialog popping up on errors; even if only for a second, so i moved on to shell script.

--------------------------------------------------------------------------------------
--"Basic Drive Mounter.app"
try
    set IP_address to "xxx.xxx.xxx.xxx"

    set IP_Valid to true

    try
        do shell script ("ping -c 2 " & IP_address)
    on error
        set IP_Valid to false
    end try

    if IP_Valid then
        tell application "Finder"
            if disk "work" exists then
            else

                try
                    do shell script "mkdir /Volumes/work"
                end try

                do shell script "mount_afp afp://xxx.xxx.xxx.xxx/work /Volumes/work/"

                -->>finder mount volume version
                --with timeout of 2 second
                --  mount volume "afp://xxx.xxx.xxx.xxx/work"
                --end timeout
                --<<finder mount volume version
            end if
        end tell
    end if
on error
    return 0

    -->>finder mount volume version
    --on error finder returns an error dialog which needs to be closed to go back and  retry
    --tell application "System Events"
    --  keystroke return
    --end tell
    --<<finder mount volume version
end try    
--------------------------------------------------------------------------------------
0

精彩评论

暂无评论...
验证码 换一张
取 消