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 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.
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
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.
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.
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.
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.
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
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.