Wednesday, August 10, 2022

#1 2009-03-12 02:36:43 pm

crow
Member
Registered: 2009-03-12
Posts: 3

Mount volume dialogue box

If the volume I am trying to mount is not available, I do not want the dialogue box telling me that the volume is not available. I have:

Applescript:

tell application "Finder"
   try
       mount volume "afp://user:pword@1.1.1.1/myvol"
   on error
Doing stuff here....
   end try
end tell

I get the error; Connection Failed The server may not exists.......

I also tried:

Applescript:

tell application "Finder"
   disk "myvol" exists
   if false then
       mount volume "afp://user:pword@1.1.1.1/myvol"
   end if
   disk "myvol" exists
   if false then
   Doing stuff here....
   end if
end tell

But it never actually tries to get the volume?

I don't care which method I use, I just don't want the dialogue box saying I can't get the volume.


Filed under: Finder

Offline

 

#2 2009-03-12 02:50:02 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11771
Website

Re: Mount volume dialogue box

Hi,

do it the old-fashioned way, creating the mount point "manually" and use mount_afp

Applescript:


property server : "myServer.local"
property serverVolume : "Server"
property user : "user"
property pass : "pass"

if serverVolume is not in (do shell script "/bin/ls /Volumes") then
   set flag to mountAFP(user, pass, server, serverVolume)
else
   set flag to true
end if
if flag then
   -- do something
end if

on mountAFP(user_name, pass_word, thehost, theVolume)
   set theAddress to quoted form of ("afp://" & user_name & ":" & pass_word & "@" & thehost & "/" & theVolume)
   set mountpoint to quoted form of ("/Volumes/" & theVolume)
   try
       do shell script "/bin/mkdir " & mountpoint & "; /sbin/mount_afp " & theAddress & space & mountpoint
       return true
   on error
       do shell script "/bin/rm -r " & mountpoint
       return false
   end try
end mountAFP


regards

Stefan

Offline

 

#3 2009-03-13 08:06:20 am

crow
Member
Registered: 2009-03-12
Posts: 3

Re: Mount volume dialogue box

Showing the tail end of the above example:

Applescript:

return true
on error
do shell script "/bin/rm -r " & mountpoint
--stop running here--
return false
end try
end mountAFP

-- more scripts here--

If the volume does not mount, I do not want the scripts after this to run. What Can I enter to have it just stop. I have searched but I can't seem to find a stop/end/quit/done/finish command that does what I am trying to do. I just want the script app to abort/quit. It still continues when I use quit.

Thanks for the help!

Offline

 

#4 2009-03-13 08:12:07 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11771
Website

Re: Mount volume dialogue box

The script actually doesn't continue.
If the volume couldn't be mounted, the mount handler returns boolean false which will be saved into the variable flag.
do something will only be executed if flag is true
You can write

Applescript:

if flag is false then return

to abort the script immediately

Last edited by StefanK (2009-03-13 08:13:28 am)


regards

Stefan

Offline

 

#5 2009-03-13 08:39:34 am

crow
Member
Registered: 2009-03-12
Posts: 3

Re: Mount volume dialogue box

Hmm the script is still continuing for me. After your example I am telling an application to launch and place files on this server volume. That application is still launching when it does not have the server volume.

It would seem maybe there is not a way to do this and maybe I just need to put my entire part in the -do something- part of your example. If I am understanding you correctly.

Thanks for your reply.

Offline

 

#6 2009-03-13 08:45:44 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11771
Website

Re: Mount volume dialogue box

this version without the handler aborts the script immediately, if the volume couldn't be mounted

Applescript:


property theServer : "myServer.local"
property theVolume : "Server"
property user : "user"
property pass : "pass"

if serverVolume is not in (do shell script "/bin/ls /Volumes") then
   set theAddress to quoted form of ("afp://" & user & ":" & pass & "@" & theServer & "/" & theVolume)
   set mountpoint to quoted form of ("/Volumes/" & theVolume)
   try
       do shell script "/bin/mkdir " & mountpoint & "; /sbin/mount_afp " & theAddress & space & mountpoint
   on error
       do shell script "/bin/rm -r " & mountpoint
       return
   end try
end if
-- do something


regards

Stefan

Offline

 

#7 2010-06-24 10:05:52 am

Fenton
Member
Registered: 2004-07-26
Posts: 120
Website

Re: Mount volume dialogue box

"serverVolume" should be "theVolume", right? Darn variable name changing (from previous script).

Offline

 

#8 2010-06-24 11:11:00 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11771
Website

Re: Mount volume dialogue box

Fenton wrote:

"serverVolume" should be "theVolume", right? Darn variable name changing (from previous script).


Right smile


regards

Stefan

Offline

 

#9 2010-07-01 10:03:10 am

gleam
Member
Registered: 2009-12-28
Posts: 18

Re: Mount volume dialogue box

Hi,

I was redirected here from this thread:
http://macscripter.net/viewtopic.php?id=10583

@Stefan: Thanks, it seems to work great, but...... problems smile

@all other frustrated people: Basically I (was) having a lot of issues with mounting an afp volume, these are my findings and refinements so far, which i'm using now without any more surprises:


On the first run stefan's script mounts the server alright, then when I select the volume in the finder, and then eject, the alias in the Volumes directory turns into a regular folder icon and stays like that (meaning: the volume is no longer mounted, but some kind of residue of the mount point), so the next time I run the script, it thinks the volume is mounted, while it's not. Fortunately, remounting over the dead mount point works fine, so we're cool there.

    But, accidentally mounting again over an active mount point hangs the script indefinitely. So we'll need an additional check to see if the mount point is alive or dead, before proceeding with the mount. This essentially checks if the mount point is a regular directory or not. (If it's just a folder and not a real volume, mount it again, if it's is already really mounted, do not mount again or we will hang.)
   
    Another complication is that such a residual folder can be present, while the volume can still be already mounted at the same time, but then with a dash and a number appended to the name, like: "MyDisk-1". So an additional routine was added to check numbered variants to see if there really is no mounted volume with the same name and an incremental number added at the end.
   
   
    But man... what a trip from AS's "mount volume" to this, hope this helps somebody & thanks everyone.
   
    If anyone has any better ideas, I'd like to learn!


Applescript:

-- note: any display dialog statements in the code are just for test purposes, you must disable them for production use

-- test parameters
property server_address : "192.168.0.2"
property volume_name : "TestVolumeName"
property username : "workstation"
property userpass : "workstation"

-- usage
really_mount_afp_volume(username, userpass, server_address, volume_name)


-- functions

-- the main function
-- if the mount checker says it's not mounted, mount it
on really_mount_afp_volume(username, userpass, server_address, volume_name)
   
   set volume_is_mounted to false
   
   set volume_is_mounted to check_volume_mount_status(volume_name)
   
   -- evaluate check
   if volume_is_mounted then
       
       display dialog "Volume is already mounted."
       
   else
       
       -- volume was not mounted, attempt to mount it
       set volume_is_mounted to mount_afp_volume(username, userpass, server_address, volume_name)
       
       -- evaluate mount attempt
       if volume_is_mounted then
           
           display dialog "Volume was mounted successfully."
           
       else
           
           display dialog "Volume failed to mount, was not available or did not exist."
           
       end if
       
   end if
   
end really_mount_afp_volume


-- mount an afp volume, only use after additional checks say it's safe
-- this is stefan's mount function from macscipter.net, essentially unchanged, thanks stefan
on mount_afp_volume(username, userpass, server_address, volume_name)
   set location to quoted form of ("afp://" & username & ":" & userpass & "@" & server_address & "/" & volume_name)
   set mountpoint to quoted form of ("/Volumes/" & volume_name)
   try
       do shell script "/bin/mkdir " & mountpoint & "; /sbin/mount_afp " & location & space & mountpoint
       return true
   on error
       try
           do shell script "/bin/rm -r " & mountpoint
       end try
       return false
   end try
end mount_afp_volume


-- check if a volume name is mounted in one way ot another
-- this takes into account dead mount points and incremental numbering of mount point names
on check_volume_mount_status(volume_name)
   
   -- start check variant number counter
   set finished to false
   set check_variant_number to 0
   
   repeat while not finished
       
       -- make folder path addition, skip zero
       if check_variant_number is not 0 then
           set addition to ("-" & check_variant_number) as string
       else
           set addition to "" as string
       end if
       
       -- add to volume name
       set current_volume_name to (volume_name & addition) as string
       
       
       -- see if it exists in the "Volumes" folder
       if current_volume_name is in (do shell script "/bin/ls /Volumes") then
           
           -- a mounted volume or folder by this name exists
           -- if it is just a folder the volume is not really mounted
           
           -- get mountpoint path
           set volume_mountpoint to ((path to startup disk as string) & "Volumes:" & current_volume_name) as string
           
           
           -- see if it is just a folder
           set folder_status to check_folder_status(volume_mountpoint)
           
           if folder_status is true then
               
               -- it's just a folder
               -- continue to next check
               set check_variant_number to check_variant_number + 1
               
           else
               
               -- it's not a folder but a real mounted volume
               return true
               
           end if
           
       else
           
           -- no mounted volume or folder by this name exists
           -- the volume is certainly not mounted
           return false
           
       end if
       
   end repeat
   
end check_volume_mount_status


-- check if a path leads to a real folder
on check_folder_status(folder_path)
   
   -- check if it's a real folder
   tell application "Finder"
       
       if (exists folder folder_path) then
           --display dialog "It's just a folder."
           return true
       else
           return false
       end if
       
   end tell
   
end check_folder_status


Filed under: mount afp volume

Offline

 

#10 2010-07-01 01:54:23 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11771
Website

Re: Mount volume dialogue box

Maybe this could help.
Once I wrote a small command line interface, which displays the available AFP hosts.
In Tiger you could get the information with AppleScript, since Leopard it's not possible anymore.
You can download it here

Just write

Applescript:

set AFPhosts to do shell script "/path/to/AFPHosts"


regards

Stefan

Offline

 

#11 2010-07-05 03:24:19 am

gleam
Member
Registered: 2009-12-28
Posts: 18

Re: Mount volume dialogue box

That sure is interesting. Could you please post the source code for me? I'd like to take a look.

Offline

 

#12 2010-07-05 03:34:30 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11771
Website

Re: Mount volume dialogue box


regards

Stefan

Offline

 

#13 2017-06-22 03:34:15 am

robmorgan
Member
Registered: 2017-06-22
Posts: 1

Re: Mount volume dialogue box

Careful with this script as most of my external hard drive has been deleted and I'm fairly certain this script is the reason.

You've got this in there:

Applescript:

on error
try
do shell script "/bin/rm -r " & mountpoint
end try

and clearly it's supposed to only happen when the mount point is dead etc, but I suspect another error condition of some kind meant this was run when it should not have been.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)