Tuesday, September 17, 2019

#1 2019-04-22 03:28:55 am

Joy
Member
From:: South Tirol
Registered: 2008-07-04
Posts: 608
Website

Get mountable disks

I was wondering if there's an easier way to access names of unmounted disks,

Applescript:

do shell script "diskutil list | grep Apple_HFS | awk '{print substr ($4)}'"

My awk syntax isn't right. Some of my disks' names are longer than 1 word else it would be easy to filter the names...

Please some help

Last edited by Joy (2019-04-22 03:30:49 am)

Offline

 

#2 2019-04-25 10:56:08 am

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

Re: Get mountable disks

As you can get the diskutil output also as property list this is a solution using NSPropertyListSerialization of the Foundation framework.

The result is in the variable diskNames


Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

property |⌘| : a reference to current application

set nsListDisks to |⌘|'s NSString's stringWithString:(do shell script "diskutil list -plist")
set diskData to nsListDisks's dataUsingEncoding:(|⌘|'s NSUTF8StringEncoding)
set {discInfo, plistError} to |⌘|'s NSPropertyListSerialization's propertyListWithData:diskData options:0 format:(missing value) |error|:(reference)
if plistError is not missing value then display dialog plistError buttons {"Cancel"} default button 1
set diskNames to {}
set AllDisksAndPartitions to discInfo's objectForKey:"AllDisksAndPartitions"
set predicate to |⌘|'s NSPredicate's predicateWithFormat:"Content == 'Apple_HFS'"
repeat with aDisk in AllDisksAndPartitions
   set partitions to (aDisk's objectForKey:"Partitions")
   set hfsPartitions to ((partitions's filteredArrayUsingPredicate:predicate)'s valueForKey:"VolumeName") as list
   set diskNames to diskNames & hfsPartitions
end repeat

Last edited by StefanK (2019-04-26 01:14:59 am)


regards

Stefan

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)