Preventing script error

In a recent thread entitled “Merge Pictures into a PDF”, I posted an Applescript that I had a question about. I didn’t want to hijack that thread and instead started this new one.

The script contains the following:


set pictureExtensions to {"jpg", "png"}

set pictureFolder to (choose folder default location (path to home folder))

tell application "Finder" to set pictureFiles to (files of entire contents of pictureFolder whose name extension is in pictureExtensions) as alias list

This works well, unless I inadvertantly select a very large folder (such as my home folder) in which instance the script beachballs, reports an error, and ultimately requires a hard reset of the computer. To deal with this, I modified the above with the following:


tell application "Finder" to set pictureFiles to (files of entire contents of pictureFolder) as alias list

Then, later in the script, I processed the file list to include only those files with the specified extension. This improved matters but only to a certain extent.

My final solution–which is slow but works–is to include the following at the beginning of the script:


set pictureFolder to (choose folder default location (path to home folder))

tell application "Finder" to set folderCount to count (folders of entire contents of folder pictureFolder as alias list)

if folderCount > 20 then
display dialog "The selected folder contains " & folderCount & " folders, which is beyond the capacity of this script." buttons {"OK"} cancel button 1 default button 1 with title "Picture Merge" with icon stop
end if

Is there a better way to do this, or am I taking the wrong approach to get a list of the pictures files?

Thanks.

Hi. The issue relates to the entire contents command, which is notoriously slow and questionably reliable. If there’s even the possibility that there may be more than a couple hundred total files—including within nested folders—it should be avoided. One solution is to use a shell script.

set posixList to (do shell script "find " & ((choose folder)'s POSIX path)'s quoted form & " -type f | egrep '[.](jpg|rtf)$' ")'s paragraphs 

set aliasList to {}

repeat with this in posixList
	set aliasList's end to my deposixify(this)
end repeat

aliasList

on deposixify(this)
	POSIX file this as alias
end deposixify

–Edit: Changed the regex to only capture files ending with the extension, rather than simply containing it. Edit 2: Made regex pattern shorter/less redundant.

You’re running into a problem with the Finder – it’s simply too slow at this, and often ends up timing out. The best solution is to avoid using the Finder.

If you download my FileManagerLib here:

https://www.macosxautomation.com/applescript/apps/Script_Libs.html

And use a little AppleScriptObjC, this will be considerably faster:

use AppleScript version "2.5" -- 10.11 or later
use framework "Foundation"
use scripting additions
use script "FileManagerLib" version "2.2.1"

set pictureExtensions to {"jpg", "png"}
set pictureFolder to (choose folder default location (path to home folder))
set allPaths to objects of pictureFolder result type urls array with searching subfolders
set theFilter to current application's NSPredicate's predicateWithFormat:"pathExtension IN[c] %@" argumentArray:{pictureExtensions}
set wantedPaths to (allPaths's filteredArrayUsingPredicate:theFilter) as list

Thanks Marc Anthony and Shane for your responses and suggestions. I knew that the Finder was slow but didn’t realize it was that limited. I’ll use Marc Anthony’s suggestion for now and then spend some time learning scripting additions and AppleScriptObjC.

Shane can you explain the [c] part of your predicate filter.

Thanks

The [c] in the predicate expression indicates the comparison is case insensitive.