Sunday, September 15, 2019

#1 2019-08-24 03:11:22 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 516

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:

Applescript:


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.

Last edited by KniazidisR (2019-08-25 05:16:31 am)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#2 2019-08-24 04:06:21 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 516

Re: Random Cinema

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

Applescript:


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

Last edited by KniazidisR (2019-08-25 05:15:13 am)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#3 2019-08-24 11:00:19 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11594
Website

Re: Random Cinema

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

Applescript:

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

Or completely in AppleScriptObjC

Applescript:

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))

Last edited by StefanK (2019-08-24 11:08:00 pm)


regards

Stefan

Offline

 

#4 2019-08-25 04:37:33 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 516

Re: Random Cinema

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

Last edited by KniazidisR (2019-08-25 04:43:35 am)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)