APFS Toggle Mount Status of Local Volumes

I haven’t seen anything about toggling local volume mount status in a pretty long time, but I finally configured an AppleScript that makes it simple for me. If something else handles this without any time lag, I’d sure like to know about it, but if this is something you can use, you’re certainly welcome to it.

AppleScript to Toggle Local APFS Volumes
(in case you want usage information)

##  https://askubuntu.com/questions/530141/how-to-command-grep-not-to-display-the-searched-string
set alldisks to paragraphs of (do shell script "diskutil list|grep -v 'EFI EFI'|grep -v 'Recovery HD'|grep -o 'disk[0-9]*[0-9][a-z][0-9]'")

set x to alldisks
my rubify(x)
set alldisks to result

set mountables to {}
repeat with i in alldisks
    if ((do shell script "diskutil info" & space & i & space & " | egrep -i 'mounted:'") as text) does not contain "(no file" then
        copy (i as text) to the end of mountables
    end if
end repeat

my rubify(mountables)
set mountables to result

set workables to {}
repeat with i in mountables
    ((do shell script "diskutil info" & space & i & space & "| egrep -i 'Protocol'") as text)
    if result does not contain "Fabric" then
        copy (i as text) to the end of workables
    end if
end repeat

set gelax to true
repeat with i in workables
    set ISmounted to (characters -3 thru -1 of (do shell script "diskutil info" & space & i & space & "| egrep -i 'mounted:'") as text)
    if ISmounted is "Yes" then
    else
        set gelax to false
        do shell script (("diskutil mount") & space & i)
    end if
end repeat

if gelax then
    repeat with i in workables
        set FinderEjectable to (paragraphs of (do shell script "diskutil  info" & space & i & space & "| grep -i 'Volume Name:'|cut -c31-") as text)
        --display dialog FinderEjectable
        --do shell script (("diskutil unmount") & space & i)
        tell application "Finder" to eject disk FinderEjectable
    end repeat
end if

on rubify(l)
    script JO
        property JO1 : l
        property Redwell : {}
    end script
    considering case
        repeat with i from 1 to count (JO's JO1)
            set x to ((JO's JO1)'s item i)
            if x is in JO's Redwell then
            else
                set end of JO's Redwell to x
            end if
        end repeat
    end considering
    JO's Redwell
end rubify