Random Cinema

Suppose you have a large collection of your favorite films in your Downloads and Movies folders, and you want to watch random ones. The following script does just that, and does that quickly:


use AppleScript version "2.5"
use framework "Foundation"
use script "FileManagerLib" version "2.2.1" --<https://www.macosxautomation.com/applescript/apps/FileManagerLib_stuff.zip>
use scripting additions

set downloadsFolder to path to downloads folder
set moviesFolder to path to movies folder

set downloadsContents to objects of downloadsFolder ¬
	searching subfolders true ¬
	include invisible items false ¬
	include folders true ¬
	include files true ¬
	result type urls array

set moviesContents to objects of moviesFolder ¬
	searching subfolders true ¬
	include invisible items false ¬
	include folders true ¬
	include files true ¬
	result type urls array

set thePred to current application's NSPredicate's predicateWithFormat:"pathExtension IN[c] %@" argumentArray:{{"mkv", "avi", "mp4", "mov"}}

set downloadsContents to (downloadsContents's filteredArrayUsingPredicate:thePred) as list
set moviesContents to (moviesContents's filteredArrayUsingPredicate:thePred) as list
set moviesList to (downloadsContents & moviesContents) as list

set Random_File_Number to 0
repeat while Random_File_Number = 0
	set Random_File_Number to random number (count of moviesList)
end repeat

set Random_File to item Random_File_Number of moviesList
tell application "VLC"
	activate
	open Random_File
end tell

NOTE: You can 1) add other video formats besides my choice, and 2) you can also cancel the search in subfolders by setting the corresponding values to false.

And, here is the 2nd variant - without third-party library, as fast as the 1st variant:


use AppleScript version "2.5" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
property NSDirectoryEnumerationSkipsHiddenFiles : a reference to 4

set listOfExtensions to {"mkv", "avi", "mp4", "mov"}
set downloadsFolder to POSIX path of (path to downloads folder)
set moviesFolder to POSIX path of (path to movies folder)
set fileManager to current application's NSFileManager's defaultManager()

set downloadsFolder to current application's |NSURL|'s fileURLWithPath:downloadsFolder
set downloadsContents to fileManager's contentsOfDirectoryAtURL:downloadsFolder includingPropertiesForKeys:{} options:NSDirectoryEnumerationSkipsHiddenFiles |error|:(missing value)
set moviesFolder to current application's |NSURL|'s fileURLWithPath:moviesFolder
set moviesContents to fileManager's contentsOfDirectoryAtURL:moviesFolder includingPropertiesForKeys:{} options:NSDirectoryEnumerationSkipsHiddenFiles |error|:(missing value)

set thePred to current application's NSPredicate's predicateWithFormat_("pathExtension.lowercaseString IN %@", listOfExtensions)
set downloadsContents to (downloadsContents's filteredArrayUsingPredicate:thePred) as list
set moviesContents to (moviesContents's filteredArrayUsingPredicate:thePred) as list

set moviesList to (downloadsContents & moviesContents) as list

set Random_File_Number to 0
repeat while Random_File_Number = 0
	set Random_File_Number to random number (count of moviesList)
end repeat

set Random_File to item Random_File_Number of moviesList
tell application "VLC"
	activate
	open Random_File
end tell

There are also path to references to the folders Movies and Downloads

set downloadsFolder to POSIX path of (path to downloads folder)
set moviesFolder to POSIX path of (path to movies folder)

Or completely in AppleScriptObjC

property |⌘| : a reference to current application

set downloadsFolder to first item of (|⌘|'s NSFileManager's defaultManager's URLsForDirectory:(|⌘|'s NSDownloadsDirectory) inDomains:(|⌘|'s NSUserDomainMask))
set moviesFolder to first item of (|⌘|'s NSFileManager's defaultManager's URLsForDirectory:(|⌘|'s NSMoviesDirectory) inDomains:(|⌘|'s NSUserDomainMask))

Thanks, StefanK, for this. I updated both scripts.