How to create an applescript for multiple dd terminal commands in sequence ?

Discussion in 'Mac Programming' started by AlbertoBC, Feb 2, 2014.

  1. AlbertoBC macrumors newbie

    Feb 2, 2014
    I need to create an apple script to perform DD Commands in sequence, to replicate the same .img file to multiple USB flash drives

    The context: the computer is connected to a powered USB hub with 7 usb flash drives connected and unmounted by diskutil. No other USB devices or disk are connected to the computer (so the USB Sticks will be assign a disk1 - disk7 resource mapping)

    The steps

    - a file named "source.img" will be prepared and put on the desktop (say an .img of a bootable OS)

    - terminal must be asked to change directory to the desktop

    - with administrative privileges (the password can be inserted into the script, or asked once to the user) it must be asked to perform:

    "sudo dd if=source.img of=/dev/rdisk1 bs=1m"

    - the script must wait until the operation is concluded, then ask the terminal to perform another DD, to disk2 this time:

    "sudo dd if=source.img of=/dev/rdisk2 bs=1m"

    and again, wait until the operation is concluded, and then ask:

    "sudo dd if=source.img of=/dev/rdisk3 bs=1m"

    and so on with rdisk4, rdisk5, rdisk6, rdisk7.

    - at the end of the disk7 operation , the script can shut down the terminal and send a finder message to the user (or an audio notification) that the USB Duplication process to the 7 flash drives is concluded.

    It's a way to create a "USB duplicator on the cheap" for bootable images to be put on multiple sticks, I need it for a school project to my students.

    Anybody can help ? I am a zero with Applescript. And this thing will be useful for many.

    Thank you !
  2. theluggage macrumors 68030

    Jul 29, 2011
    Why not create a good old fangled shell script to do the job?

    Using a text editor (if you use TextEdit, remember to do 'Format->Make Plain Text' - you don't want a Rich Text File) create something like:

    cd ~/Desktop
    echo This will overwrite /dev/rdisk1, /dev/rdisk2, … 
    echo If you don't want that to happen hit ctrl-c now!
    echo otherwise, hit Return
    read PROCEED
    sudo echo dd if=source.img of=/dev/rdisk1 bs=1m
    sudo echo dd if=source.img of=/dev/rdisk2 bs=1m
    sudo echo dd if=source.img of=/dev/rdisk3 bs=1m
    echo All done! it as "bulkcopy.command". Before you can run it for the first time, you'll
    have to open Terminal and
    cd /directory/where/you/saved/it
    chmod u+x bulkcopy.command
    ...but you'll only have to do this once. After that, you should be able to double-click on 'bulkcopy.command' to run it.

    Note - as you may have spotted, actually that only displays the commands on the terminal. If you actually want it to work remove the 'echo' from before each 'dd' each command - but only after you have thought long and hard about whether you really want a script lying around that will overwrite whatever disk is attached to /dev/rdisk1. - that goes for any AppleScript solution, too.

    I recommend Googling around about bash scripting a bit - you can make a slightly better 'are you sure' prompt and also fix it to run the DDs as background tasks so it can copy several sticks simultaneously.
  3. chown33 macrumors 604

    Aug 9, 2009
    #3 can run a shell script as a step (action) in a workflow. It can also do steps like present a dialog or alert, prompt for a user-entered string, etc.

    I suggest putting the parts together in an Automator workflow, with only the minimal amount in a Run Shell Script action.

    The 'sudo' to run with elevated privileges is a bit non-obvious. There is an AppleScript command that can run a shell script (do shell script) with the option of asking for an admin password using a dialog. Since Automator also has a Run AppleScript action, you could:
    1. Wrap the dd command in a 'do shell script' with admin privileges.
    2. Put that piece of AppleScript in a Run AppleScript step in Automator.
    3. Do any other necessary steps before or after in Automator.

    In short, there are several ways of approaching this that don't involve large amounts of either bash scripting or AppleScript.

Share This Page