Why does this script sometimes need a delay?

I’m writing a script that does the following

  1. Use the open-source wpd2odt executable to convert an old WordPerfect file to Open Document format and save the output in the Temporary Items folder.

  2. Use a tiny viewer app that I put together in Xcode to open a read-only TextView window which displays the converted ODT file (like a read-only TextEdit). The script launches this viewer app with an "open " command and the name of the converted file as an “–args” argument.

What baffles me is that the script sometimes needs to have delay of 1 to 3 seconds between converting the file and opening it in the application - if I don’t have the delay, the viewer application sometimes opens with a blank file. This seems to be intermittent; sometimes the script works without a delay; then, using the same input file, it works only if I add a 1-3 second delay.

My ignorant guess is that the output file is still busy when I try to open it, but I don’t see how this happen, since the wpd2odt command has to exit (I think) before the next command in the script can take place.

As an experiment, I’ve tried putting this code between the conversion command and the viewer application, but it never displays the dialogs:

		set wpAlias to (POSIX file wpSource) as alias
		tell application "System Events"
			if busy status of wpAlias is true then
				display dialog "Source busy"
			end if
		end tell
		set odtAlias to (POSIX file odtOutput) as alias
		tell application "System Events"
			if busy status of odtAlias is true then
				display dialog "Target busy"
			end if
		end tell

Am I missing something very obvious?

I don’t know what’s causing your problem, but busy status is effectively meaningless under OS X.

… which explains why that code doesn’t do any good. Thank you - I use busy status successfully (I think) in the scripts I use in the SheepShaver emulator running OS 8.6 and OS 9, and didn’t realize it was meaningless now.

If I figure out any more of this, I’ll post it here.

It’s not meaningless, it just doesn’t work like most people think. Busy state doesn’t have anything to with reading or writing to a file like most people think.