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 " & aFolder's POSIX path's quoted form & " -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
Best Answer
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.