Hi,
I was redirected here from this thread:
http://macscripter.net/viewtopic.php?id=10583
@Stefan: Thanks, it seems to work great, but… problems
@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!
-- 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