Help scripting multiple terminal commands hidden with status

Hi All, I have a script to run two terminal commands. I tried to write them consecutively, but received errors. I dont think it had completed the first one before trying the second. I put in delays, but thats not a solution as the time to run could vary. I would like to not see the terminal window, but would like to get the status. If the last 6 lines of Terminal contain the word “error”, display ERROR. I have the applescript checking to see if a Terminal window is already open so it doesn’t open another, but it doesn’t work. If I run Terminal hidden it doesn’t matter how many it uses.

Second question: I have been searching and reading to figure this out, but I’m confused with the terminal script. I don’t think my terminal commands are a shell script because it uses different language than I have seen in my searching. What kind of script am I running in my terminal command?
Thanks for your help.

tell application "Terminal"
	if not (exists window 1) then reopen
	activate
	delay 1
	do script ("avrdude -P usb -c usbasp -p ATmega328 -U lfuse:w:0xc2:m -U efuse:w:0x07:m -U hfuse:w:0xdf:m")
	delay 2
end tell
tell application "Finder"
	activate
	set myFolder to (quoted form of (POSIX path of (parent of (path to me) as string)))
	set progFile to (myFolder & "RHM.hex")
end tell
tell application "Terminal"
	if not (exists window 1) then reopen
	activate
	delay 1
	do script ("avrdude -v -c usbasp -p atmega328 -e -U flash:w:" & progFile)
	delay 4

Hi adown.

Is it feasible to put both “avrdude” commands in the same script, separated by semicolons?


tell application "Finder"
	set myFolder to (container of (path to me)) as alias
end tell
set progFile to (quoted form of ((POSIX path of myFolder) & "RHM.hex"))

tell application "Terminal"
	if not (exists window 1) then reopen
	activate
	do script ("avrdude -P usb -c usbasp -p ATmega328 -U lfuse:w:0xc2:m -U efuse:w:0x07:m -U hfuse:w:0xdf:m ; avrdude -v -c usbasp -p atmega328 -e -U flash:w:" & progFile) in window 1
end tell

The script you posted uses ‘do script’ twice without specifying a window or tab. On my machine, this means the scripts are executed in separate windows ” and thus in separate shells. Maybe this has some bearing on why the second script doesn’t wait for the first to finish. (I don’t actually know. I seldom use the Terminal myself.)

‘do script’ is a command from Terminal’s AppleScript implementation. If you don’t want to use Terminal, you could try ‘do shell script’, which is a command from AppleScript’s StandardAdditions and doesn’t involved the Terminal at all. You’d use it something like this (assuming the code above is more or less correct):


tell application "Finder"
	set myFolder to (container of (path to me)) as alias
end tell
set progFile to (quoted form of ((POSIX path of myFolder) & "RHM.hex"))

do shell script ("avrdude -P usb -c usbasp -p ATmega328 -U lfuse:w:0xc2:m -U efuse:w:0x07:m -U hfuse:w:0xdf:m ; avrdude -v -c usbasp -p atmega328 -e -U flash:w:" & progFile)

“avrdude” isn’t installed on my machine ” or at least there’s no “man” entry for it. If it is on yours, you may have downloaded it from somewhere or it may have come with some other software you have.

Hi, thanks for your reply. That cleared up my confusion with sell script.

avrdude is an eeprom burning program. Your code is certainly cleaner, and the semicolons worked, but the (container of (path to me)) is only getting to the desktop(where the script saved as app is).

Here is the error I received: read from file ‘/Users/Adown/Desktop/RHM.hex’ failed. It should have the /***.app/Contents/Resources/Scripts/RHM.hex in it as well. I think it has something to do with the consecutive Terminal commands. It finds the “myFolder” just fine in my original script with two separate commands.

How can I hide the terminal window from showing. I’ve tried hidden, but it didn’t work.
How can I search the last 6 lines of the Terminal window for the word error, failed, or success to display dialog of the outcome?
Thanks again, adown

Well. I changed ‘parent’ to ‘container’ because ‘parent’ isn’t a Finder term ” although that line only runs with ‘parent’ in it if it’s in a “Finder” tell block. You didn’t say the script was to be saved as an application or a script bundle. Even so, on my machine, both ‘parent’ and ‘container’ of ‘path to me’ return the desktop if that’s where the script is, whatever its format.

If “RHM.hex” is in the current bundle’s “Scripts” folder, you should be able to get the path to it like this, without using the Finder at all:

set progFile to quoted form of POSIX path of (path to resource "RHM.hex" in directory "Scripts")

The only suggestions I have are not to use the Terminal, as described in my previous reply, or to cut the ‘activate’ command from the Terminal code.

The ‘content’ of the relevant window gives the current text. But further experimentation indicates that ‘do script’ doesn’t wait for the done script to finish, so you’d need to insert an adequate ‘delay’ before reading the window’s contents for the outcome text. This doesn’t sound very satisfactory. If you can get the same text from ‘do shell script’, which does wait, it would be much better.

Thanks Nigel, running without ‘tell Finder’ cleaned it up to almost nothing, but it works. I removed the 'activate, but it still shows the Terminal window. It errors with can not understand ‘sh’ in shell, but thats ok. being hidden isn’t a must, just a want… Thanks so much for your help, adown

To get the same shell as in the terminal you can prefix a do shell script command with '/bin/bash -l -i -c '. do shell script uses bash who mimics the old sh. So the shell in Terminal and the shell in a do shell script are not exactly the same and some commands can behave differently. One of the things can be that $PATH variable are different between shells which can be solved by not using “avrdude” but the complete path to the the avrdude executable.