How to list all unmounted disks/volumes?

Discussion in 'Mac Programming' started by ?gruebel?, Aug 3, 2014.

  1. ?gruebel? macrumors newbie

    Joined:
    Aug 3, 2014
    #1
    Hi all

    Is there a way to list all unmounted disks/volumes with AppleScript?

    Thanks for your help.
    ?gruebel?
     
  2. Weaselboy Moderator

    Weaselboy

    Staff Member

    Joined:
    Jan 23, 2005
    Location:
    California
    #2
    This should do the trick.

    Code:
    do shell script "diskutil list"
     
  3. ?gruebel? thread starter macrumors newbie

    Joined:
    Aug 3, 2014
    #3
    Code:
    do shell script "diskutil list"

    Thanks, it works so far. But how to go from there . . .

    p.ex.
    Code:
    set all to do shell script "diskutil list"
    set one to choose from list (result)
    gives
    /dev/disk0

    How can I get the name of the disk/partition and how can I mount it?

    Thanks a lot for your help. I'm eager to learn.
     
  4. Weaselboy Moderator

    Weaselboy

    Staff Member

    Joined:
    Jan 23, 2005
    Location:
    California
    #4
    I found this that should help you.

    Also, I know a lot of forum members use the fee app Semulov to manage volumes. Might be all you need.
     
  5. SDDave2007, Aug 4, 2014
    Last edited: Aug 5, 2014

    SDDave2007 macrumors regular

    Joined:
    Apr 12, 2007
    #5
    Here is a script that will mount/unmount all external drives

    oh well
     
  6. ?gruebel? thread starter macrumors newbie

    Joined:
    Aug 3, 2014
    #6
    Thanks a lot for the input

    Interesting scripts, food for brain.
    But I don't want to have all disks mounted/unmounted.
    I prefer to have a list presented to choose the one I want to mount or unmount (without having to pre-difine all my disks in the script).
    I don't know if that's possible. - I haven't found any hint in the man page for mount/unmount that you can show/list unmounted disks.
    OSX disk utility shows unmounted disks - but unfortunatley it's not an scriptable app.

    Anyway, thanks and greetings. :)
     
  7. briloronmacrumo, Aug 5, 2014
    Last edited: Aug 5, 2014

    briloronmacrumo macrumors 6502

    briloronmacrumo

    Joined:
    Jan 25, 2008
    Location:
    USA
    #7
    Possible with Objective-C/Cocoa

    If you can write a little Objective-C it isn't that difficult to call Disk Utility and parse its output ( possibly starting with parsing based on CR( carriage return ) using either CFStringCreateArrayBySeparatingStrings() or its similar Cocoa NSString componentsSeparatedByString: ) and put it in a tableview. Disk Utility could be called with a pipe or using a Cocoa NSTask. Disk Utility identifies all the volumes but AFAICT doesn't distinguish unmounted from mounted. That would take additional code. btw: since you apparently are looking for an Applescript solution, I didn't post code.
     
  8. briloronmacrumo macrumors 6502

    briloronmacrumo

    Joined:
    Jan 25, 2008
    Location:
    USA
    #8
    Another approach: use Apple's DiskArbitration calls for volume information. ( i.e. #import <DiskArbitration/DiskArbitration.h> ) and register for the NSWorkspace notificationCenter. Process the notifications you are interested in using NSWorkspaceDidRenameVolumeNotification, NSWorkspaceDidMountNotification, NSWorkspaceWillUnmountNotification and NSWorkspaceDidUnmountNotification and update the tableview accordingly.

    Here is a link for DiskArbitration code: https://stackoverflow.com/questions/11633425/get-a-list-of-unmountable-drives-using-cocoa. You would need to retrieve different keys from the dictionary ( descDict ) as shown in this code but the basic approach is right. HTH.
     
  9. ?gruebel? thread starter macrumors newbie

    Joined:
    Aug 3, 2014
    #9
    Hello briloronmacrumo

    Thank you for your effort. Your tips are a bit over my level of knowledge. But I'll to follow your hint.
     
  10. ?gruebel? thread starter macrumors newbie

    Joined:
    Aug 3, 2014
    #10
    That's the way I'm doing it now . . .

    Code:
    --- unmount - mount partitions
    set all to paragraphs of (do shell script "ls /Volumes")
    set w to choose from list all with prompt "wich one?" cancel button name "or activate?" OK button name "go away" with multiple selections allowed
    try
    	repeat with teil in w
    		do shell script "diskutil unmount `diskutil list | awk '/ " & teil & "  / {print $NF}'`"
    	end repeat
    end try
    
    if w = false then --- if you press "or activate?"
    	
    	set Part to display dialog "name it!" default answer " " buttons {"none", "this one"} default button 2
    	set this to text returned of Part
    	set butn to button returned of Part
    	if butn = "none" then error number -128 --- Cancel
    	if butn = "this one" then
    		try
    			repeat with wakeup in this
    				do shell script "diskutil mount `diskutil list | awk '/ " & this & " / {print $NF}'`"
    			end repeat
    		end try
    	end if
    end if
    
     
  11. Partron22 macrumors 68000

    Partron22

    Joined:
    Apr 13, 2011
    Location:
    Yes
    #11
  12. ?gruebel? thread starter macrumors newbie

    Joined:
    Aug 3, 2014

Share This Page