I am trying to automate a process where teachers delete content on memory sticks with different name then put the right content back (same each time) and rename the memory stick to UDISK.

I want to add a formatting of the disk in the process, and because the students keep changing names on the sticks, I can´t use name tags and if the teachers have more than one disk on their Mac I am worried if I use "disk1" for instance I suddenly format the wrong partition. So therefor I am wondering if it is possible to choose the right disk based on size.

I am using this code: sudo diskutil eraseDisk FAT32 UDISK MBRFormat /dev/disk1

  • I wouldn't use any numbered reference, as they will change if any other partition is mounted. I had a similar issue & ended up using the USB reader's device name instead... apple.stackexchange.com/questions/207089/… I would read all the comments too, to see how the actual solution was arrived at - it took a few attempts at to & fro before it was nailed. – Tetsujin Apr 6 '16 at 12:29
  • I'll post the resulting code I ended up with, though it doesn't have any 'choice' setup for where to copy from, you'd have to add that yourself. I have it saved from Automator as a Service, which I then trigger by key command after I insert my target SD card – Tetsujin Apr 6 '16 at 12:48

This is the script of a similar bulk erase/copy routine I made with the help of klanomath in this related question - Find specific attached disk, not volume, by Applescript which you need to reference to see how the device name is extracted.

I saved the script as an Automator Service, which I then tied to a non-trivial, in fact very hard to do by accident Cmd ⌘ Opt ⌥ Shift ⇧ F19 key command in
System Prefs > Keyboard > Shortcuts > Services
so I can manually trigger each operation & not be at risk of it being fired accidentally.

The routine finds the potential target by using the device name of my USB card reader, reducing the risk of formatting at least the wrong disk, though it has no safety measure for the individual SD card & will act on any card in the reader when I hit the key combo.

on run {input, parameters}  
    -- see https://apple.stackexchange.com/questions/207089/find-specific-attached-disk-not-volume-by-applescript/207103#207103 for BSD Disk workings 

    global diskName
    set diskName to "DISK_NAME"
    global BSD_disk
    --using ioreg...
    set BSD_disk to do shell script "ioreg -l | grep -A 10 SD/MMC/MS/MSPRO | awk '/disk/ {gsub(\"\\\"\",\"\"); print $NF}'" 

    if BSD_disk is not "" then
            --FAT 16
            do shell script "diskutil eraseDisk \"MS-DOS FAT16\" " & diskName & " MBRFormat " & BSD_disk
        on error
                --FAT 32 [>2GB cards]
                do shell script "diskutil eraseDisk \"MS-DOS\" " & diskName & " MBRFormat " & BSD_disk
            end try
        end try

        delay 2
        tell application "Finder"
            set x to (POSIX file "/Users/[user]/BatchFolder") as alias
                set dest to (POSIX file "/Volumes/" & diskName as string) as alias
            on error return
            end try
            duplicate every item of x to dest
            delay 1         
            if disk diskName exists then
                eject disk diskName
            end if
        end tell
        return "no SD Card detected"
    end if

    return input
end run

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .