AppleScript Dialog File Chooser with Image Thumbnails / Preview

I am writing an AppleScript for Adobe InDesign that searches for images, if I find 1 image then I will import it to the page, but if I find more than 1 image I want to be able to present the user with a complex dialog containing thumbnails or a preview of the images that were found so that they can choose the ones they want to import.

I found this awesome post AppleScript Dialogs with Image Thumbnails however I can’t get the solution to work and assume that because it’s about 5 years old it might be redundant and I should be using a different approach. I also revisited Xcode for the first time in a while today thinking I’d rebuild it there but the “AppleScript App” template has been removed and I am taking that as strong suggestion from Apple to not to go that way either.

Can anyone advise if it is possible to display a gallery of thumbnails for the purposes of making a selection using AppleScript?

@pcLoadLetter’s code runs just fine here on macOS 10.14.6 Mojave.

But – you have to run it on the main thread in the Script Editor: R

I’ve made minor adjustments to the code here:

AppleScript Code

-- Adding the images to the list
tell application "System Events"

   set image_option1 to POSIX path of disk item "~/test_directory/Image_Files_50/test_file_005.jpg"
   set image_option2 to POSIX path of disk item "~/test_directory/Image_Files_50/test_file_006.jpg"
   set image_option3 to POSIX path of disk item "~/test_directory/Image_Files_50/test_file_007.jpg"
   set image_option4 to POSIX path of disk item "~/test_directory/Image_Files_50/test_file_008.jpg"
   set image_list to {image_option1, image_option2, image_option3, image_option4}
   -- Getting the number of items in the list
   set image_count to count image_list
   -- Figuring out the height of the display window based on the number of images in the list
   if image_count is less than or equal to 4 then
      set image_rows to 1
      set image_rows to 2
   end if
   set screen_height to (400 * image_rows) as number
end tell


use AppleScript version "2.4"
use framework "Foundation"
use framework "AppKit"

property NSWindow : a reference to current application's NSWindow
property NSImage : a reference to current application's NSImage
property NSImageView : a reference to current application's NSImageView
property NSWindowStyleMaskTitled : a reference to 1
property NSWindowStyleMaskClosable : a reference to 2

set theWindow to createWindowWithRect(0, 0, 1000, screen_height)
my performSelectorOnMainThread:"showTheWindow:" withObject:theWindow waitUntilDone:true

-- Creating the window 
on createWindowWithRect(xMin, yMin, xLen, yLen)
   set windowSize to {{xMin, yMin}, {xLen, yLen}}
   set winStyle to NSWindowStyleMaskTitled + NSWindowStyleMaskClosable
   set aWindow to NSWindow's alloc()'s initWithContentRect:windowSize styleMask:winStyle backing:2 defer:yes
   return aWindow
end createWindowWithRect


--> add the heading "MULTIPLE IMAGE(S) FOUND. Which image would you like to use?" to the UI


-- Adding the images to the UI Window
set x1 to 200
repeat with image_option in image_list
   tell application "Finder" to set image_name to name of (POSIX file (image_option as string) as alias) -->getting the image name for the label
   log image_name
   set theImage to (NSImage's alloc()'s initWithContentsOfFile:image_option)
   set theImageView to (NSImageView's alloc()'s initWithFrame:{{25, 50}, {x1, 300}})
   (theWindow's contentView()'s addSubview:theImageView)
   (theImageView's setImage:theImage)
   --> add image name beneath the image as a label to the UI
   --> add buttons to the UI for the user to choose which image they want to use
   set x1 to (x1 + 450)
end repeat

-- Showing the window
on showTheWindow:theWindow
   (theWindow's makeKeyAndOrderFront:me)
end showTheWindow:


--> Pass the selected image back to AppleScript as a variable


Pretty spiffy…

I won’t be able to help improve upon this. I’m not familiar with that aspect of AppleScriptObjC and have other things to do right now, but it sure looks like your task is achievable.

You may also want to have a look at Shane Stanley’s Dialog Toolkit and Myriad Tables Libraries over on the Script Debugger Forum.

Thank you that helps. I wasn’t aware it was even possible to run AppleScript code in the main thread, so winning already.

I think this poses another question which I’ll hunt around for, but when I save out the run-only application of the script it’s asking me each time to access the folder the images are pointing to.