select a random file from google drive

I’m an artist, and have a reference folder on google drive of 1000s of images, and would like a way to pick a file at random for a drawing exercise. I don’t even almost understand writing scripts, but would somebody be so kind as to make me a script to pick a random file from a folder on google drive? I would absolutely compensate for help

MacBook Pro 15″, macOS 13.1

Posted on Jan 12, 2023 1:30 PM

Reply
Question marked as Top-ranking reply

Posted on Jan 13, 2023 10:11 AM

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.


  1. Launch Apple's Script Editor from Dock > Launchpad > Other.
  2. 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.
  3. Click File menu > Save…
    1. File Format: Text
    2. No options
    3. Save As: random_file.applescript
    4. Select Desktop for Save location
    5. 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

28 replies
Question marked as Top-ranking reply

Jan 13, 2023 10:11 AM in response to CaltropCreative

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.


  1. Launch Apple's Script Editor from Dock > Launchpad > Other.
  2. 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.
  3. Click File menu > Save…
    1. File Format: Text
    2. No options
    3. Save As: random_file.applescript
    4. Select Desktop for Save location
    5. 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

Jan 16, 2023 6:40 AM in response to CaltropCreative

Although it would appear that the Shortcut solution appeals more to you than code, here is the entire AppleScript with fixes that I have tested here on Ventura 13.1. Change the ImageTypes property as needed for your images.


-- display a selected random image name in a designated folder

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 NSDirectoryEnumerationSkipsHiddenFiles as integer) + (ca's NSDirectoryEnumerationSkipsSubdirectoryDescendants as integer)

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 enumFiles to ((fm's enumeratorAtURL:theDirURL includingPropertiesForKeys:{} options:enumOptions errorHandler:(missing value))'s allObjects())'s valueForKey:"absoluteURL"
set selected_images to enumFiles'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 randImg to selected_images's objectAtIndex:(randval - 1)
ws's activateFileViewerSelectingURLs:[randImg]
enumFiles's mutableCopy()'s removeAllObjects()
selected_images's mutableCopy()'s removeAllObjects()
return


Jan 13, 2023 11:00 AM in response to VikingOSX

Generally I agree - and your code is probably faster, but in the case of 'some file'. In my testing it seems pretty quick, even with a 1000-file folder.


The Finder certainly chokes on getting a filtered list of files via AppleScript, or any time you even think about 'entire contents of', but it seems that 'some file' seems to shortcut that.

I think that's because the Finder only passes back 'contents of theFolder', which is pretty quick since there's no filtering, and AppleScript performs the random item extraction from the list of files.


The other difference is that your script actively filters image files, whereas mine is blind (it will select any random file, not specifically an image file, if there are non-image files in the directory). That part would certainly be much slower via AppleScript.

If the OP is disciplined about only having image files in the directory (or is OK to re-run the script should a non-image file be selected), then this short script may be sufficient.

Jan 13, 2023 12:58 PM in response to CaltropCreative

CaltropCreative wrote:
The folder is all image files, but many different file types. If that helps. Also, can someone please explain this to me like theyre explaining it to a child who has never used a computer before? How do i run this script? where do i run it? how do i implement it within google drive? none of this makes sense to me, im a visual artist haha


I use google drive only when I connect via my web browser.

I know that they have an App that has a the dive as a folder on the Desktop. Is this how you use Google Drive?

Jan 14, 2023 1:48 PM in response to CaltropCreative


CaltropCreative wrote:
How do I get a google drive folder on my Desktop? I access it via chrome


I've never used the Google Drive Desktop App (so can't say that this will help do what you want or work with the posted scripts), but....Click the gear icon when you access with Chrome:



Also, there's an article here:

https://www.lifewire.com/how-to-set-up-and-use-google-drive-on-mac-2260845



Jan 15, 2023 8:41 AM in response to CaltropCreative

That Script Error "Can't make ENumerationSkipsHiddenFiles into type Integer" is my mistake.


Once you have updated the AppleScript as below:


set enumOptions to (ca's NSDirectoryEnumerationSkipsPackageDescendants as integer) + (ca's NSDirectoryEnumerationSkipsHiddenFiles as integer)


Then the code will work correctly as I just tested it here — provided you have installed the current Google Drive application and have a mounted share to choose from the script.



This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

select a random file from google drive

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.