Thursday, September 20, 2018

#1 2018-09-12 03:34:07 pm

peavine
Member
Registered: 2018-09-04
Posts: 8

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:

Applescript:


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:

Applescript:


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:

Applescript:


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.

Offline

 

#2 2018-09-12 05:18:32 pm

Marc Anthony
Member
From:: Dallas, TX
Registered: 2006-04-27
Posts: 819

Re: Preventing script error

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.

Applescript:

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.

Last edited by Marc Anthony (2018-09-15 09:33:15 am)

Offline

 

#3 2018-09-12 05:29:57 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5448

Re: Preventing script error

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/apples … _Libs.html

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

Applescript:

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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#4 2018-09-13 06:28:13 pm

peavine
Member
Registered: 2018-09-04
Posts: 8

Re: Preventing script error

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.

Offline

 

#5 2018-09-15 06:31:12 pm

technomorph
Member
Registered: 2017-12-14
Posts: 47

Re: Preventing script error

Shane Stanley wrote:


Applescript:

set theFilter to current application's NSPredicate's predicateWithFormat:"pathExtension IN[c] %@" argumentArray:{pictureExtensions}



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

Thanks

Offline

 

#6 2018-09-15 09:08:01 pm

alldritt
Site Admin
From:: Victoria, B.C. Canada
Registered: 2002-12-12
Posts: 20
Website

Re: Preventing script error

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


Mark Alldritt - Late Night Software Ltd.
support@latenightsw.com | mark@macscripter.net

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)