MacOS – Automating Terminal Commands with AppleScript

applescriptautomatorcommand linemacosterminal

I have an Arduino-based device (www.picobrew.com) and needs firmware updating. The company wrote a little app to do it for Windows users, but us OS X users are out in the cold. Fortunately, we were able to use the Arduino app and some terminal commands to get around not having their app. Some people, however, are a little afraid to tinker in Terminal. I found this post, which seems like a good start, but I have two variables, the file name of the firmware file and the name of the Arduino serial port that I'm not quite sure how to get into the script without hardcoding. Here is the script:

/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avrdude -C /Applications/Arduino.app/Contents/Java/hardware/tools/avr/etc/avrdude.conf -c arduino -P /dev/tty.usbmodemFD121 -p atmega1284p -U flash:w:$HOME/Desktop/Zymatic_RC_1_1_9.hex

I can find the name of the serial port with this command:

ls /dev/tty.usb*

But how can I populate the first script with the result of this one?

Additionally, is there a way to prompt for a file location?

Apologies if any of this is super basic. I have extremely limited Terminal experience and zero AppleScript experience. Thanks for any help.

Best Answer

These examples accomplish what you want. Note the " & quoted form of portName & " portion of the shellString. Was just guessing that's where it goes, but you can figure it out from there.

-- set the porn name to a variable
set portName to do shell script "ls /dev/tty.usb*"

-- set up the shell command, including the variable you insert
set shellString to "/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avrdude -C /Applications/Arduino.app/Contents/Java/hardware/tools/avr/etc/avrdude.conf -c arduino -P /dev/tty." & quoted form of portnName & " -p atmega1284p -U flash:w:$HOME/Desktop/Zymatic_RC_1_1_9.hex"

-- run the shell command
do shell script shellString

-- this command returns an alias to a folder
set aFolder to choose folder "Please choose the correct folder"
-- this command returns an alias to a file, you can restrict file types
set aFile to choose file "Please choose the correct file"
-- you can convert the folder/file alias to a string to get just the portion you need