How can I make applescript wait for an application

Hi there,

I’ve created an applescript that batch processes files.

Whilst testing the script I’ve noticed that the script can get ahead of the app.
For example, the app may be saving a document which may take a few seconds however applescript has moved on. I’ve been able to spot this more easily by placing some ‘display dialogs’ in certain places.

Is there a way I can find out what events I need to make applescript wait for? For example, in the case above it would wait for the ‘save’ to finish. I’ve found one or two snippets that wait for a dialog window to disappear but if there isn’t one I need an event to watch for. I’ve had a look through the app’s dictionary but haven’t found anything.

I’m a little contious that the script may get too far ahead and start trying to do things that aren’t available.

Thanks in advance,

Nick

I have used this code to make sure a file has finished saving before the script moves on and tries to use that file.

tell application "Finder"
		activate
		repeat while not (exists file (PathToFile & FileName))
			delay 0.5
		end repeat
	end tell

Model: Mac G5 OS 10.3.9
Browser: Safari 312.3.1
Operating System: Mac OS X (10.3.9)

Hi Matt-Boy,

Thanks for the reply.
I’ve not tried your solution yet, just wondered if it might not work because the file already exists at the given location. At present the script overwrites the file.

Regards,

Nick

If the size of the file changes, this might help:


on fileChanged(myFile)
	-- initialize the loop
	set Size_1 to size of (info for myFile) -- get initial size
	delay 3 --wait 3 seconds, or whatever
	set Size_2 to size of (info for myFile) -- get a newer size, perhaps different
	-- repeat until sizes match
	repeat while Size_2 ≠ Size_1 --if they don't equal, loop until they do
		set Size_1 to Size_2 -- new base size
		delay 3 -- wait three seconds, or whatever
		set Size_2 to size of (info for myFolder) -- get a newer size
	end repeat --once the sizes match, the download is done
end fileChanged

Hi Adam,

Thanks for the help with this.
I’ll have a look at the files to see if the size changes much, if so this will probably be a nice solution. I just wanted to put something in that puts a hold on applescript getting too far ahead.

A while ago I thought I’d found something to do with ‘events’ and I’d been trying to track down further information regarding that. Am I right in thinking that applescript can watch for certain events? Any help would be appreciated.

Thanks in advance,

Nick

I haven’t tried to monitor ‘events’ Nick, so don’t know how. I too remember seeing something about monitoring whether a file was ‘busy’, but don’t recall where.

I don’t know when the Finder changes a modification date either, but if it’s at the end of a file save, you could replace size with modification date in the script example above.

Hi Adam,

Thanks for your reply.
Thanks for the other solution, I’ll have a play around with that and see if I can get something working.
I’m glad someone else remembers something about monitoring events.

Thanks again,

Nick

Searching for file is busy, I found these three approaches:

http://bbs.applescript.net/viewtopic.php?id=16660

http://bbs.applescript.net/viewtopic.php?pid=33693#p33693

http://bbs.applescript.net/viewtopic.php?id=14443

Hi Adam,

Thanks for posting the links.

The first 2 look interesting, I’ll have a play with around with them and see which seems best, at first glance it’s looking like the second version.
The third link seems to take me to a blank page with nothing found.

Thanks again,

Nick

Sorry about that link; it’s in archives I can reach but you can’t. Here’s the relevant script modified for waiting:


repeat 20 times
	try
		open for access file x -- if this succeeds, the file is not busy.
		close access result
		exit repeat
	on error -- file is busy
		delay 3 -- times it out in 1 minute
	end try
end repeat

Hi Adam,

Thanks for the reply and for posting the code.

I’ve been playing around with the code and have something that seems to do the trick.
I’m going to test the code with a few more files of varying sizes.

Thank you once again,

Nick

thankkkkkkk you
been looking for something similar for long time
( I am still a newbeeee