Error when running script to check status of application and log it

First, thanks to all the folks here who have helped me get this far with answers to other folks questions.

Here’s what I’m trying to do. I have an application that runs on an unattended machine in the server room. It is prone to crash. But this application moves stuff across the network using a sort of scripting and it is important that is remains running at all times. How it does what it does is not important. What is is that it remain running.

So I have written a script to run constantly, check to see if the program is running and if not, restart the program and log the restart so I can tell how often and when this is happening.

But when the script runs while the application is not running, I get the following error: Can’t make <> of Application “System Events” into type constant.

I’ve searched the archives for similar errors and one I found didn’t seem to apply.

Here is the script.


on idle
	tell application "System Events"
		if (exists process "TextEdit") then
			display dialog "TextEdit is running fine, thank you" buttons {"OK"} ¬
				default button 1 giving up after 5
			
		else
			tell application "TextEdit"
				activate
			end tell
			
			set this_data to ((current date) as string) & space & "TextEdit restarted" & return
			set this_file to (((path to desktop folder) as text) & "ct.log")
			my write_to_file(this_data, this_file, true)
			
			
		end if
	end tell
	return 900
end idle

--Here's the sub-routine:


on write_to_file(this_data, target_file, append_data)
	try
		set the target_file to the target_file as text
		set the open_target_file to ¬
			open for access file target_file with write permission
		if append_data is false then ¬
			set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access file target_file
		end try
		return false
	end try
end write_to_file

This uses the essential subroutine provided by Apple in the AppleScript guidebook on Apple’s site.

Any help would be appreciated.

Don

Model: MacBookPro 17 10.4.9 all up to date
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hi Don

path to desktop doesn’t work within a Finder oder System Events tell block.
For System Events, use this:

tell application "System Events"
	set this_file to ((path of desktop folder as Unicode text) & "ct.log")
end tell

And for the Finder:

tell application "Finder"
	set this_file to (desktop as Unicode text) & "ct.log"
end tell

Why even put up a dialog in the first place?

StefanK : Thanks, that worked like a charm. Now the entire script works like it should.

Mikey-San : This script has been evolving over a few weeks and as I did troubleshooting, I needed some feedback because sometimes the script would run and not work. The dialog was just so I’d have some idea what was going on. And some reassurance that it was actually working (I keep the dock hidden on most of the server-like machines)

Don

For checking how a lot of the scripts are running I use the say command rather than Display dialog.

This is especially useful when stepping through the script.

property bugchecking : true -- set to false to turn off bug checking
on idle
	tell application "System Events"
		if (exists process "TextEdit") then
			if bugchecking then say "TextEdit is running fine, thank you"
			
		else
			if bugchecking then say "TextEdit is not running"
			
			tell application "TextEdit"
				activate
			end tell
			if bugchecking then say "TextEdit restarted"
			set this_data to ((current date) as string) & space & "TextEdit restarted" & return
			tell application "System Events"
				set this_file to ((path of desktop folder as Unicode text) & "ct.log")
			end tell
			
			my write_to_file(this_data, this_file, true)
			
			if bugchecking then say "Script finished"
			
		end if
	end tell
	return 900
end idle

--Here's the sub-routine:


on write_to_file(this_data, target_file, append_data)
	try
		set the target_file to the target_file as text
		set the open_target_file to ¬
			open for access file target_file with write permission
		
		if bugchecking then say "log file open"
		
		if append_data is false then ¬
			set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		if bugchecking then say "Log written"
		close access the open_target_file
		
		if bugchecking then say "log file closed"
		
		return true
	on error
		try
			if bugchecking then say "Error"
			
			close access file target_file
		end try
		return false
	end try
end write_to_file

Mark, thanks for the help. Everything you showed me worked well.

For those of you following along at home, here’s a little additional information and an alternative to having the computer speak to you.

This machine is sitting in a server closet and I’m working on the script using Remote Desktop. So the folks who sit near the server room were wondering why one of the computers was talking. So I changed the SAY command to display dialog with a 2-second time out to save the trouble of explaining and so I could get visual feedback via Remote Desktop.

On to the next problem. When I run the script with these lines commented out, everything works fine, as far as the logging goes.

 tell application "TextEdit"
               activate
           end tell

But these lines are key in that they reactivate the application, which is not really TextEdit. I’m just using TextEdit on my MacBook Pro to write the script. The script is running on an Intel mini with 10.4.7.

The real application is called Transporter and it moves stuff around a database located on a remote server. It is a PPC app. The error I get is -10827 and it occurs at the point of trying to restart the application.

I’ve also tried

launch application "TextEdit"

But that doesn’t work either. If I try to launch TextEdit, as I have done in writing the script, everything works fine.

Would having the application inside its own folder (required) instead of just inside the Applications folder have anything to do with it and how would I correct that?

Anyone got any ideas?

Thanks.

Don

Hi Don,

this sounds very strange. Maybe a sloppy programmed application “Transporter”
For AppleScript the location of a particular application doesn’t matter.
To work around your problem try this:

tell application "Finder" to open application file id "com.apple.textedit"

Boy, I don’t know where you learn this stuff, but that worked. Telling the Finder to open com.baseview.transporter opened the program right up.

Much thanks.

Don