Acrobat Pro File Alias - selectively close documents

Hi

I have done a script to selectively close Acrobat Pro documents dependent on whether the “keyword” appears in the relevant document path. It works but occasionally and randomly causes Acrobat to quit unexpectedly with the error message

. This error seems to happen only when more than 1 document qualifies to be closed, and then not every time (ie sometimes the script successfully closes multiple documents).

Not sure why it throws this error. But the script is as follows:

set keyword to "searchstring"
tell application "System Events"
	if exists process "Acrobat" then
		try
			tell application "Adobe Acrobat Pro"
				set DocsToClose to every document
				repeat with n from 1 to number of items in DocsToClose
					set pathname to (file alias of item n of DocsToClose) as string
					set filename to name of item n of DocsToClose
					if pathname contains keyword then
						close (every document whose name is filename) saving yes
					end if
				end repeat
			end tell
		end try
	end if
end tell

I had a simpler version which looked just at the name of the document, ie did not check the file path, which avoided the need to include a repeat loop:

--Check Acrobat documents
tell application "System Events"
	if exists process "Acrobat" then
		tell application "Adobe Acrobat Pro"
			close (every document whose name contains keyword) saving yes
		end tell
	end if
end tell

My question is whether it is possible to have this shorter script adapted for the file alias?

I have tried unsuccessfully the two following scripts (they both throw errors):


tell application "System Events"
	if exists process "Acrobat" then
		tell application "Adobe Acrobat Pro"
			close (every document whose file alias contains keyword) saving yes
		end tell
	end if
end tell

tell application "System Events"
	if exists process "Acrobat" then
		tell application "Adobe Acrobat Pro"
			close (every document whose (file alias as text) contains keyword) saving yes
		end tell
	end if
end tell

Anyone have a suggestion as to how I do the syntax on file alias to avoid the need for a repeat loop?

Thanks

Model: MacBook Pro
Browser: Safari 535.7
Operating System: Mac OS X (10.6)

The error occurs when documents are closed in the repeat loop and therefore the references are gone.
Assuming the list contains 3 items. After deleting an item the last loop (item 3 of theList) will fail because there is no third item.

I guess you can’t avoid the repeat loop, because the whose filter doesn’t work with coerced objects

Thanks Stefan.

I guess the solution is to start the loop again (recounting the open documents) every time I get a match.

Cheers