2

I have a script I use to format any inserted SD card, copy data from specific folders to it, then eject.

Each format/copy is triggered manually, as part of a batch process involving maybe a hundred or so cards, so I'm not concerned about hitting the Start button at the wrong time, or on the wrong card.

What I am trying to find is how to make absolutely sure that I'm writing to the correct disk

This is my 'important' line
do shell script "diskutil eraseDisk \"MS-DOS FAT16\" " & diskName & " MBRFormat disk6"

The disk6 is not absolute, as I don't always have the same number of drives mounted. I can check it manually before I start the batch, using diskutil list then change that parameter in the script.

What I'd like to be able to do is have the script itself check what drive it ought to be, then insert that as a variable, avoiding any 'wrong choices'.

Excluded drives would be non-removables & RAM Disks [presumably unmounted volumes such as EFI & Recovery wouldn't need to be filtered] - these have known volume names - but I cannot figure out how to set a found volume name [known or unknown, as the SD cards can be called pretty much anything before I start] to a specific disk number.


Specifics on the card reader, built into my monitor, Dell U2713H
Attached over USB2 from a Mac Pro 4,1 [with 5,1 hardware update]

USB Hi-Speed Bus:

  Host Controller Location: Built-in USB
  Host Controller Driver:   AppleUSBEHCI
  PCI Device ID:    0x3a3c 
  PCI Revision ID:  0x0000 
  PCI Vendor ID:    0x8086 
  Bus Number:   0xfa 

Hub:

  Product ID:   0x8043
  Vendor ID:    0x0451  (Texas Instruments)
  Version:  1.00
  Serial Number:    79000089BFB9
  Speed:    Up to 480 Mb/sec
  Location ID:  0xfa200000 / 2

//snips other devices//

USB3.0 Card Reader:

  Product ID:   0x0307
  Vendor ID:    0x0bda  (Realtek Semiconductor Corp.)
  Version:  1.63
  Serial Number:    201006010301
  Speed:    Up to 480 Mb/sec
  Manufacturer: Realtek
  Location ID:  0xfa233000 / 5

Sample output of df

Filesystem    512-blocks       Used  Available Capacity   iused     ifree %iused  Mounted on
/dev/disk1s2  1950314272  485618064 1464184208    25%  60766256 183023026   25%   /
devfs                412        412          0   100%       714         0  100%   /dev
/dev/disk0s2  3906357344 1195025808 2711331536    31% 149378224 338916442   31%   /Volumes/JuSpace
/dev/disk3s2  1951855464  635192376 1316663088    33%  79399045 164582886   33%   /Volumes/Downloads
/dev/disk2s2  5858067520 2309215360 3548852160    40% 144325958 221803260   39%   /Volumes/OhDaSpace
/dev/disk4s2  5753889792 4638480736 1115409056    81% 289905044  69713066   81%   /Volumes/TMach
map -hosts             0          0          0   100%         0         0  100%   /net
map auto_home          0          0          0   100%         0         0  100%   /home
/dev/disk3s4   408324240  210669592  197654648    52%    200589  98828207    0%   /Volumes/MacWin7
/dev/disk5       2097152     700536    1396616    34%     87565    174577   33%   /Volumes/RAM Disk
/dev/disk7s1     1936768     104192    1832576     6%       512         0  100%   /Volumes/F_CARD

Or... Using ioreg -l with the following result

| |   |   |             +-o Generic- SD/MMC/MS/MSPRO Media  <class IOMedia,$
| |   |   |               | {
| |   |   |               |   "Removable" = Yes
| |   |   |               |   "Content" = "FDisk_partition_scheme"
| |   |   |               |   "Whole" = Yes
| |   |   |               |   "Leaf" = No
| |   |   |               |   "BSD Name" = "disk7"

how do I fix something like grep -A 100 SD/MMC/MS/MSPRO Media | grep disk | awk... to find the disk7

  • Do the SD cards have any characteristic property like size, manufacturer, volume format? – klanomath Sep 23 '15 at 9:07
  • I'm afraid not. Multiple manufacturers, multiple sizes (I have a little 'try' routine for FAT32 if they're too big for FAT16), multiple existing formats, multiple existing names. It may be a process of eliminating the known [list of pre-determined potential volumes] & what remains is the target. – Tetsujin Sep 23 '15 at 9:11
  • Do you use an external SD-card reader or an internal one (which Mac?) or both? – klanomath Sep 23 '15 at 9:14
  • reader is built into my screen, Dell U2713H. I'll add details to the question... – Tetsujin Sep 23 '15 at 9:26
2

I'm not that great in writing Apple scripts but here is an idea how to solve your problem.

In the first method outlined below I search the device tree of system_profiler to get the DiskIdentifier of a disk device attached to the USB3 card reader built into your Dell monitor. In the second method I use ioreg.

system_profiler:

To get the DiskIdentifier of a SD-card attached to the external SD-Reader use something like:

system_profiler -detailLevel mini | grep -A 30 0x0307 | awk '/disk/ {gsub("BSD Name:", ""); print $NF}'

with grep -A 30 0x0307: output the next 30 lines after finding the string 0x0307
and awk '/disk/ {gsub("BSD Name:", ""); print $NF}' to get the disk number but remove BSD Name: from the output.

The command takes about 10 seconds on my system.

You may have to adjust the value of -A and the characteristic string (in the example above I used your USB3.0 Card Reader Product ID: 0x0307). Check the device tree with the app System Information and search for an appropriate string.

Wrap all that in a variable like $SDDiskToErase and hand it over to your diskutil eraseDisk command.

Using the above slightly modified line I got the diskIdentifier (disk2) of an external USB disk directly attached to my Mac:

system_profiler -detailLevel mini | grep -A 30 0x1c26 | awk '/disk/ {gsub("BSD Name:", ""); print $NF}'
disk2

ioreg:

Another initial command is ioreg -l | grep … (… is similar to the above) which is much faster (less than a second).

Attach an SD-card and get its DiskIdentifier with diskutil list. Then make a dump of ioreg with ioreg -l > ~/Desktop/ioreg-dump.txt.

Search in ioreg-dump.txt for the diskIdentifier found above. The disk is connected to a superior controller (the one in your monitor) visualized by the long vertical and short horizontal "lines" on the left in the dumb file. Use a characteristic and unique string of the controller like a serial number, IOName, deviceID etc. Then choose a number of subsequent lines for -A big enough to contain the diskIdentifier (e.g. disk6) but small enough not to contain the volume identifiers (e.g. disk6s1).

Example:

ioreg -l | grep -A 350 0x100000214 | awk '/disk/ {gsub("\"",""); print $NF}'
disk2

To use the command in Apple Script you have to escape all inner " and \:

"ioreg -l | grep -A 10 SD/MMC/MS/MSPRO | awk '/disk/ {gsub("\"",""); print $NF}'"

becomes:

do shell script "ioreg -l | grep -A 10 SD/MMC/MS/MSPRO | awk '/disk/ {gsub(\"\\\"\",\"\"); print $NF}'"
  • I tried the grep - a bit blindly, I must admit - & it spat a couple of odd errors; so I just ran system_profiler on its own, hoping to see where they came from... maybe not the wisest... a full minute later, I had an Encyclopaedia Britannica of info to sift. – Tetsujin Sep 23 '15 at 10:32
  • @Tetsujin Hmm attach an SD-card, check the diskIdentifier with diskutil list then open System Information.app and search for the diskxxx in the device tree and then use some characteristic string of an superior device nearby. You skipped the most important part in the "specifics" above: The superior device where the SD-disk is attached. – klanomath Sep 23 '15 at 10:39
  • many thanks for your help so far; a lot of this, as you can probably imagine is a bit beyond my usual comfort zone. From further Googling - might I do better with df just getting a list of mounted drives? My problem will be the grep & the variable $SDDiskToErase , neither I have much experience with. Editing in a sample of df to my question... – Tetsujin Sep 23 '15 at 10:40
  • @Tetsujin Would you mind attaching an SD-card to the card reader and posting the part of the system info containing the diskIdentifier AND the superior USB-device tree? Is the complete bash script and the Apple script available somewhere? – klanomath Sep 23 '15 at 11:18
  • I'm not sure this is going to be our solution, tbh - it's taking almost a minute to process the system profile output, which would add about 2 hours to a standard copy run. I'm getting somewhere close using set BSD_disk to do shell script "df | grep -Ev 'KickMeHard|MacWin7|Downloads|OhDaSpace|JuSpace|TMach|RAM Disk' | grep -o 'disk[1-9]'" though it's currently returning disk1 and disk7 [the current correct answer] – Tetsujin Sep 23 '15 at 12:26

You must log in to answer this question.

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