Batch open JPGs in Preview

I've had a look already on the forums for an answer to the problem but most of the time it is in the context of batch converting files (with disparate names). I only want to just open a range of JPEG files in Preview.

I capture images of particlar items in my workshop using "Workflow" for iOS. That has a script of its own that saves the files in the format 00000-0.JPG where the first five zeros are the article number and the final zero is the sequential order of that particalular item. e.g 78934-4.JPG is the 4th photo of item "78934". The number of images for given item can vary a lot.

These all wind up in a folder (on my Mac) with several thousands other images, all with the same naming convention. I would like to speed up the process of finding and opening these (akin to being in Finder, selecting a range of images by hand and double-clicking the range and having them open in one "instance" or window of Preview - to minimise having to drag windows about) with AppleScript.

Here is what I've written so far...

tell application "Finder" to set myFiles to folder "Macintosh SSD:Users:me:ownCloud:Bilder_neu:Einzelbilder"

set file_extention to ".JPG"

set articleNumber to display dialog "Please enter article number" default answer "" with icon note buttons {"Cancel", "Continue"} default button "Continue"

I am not sure how to manage the "for loop" to find and then open the images.

Thanks for any help given


The following example AppleScript code assumes that the macOS default of images opening in Preview is still in play.

set theImages to quoted form of POSIX path of ¬
    ((path to home folder as string) & "ownCloud:Bilder_neu:Einzelbilder")

set articleNumber to text returned of ¬
    (display dialog "Please enter article number:" default answer ¬
        "" with icon note buttons {"Cancel", "Continue"} default button "Continue")

do shell script "find " & theImages & " -type f -iname '" & articleNumber & "-*.JPG' -exec open {} +"

If Preview is not the default handler for image files, then change the do shell script command to:

do shell script "open -a Preview $(find " & theImages & " -type f -iname '" & articleNumber & "-*.JPG')"

Note that "pure AppleScript" is not required!


Note: The example AppleScript code is just that and does not employ any error handling and is meant only to show one of many ways accomplish a task. The onus is always upon the User to add/use appropriate error handling as needed/wanted.