Our compensation comes in the form of satisfaction in helping you solve a problem.
Although I have a Google Drive account, I do not use the Google Drive application on my Mac. The following code was tested on macOS Ventura 13.1 and my Pictures folder. Presumably, your mounted Google Drive content on the Mac would be in the form of a local folder path that you can select and that is all this script needs to do its processing.
The following script prompts you for a folder location (your mounted Google drive folder) and then first gets all of the images into a list and then forms a subset list of those images based on the imgType list you may need to modify in the script.
Based on that subset list, it generates a random number that is used as an index into that list and isolates one file that it in turn displays as a selected icon in the original folder location.
Assumption: You are using the most current Google Drive application on your Mac and the Drive contents are present in a folder that you can select from this script.
- Launch Apple's Script Editor from Dock > Launchpad > Other.
- Copy/paste the following code into the Script Editor, click the hammer icon to compile it. The purple text will change color to indicate a good compile. You may want to alter the image extensions to your own preference in the ImgTypes property.
- Click File menu > Save…
- File Format: Text
- No options
- Save As: random_file.applescript
- Select Desktop for Save location
- Save
Code: (copy/paste the following into the open Script Editor
-- Select a folder of images and get a single random image that is displayed in a new Finder
-- Window as an icon selection.
use framework "Foundation"
use framework "GamePlayKit"
use scripting additions
property ca : current application
property imgTypes : ["png", "jp2", "cr3"]
set theDir to POSIX path of (choose folder) as text
set theDirURL to ca's NSURL's fileURLWithPath:theDir
set enumOptions to (ca's NSDirectoryEnumerationSkipsPackageDescendants as integer) + (ca's EnumerationSkipsHiddenFiles as integer)
-- a predicate that does case-insensitive [c] path extension comparisons
set image_pred to ca's NSPredicate's predicateWithFormat:"pathExtension IN[c] %@" argumentArray:imgTypes
set fm to ca's NSFileManager's defaultManager()
set ws to ca's NSWorkspace's sharedWorkspace()
set urlArray to ((fm's enumeratorAtURL:theDirURL includingPropertiesForKeys:{} options:enumOptions errorHandler:(missing value))'s allObjects())'s valueForKey:"absoluteURL"
set selected_images to urlArray's filteredArrayUsingPredicate:image_pred
set imgCnt to selected_images's |count|()
set randval to (ca's GKRandomDistribution's distributionWithLowestValue:1 highestValue:imgCnt)'s nextInt()
set randomImg to selected_images's objectAtIndex:(randval - 1)
-- open a Finder Window with the random image icon selected
ws's activateFileViewerSelectingURLs:[randomImg]
return