Opening Randomized Images From Multiple Folders With Preview Application Basically what I am trying to achieve is the following:


Create an alias list of any amount of folders with the Finder's selection.
Get all images from those folders with a shell script.
Randomize those images.
Open the randomized images in the Preview application.


The script I created is partially working with two selected folders that each contain three images (6 total). But it is only opening 3 images with the preview app instead of all 6.

I am also trying it out with more than 100 images in two separate folders and it gives me an error: 

"Can’t make file \":43\" into type alias." number -1700 from file ":43" to alias


Also, the images are not being randomized every time I execute the script.

Please note that I prefer using the shell script to fetch the images because it is much faster than using the Finder. If it is possible to also use a shell script to randomize the images, I will like to know how this can be achieved in the script I created.

Any help is appreciated. Thanks!

tell application "Finder"
    if selection is {} then
    else
        set theFolders to selection as alias list

        set theImages to {}
        repeat with aFolder in theFolders
            set getImages to "find " &amp; aFolder's POSIX path's quoted form &amp; " -iname '*.jpg'"
            set end of theImages to paragraphs of (do shell script getImages)
        end repeat

        -- Randomize selected images

        set randomImages to {}
        repeat with thisImage in some item in theImages
            set end of randomImages to thisImage
        end repeat

        -- Get aliases

        set filePaths to {}
        repeat with thisFile in randomImages
            set end of filePaths to (thisFile as POSIX file as alias)
        end repeat

        -- Open random images with Preview

        open filePaths using application file id "com.apple.Preview"

    end if
end tell
Fixing and rewriting the code, particularly in the repeat with aFolder in theFolders loop, and using a handler from the accepted answer by Lri in Is there a simple way to shuffle a List in AppleScript? to randomize the list over your method, this now works.

I tested it on several different folders individually as well as multiple folders together, with as many as 500 JPG files in a test run.

tell application "Finder"
    if not selection is {} then
        set theFolders to selection as alias list
    else
        return
    end if
end tell

set theImages to {}
repeat with aFolder in theFolders
    set getImages to "find " &amp; aFolder's POSIX path's quoted form &amp; " -iname '*.jpg'"
    set tempList to paragraphs of (do shell script getImages)
    repeat with aItem in tempList
        copy aItem to end of theImages
    end repeat
end repeat

set randomImages to shuffle(theImages)

set filePaths to {}
repeat with thisFile in randomImages
    copy (thisFile as POSIX file as alias) to the end of filePaths
end repeat

tell application "Finder" to ¬
    open filePaths using application file id "com.apple.Preview"


### Handlers ###

on shuffle(input)
    script s
        property L : input
    end script
    set i to count of L of s
    repeat while i ≥ 2
        set j to random number from 1 to i
        set {item i of L of s, item j of L of s} to {item j of L of s, item i of L of s}
        set i to i - 1
    end repeat
    L of s
end shuffle