Testing for a mounted share

Is there an easy way to test to see if a network share is mounted and available?

I used to be able to just do an “exists” query in Applescript, but it doesn’t seem to work in ApplescriptObjC.

thanks.

–jon

Hi,

NSWorkspace has two methods to get information about devices

mountedRemovableMedia() – Returns the full pathnames of all currently mounted removable disks.
mountedLocalVolumePaths() – Returns the mount points (full pathnames) of all local volumes, not just the removable ones returned by mountedRemovableMedia.

Stefan:

Thanks for the suggestion. My only concern is that in the xCode docs,

mountedRemovableMedia()

is marked as deprecated in OS X v10.7 which I guess means it’s not going to be supported going forward in Mountain Lion.

All I’m trying to do is just test that the server is mounted so I can kick out a warning to the user to mount it (I’m not going to try to force a mount in the script itself).

Any other quick way that you can think of to accomplish this?

Thanks.

the replacement is probably
mountedVolumeURLsIncludingResourceValuesForKeys:options:
of NSFileManager

Could someone give me a hint on how to access the mountedVolumeURLsIncludingResroucheValuesForKeys reference? My code succeeds on build, but I don’t get a dialog box with the first item of the array.

script AppDelegate
	property parent : class "NSObject"
    
    
    tell current application's NSArray to set varOne to mountedVolumeURLsIncludingResroucheValuesForKeys()
    
    set varTwo to item 1 of varOne as text
    
    display dialog varTwo
    
    
 	
	on applicationWillFinishLaunching_(aNotification)
		-- Insert code here to initialize your application before any files are opened 
	end applicationWillFinishLaunching_
	
	on applicationShouldTerminate_(sender)
		-- Insert code here to do any housekeeping before your application quits 
		return current application's NSTerminateNow
	end applicationShouldTerminate_
	
end script

Also, if I’m testing to see if a volume is mounted and it isn’t when my ApplescriptObjC app launches, is there anyway for the app to recognize that it has been mounted (e.g., the app launches, sees there’s no volume, issues a dialog informing the user of that fact, the user then mounts the needed volume but if they then just hit the app generate button again, the app isn’t going to know the volume has been mounted).

I suppose I could force the app to quit after issuing the warning and make the user relaunch, but that’s a pretty inelegant way of handling it.

Thanks.

–jon

several problems:

first of all the code must be within a handler.
The method name has a typo and you missed the second parameter
and a couple of other problems


	
	on applicationWillFinishLaunching_(aNotification)
        set varOne to current application's NSFileManager's defaultManager()'s mountedVolumeURLsIncludingResourceValuesForKeys_options_({"NSURLNameKey"}, 0)
        set varTwo to item 1 of varOne
        display dialog varTwo's lastPathComponent() as text

    end applicationWillFinishLaunching_

PS: or to skip invisible volumes and display all volume names


	on applicationWillFinishLaunching_(aNotification)
        set mountedVolumeURLs to current application's NSFileManager's defaultManager()'s mountedVolumeURLsIncludingResourceValuesForKeys_options_({"NSURLNameKey"}, 2)
        set mountedVolumeNames to current application's NSMutableArray's array()
        repeat with aVolume in mountedVolumeURLs
            mountedVolumeNames's addObject_(aVolume's lastPathComponent())
        end
        set mountedVolumeNames to mountedVolumeNames's componentsJoinedByString_(return)
        display dialog mountedVolumeNames as text
        
    end applicationWillFinishLaunching_


Worked like a charm.

Plus, seeing what you did finally clicked in how to use the cocoa commands.

Many thanks.

–jon