Long 'with timeout' seemingly not working???

I have the following (snippet of) code:

							
set deleteSucceeded to true
try
	with timeout of 15 * 60 seconds
		tell application "System Events" to delete earliestFolder
	end timeout
on error m number n from f to t partial result p
	set deleteSucceeded to false
	if n = -1712 then
		-- tell the user we had a problem
		display dialog "Ran out of time to finish deleting the earliest folder"
	else
		-- otherwise, pass the error on
		error m number n from f to t partial result p
	end if
end try

earliestFolder contains a lot of stuff, so it takes a while to delete it. That’s fine, but about 2 minutes after the deletion begins I get the following error message:
AppleEvent timed out.
System Events got an error: AppleEvent timed out. (-1712)

The deletion continues past this, and once it’s finished the script does the rest of its work. But I would really like to not have this error message!

I’ve used timeouts successfully in the past and never had this issue. I’d be grateful for any help figuring out what’s wrong here.

Strange situation.

What happens if you remove the try statement?

The ‘with timeout’ guidance in the ASLG implies that the try statement should be inside the ‘with timeout’ block.

What I’m guessing is happening is the script is ignoring the timeout statement and after two minutes, hits the default and thus throws the error. I just ran a script that tried to save textedit documents with ask and set the timeout to higher than the default. Ran the script and at the default, it threw the error. When in doubt, remove the try statement.

The other thing I would look at is putting the timeout statement inside a System Events tell block. The guide suggests that the timeout only applies to commands sent to application objects (I will add that I’m a little murky on ‘objects’, application or otherwise). The provided example has the timeout statement inside a tell application block.

Anyways… just some random thoughts.

Another option is to wrap the commands within an ignoring application responses statement.

The following example shows how to use an ignoring statement so that a script needn’t wait while System Events (or any other application) is performing a potentially lengthy task.

tell application "System Events"
	ignoring application responses
		delete  earliestFolder
		-- other statements that ignore application responses
	end ignoring
end tell

This method removes the need to use “try” or “with timeout” commands. It will just continue on with the rest of the commands, while System Events continues on deleting the files in the background…whether or not there were any errors.

My apologies for the delay in responding, I thought the system would email me about replies.

wch1zpink, I actually want my AS to wait while the deletion happens, but thanks for the idea.

Mockman, thanks for your two ideas, I will try them (pun not intended) and report back. Also, do you have a link to “the guide” – sounds like it would be a good resource for me!

Many thanks to you both, I am really scratching my head with this one! :confused:

The System Event’s delete command never returns a result, which means that AppleScript knows that it shouldn’t expect any response from the application. Therefore, the with timeout block is absolutely useless here. Instead, you need to check for the existence of the folder after the delete command triggering:


set folderHFSpath to (choose folder) as text

tell application "System Events"
	repeat while exists folder folderHFSpath
		try
			delete folder folderHFSpath
		end try
		delay 0.5
	end repeat
end tell

If some application continuously writes something to a folder, that folder will be busy all the time and the repeat loop will never end. And this is logical. As I know, you can’t erase some folder or file while it is busy with other process.

The repeat loop (and deletion of the folder) will complete successfully the moment the folder becomes freed by any other processes.

The finder’s delete command does return a result:

delete
delete (verb) Move an item from its container to the trash (from Standard Suite)

FUNCTION SYNTAX
set theResult to delete reference

RESULT
reference to the item that was just deleted
 

Since the command actually moves files to the trash the result is the file’s path in the trash.

This is a useful correction, and I took it into account in my post #5. But again, if some application continues to write to a folder that is in the trash, the system will not allow you to permanently destroy it. Even manually.

Results of testing tonight: neither moving the try inside the with timeout block, nor putting the timeout into a tell System Events block, makes any difference – I still get the error after about 2 minutes. I’m going to have to bite the bullet and use do script in the Terminal. As opposed to do shell script, which I don’t want to use here, I can use the former.

Many thanks for the ideas, at least I had something to try – always better than just being stumped and having nothing! :slight_smile:

@SkiAddict1

Here is ex. To make a timeout with do shell script in 3 minutes.

set timeSec to 60 * 3 + 1 -- timeout 1s after do shell script sleep in 3min

with timeout of timeSec seconds
	set timeDelay to 60 * 3
	do shell script "sleep " & timeDelay
end timeout

FYI, here is the link to the ASLG:

https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/introduction/ASLR_intro.html

The ‘with timeout’ section is under Control Statements:

https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_control_statements.html#//apple_ref/doc/uid/TP40000983-CH6g-130992

FWIW, I only use the ‘timeout’ with Excel as it sometimes interprets commands so that they work on every possible cell. Excel seems to obey the timeout but as mentioned in other comments, that behaviour may not be universal.

As an aside, just how many files are you trying to delete? I just did a small test which involved deleting some 1500 files (about 1GB in total size) contained within several sub-directories. It took only 700 milliseconds and my mac is slow.

Thanks for this – damn shame they couldn’t see fit to post a PDF link too…I don’t trust links to Apple pages since they have a bad habit of disappearing all too quickly! This one seems to have stood the test of time, though.

The vast majority of stuff that I know about AS has been gleaned from googling over the years so this reference should be quite handy – thanks :slight_smile:

Well, it’s a copy of my entire home folder so you can imagine the sort of quantum I’m dealing with. I have two MacBook Pros (one quite old) and since Apple saw fit to not allow Time Machine to write to (new) external disks in anything other than APFS and my old machine is still running El Capitan, if I want a disaster recovery copy of my home folder it has to be made specially, which I do twice a week into rotating folders in a separate partition.

The deletion takes on the order of 3 or 4 minutes.

Thanks again for your help!

My pleasure.

They stopped updating the pdf prior to the most recent site update (based on revision history).

If you take a look at this thread below (for which I finally registered on this site to comment on), you will find a script that will ostensibly construct a pdf based on the developer site. You can probably also use wget to build a local copy of the site. Finally, I think that some library site hosts a copy (or used to) of the pdf, among other docs. It might be worth searching for.

https://macscripter.net/viewtopic.php?pid=202617#p202617

If you can’t get around the default timeout of two minutes, it might be worth looking into breaking the task up: e.g. break the list of folders into multiple groups of folders at the top level of the user Library (as lists) and have a repeat loop cycle through each group. Something like this maybe:

tell application "System Events"
	set topFolder to alias "MacDrive:Users:username:Library:"
	set fList to (get disk items of topFolder)
	
	-- break into 3 approximately equal folder groups
	set l to length of fList --> 86
	set aPack to l div 3 --> 28
	
	-- make 3 folder lists
	set aList to items 1 thru aPack of fList
	--> dsstore … developer
	set bList to items (1 + aPack) thru (2 * aPack) of fList
	--> dictionaries … mobile devices	
	set cList to items (2 * aPack + 1) thru -1 of fList
	--> passes … workflows
	length of aList
	
	-- cycle through each group of folders then each folder in the group and delete
	set listList to {aList, bList, cList}
	repeat with xList in listList
		repeat with xFolder in xList
			delete xFolder
		end repeat
	end repeat
end tell

Of course, you could just arbitrarily pick the cut-off points. Presumably, this might allow you to avoid hitting the 2-minute warning.

FWIW, the System Events delete command seems to have problems deleting file packages (e.g. omnioutliner3, voodoopad docs). Not sure if that would be an issue with the Library contents but such files could likely be handled separately.

[format]error "System Events got an error: Can’t get file package… number -1728 from file package….[/format]

I think I’ll stick with the URL you gave me earlier; PDFs exist, but they all seem to be old (one is so old that screengrabs show pre-OSX style windows!!!), wget (following the cookbook at https://www.hostinger.com/tutorials/wget-command-examples/) fails to get the entire set of files, and the script requires Safari which I don’t usually run, and which refused to load the URL that the script requires as a start point. Never mind, so long as Apple don’t disappear the guide I’ll be OK!

Yes, I’m going to have to do this anyway, since when the deletion fails (https://macscripter.net/viewtopic.php?id=49215) to delete some of the files in the Library it throws up its hands in horror, despite being inside a try block :rolleyes:

I don’t think I have any like this, but good to know.