Problem with certain line?

Here’s the script:


with timeout of 1800 seconds -- 30min timeout (Takes approx 9min to run)
	ignoring application responses
		try
			tell me to activate
			display dialog "Would you like to start a " & return & "System Integrity Check now?" buttons {"No", "Yes"} default button "Yes" with icon note giving up after 5
			if the button returned of the result is "No" then
				quit
			else
				try -- Deletes file from the desktop  if it exists
					do shell script "rm  -f -R -P /Users/denns/Desktop/updatedDB.twr"
				end try
				
				tell application "Terminal" -- Initiates a system integrity check
					activate
					do script "cd /usr/local/tripwire/bin;sleep 2;sudo ./tripwire -m c --check --email-report;sleep 10;clear;cd" in window 1
				end tell
				
				delay 600 --(10 minutes)  I want the new report file to be ready before copying to the desktop
				
				try -- Copies the most recent report to the desktop
					set filename to do shell script "ls -tr /usr/local/tripwire/report | tail -1"
					delay 1
					do shell script "/usr/bin/ditto -v -rsrc  /usr/local/tripwire/report/" & filename & " $HOME/desktop/tripwire.twr"
				end try
				
				tell me to activate
				display dialog "Integrity Check is finished." with icon note giving up after 5
			end if
		on error theErrMsg
			display dialog theErrMsg
		end try
	end ignoring
end timeout

This script is being run with root privledges thru Macaroni. The problem I’m having is with the line

try -- Copies the most recent report to the desktop
			set filename to do shell script "ls -tr /usr/local/tripwire/report | tail -1"
			delay 1
			do shell script "/usr/bin/ditto -v -rsrc  /usr/local/tripwire/report/" & filename & " $HOME/desktop/tripwire.twr"
end try

Sometimes it works and sometimes it doesn’t. I have the finder window where this report is found not locked into any sort eg kind, time etc

What do you mean by sometimes it “doesn’t work”?

You mean the last line is not executed? If so, theis would happen when the first shell script throws and error (possibly if the file does not exist) and the rest of the try block is skipped. Add an on error to the try block, and from it display a dialog box.

For ensuring the file exists, run the scripts using double ampersands instead of semicolons. (I know this works in bash, but I ma not sure if it works in the default shell or not). The double ampersand requires that the first command succeed before the next is executed. It may remove the need for the delay 600 (which may be causing the problem). Is there another way to check when the process has finished?

Is there a way to change the default report save location?

Another option: in getting the last tripwire report, you need a -c1 option on the ls command to put the output into one column. The script probably would fail if mofe than one filename appears on the same line (which is dependent on the size of the terminal window).

The problem I seem to be having is when I send the do script command to Terminal applescript doesn’t wait for Terminal to finish working and blows thru the whole script and then quits. And Terminal is still working. So when the script copies a file to the desktop it will not wait for tripwire to finish what it’s doing. This means that a file will be copied, but not the most recent one. And sometimes no file is copied. I wonder if it is a permissions problem? The delay command is there so terminal can finish what it’s doing. I tried the ampersands and they do the same job that the semicolons do. There seems to be no effect on the do script command. For your information Tripwire is a file integrity check. It looks for any changes on a system and compares it to a previous saved database and then alerts me and saves the changes to a report for future viewing.

This is probably because of the ‘ignoring application responses’ wrapper. I can’t say definitely if it applies here (I don’t do any Terminal scripting), but in theory, you need either to increase the delay amount or to zap the delay entirely along with the ‘ignoring’ lines.

For my own education, is there any particular reason for the mixture of shell scripts and Terminal scripting?

Good point on the mixture. You could make the entire thing a shell script and use the terminal command osascript to display the dialogs.

In my original reply I also misread the script. Telling the terminal to do a command actually finishes the command (as far as applescript is concerned. You could use the do shell script command (as a part of the Standard Additions) instead of telling the terminal to perform the command, which will block AppleScript until the command has actually finished.

Thanks to Nigel Garvey & Plaid Cow Solutions,
The reason for the mixture, if I read your question correctly, is Tripwire is a UNIX program. And It only works in Terminal. Hence the ‘do script’ and not the ‘do shell script’ command. Normally I would go to terminal and start it there. But I wanted to automate it and then copy the generated report from the original directory (which is invisible) and then update the database with the report info at my leisure. When Tripwire finishes it generates a report, one is emailed to you and the other to its own report directory, and you have to manually update the original database with the new report information thru Terminal.

    try -- Copies the most recent report to the desktop 
               set filename to do shell script "ls -tr /usr/local/tripwire/report | tail -1" 
               delay 1 
               do shell script "/usr/bin/ditto -v -rsrc  /usr/local/tripwire/report/" & filename & " $HOME/desktop/tripwire.twr" 
   end try 

When I copy the report from the tripwire directory I was changing the name to tripwire.twr. When I removed the change in name and kept the original name it copies just fine. Why would that be?

On the most recent question: trap the error and display it. I’m really not sure. Best guess would be that file name is not what you think it is.
You may need to used quoted form of filename, or just skip the whole mess altogether and put the calculation of the filename in back ticks and have a single expression to find the name and move the file.
OSX is case insensitive for the most part, but you should still strive to get th eproper capitalization of directory names.