Search code examples
applescriptfinder

Tell QuickTime to open a random video file in a folder with AppleScript


I would like to play a random video file using QuickTime. My default application for opening video files is VLC, so I specifically want to tell QuickTime Player to open the file, rather than telling Finder to open the file with the default application.

I ran into issues when using 'some file' to select a random file as part of the "tell application "QuickTime Player"" section (I assume it only works with the application "Finder"), so I ended up trying a workaround that involves duplicating a random file to a specific location, then opening that specific location.

My current solution:

        tell application "Finder"
            set folderPath to "Macintosh HD:Users:username:Desktop:No Backup:Music:TimeOut:"
            delete (every item of folder folderPath whose name is "video.mp4")
            set sourceFile to some file of folder folderPath
            set duplicateFile to duplicate file sourceFile of folderPath
            set the name of duplicateFile to "video.mp4"
        end tell
        tell application "QuickTime Player"
            set theMovie to open "Macintosh HD:Users:username:Desktop:No Backup:Music:TimeOut:video.mp4"
            tell theMovie
                set the looping of theMovie to true
                set the present of theMovie to true
                play
            end tell
        end tell

This leads to the following error, highlighting the duplicate file sourceFile of folderPath bit:

Can’t make «class docf» "One of the random videos.mp4" of «class cfol» "TimeOut" of «class cfol» "Music" of «class cfol» "No Backup" of «class cfol» "Desktop" of «class cfol» "username" of «class cfol» "Users" of «class sdsk» of application "Finder" into type integer.

The QuickTime Player part of the script seems to be working correctly; I'm trying to work out the first half of the script.

A bit of help would be very much appreciated!


Solution

  • The following example AppleScript code works for me when the value of the folderPath variable is a proper HFS path:

    set folderPath to ¬
        "Macintosh HD:Users:username:Desktop:No Backup:Music:TimeOut:"
    
    tell application "Finder" to ¬
        set the fileToPlayInQuickTime to ¬
            some file of container folderPath as alias
    
    tell application "QuickTime Player"
        set theMovie to open fileToPlayInQuickTime
        tell theMovie
            set the looping of theMovie to true
            set the present of theMovie to true
            play
        end tell
    end tell
    

    If you want to target a specific type of file, assuming there are various types in the target folder, then use the following example AppleScript code while setting the value of the name extension property to one that is compatible with the target application:

    tell application "Finder" to ¬
        set the fileToPlayInQuickTime to ¬
            (some file of container folderPath ¬
                whose name extension is "mp4") as alias