Opening Random File With AppleScript

2 min read

Late last year I became interested in automation on my devices. Since computers ment to help us, why not take it to the next level and automate repetitive tasks?

Exercise Before a Test

If you were studying, you know that practice can help you to get a better score on.a test or exam. There are even specific applications which is dedicated to make it easier to rotate through your flash cards, which will in random order show you your cards. Later after you have taken a few sessions and, based on frequency you chose and your results, you will get a mixed quiz to improve knowledge gaps in weak areas.

All this is great, but unfortunately, I haven’t found an accessible flash card application which will work great with VoiceOver.

Solving by AppleScript

AppleScript is a scripting language exclusive on macOS, lets you do a range of operations. One of them is the ability to open a random file in a folder in Finder. You can drop all crucial material for your test in one folder and by script which I will share below, you will be able to practice in random order without need to install third-party applications. Moreover, you can be more specific and for example you can have many files in your folder, but maybe you only want to be tested on files whose name starts with the letter "T"? AppleScript, get you covered.

The Script

    tell application "Finder"
    get folder "Random Files" of folder "Documents" of home as alias
    set theFolder to result
    open (some item of theFolder whose name begins with "T")
    end tell

However, if you just want to open a random file without specify the first letter of a file. You just need to remove text that comes after "theFolder" on the fourth line from the script. As long as your needs is simple, and you want just to be able to open your files in random order, this short script will help you with that!

Below is few resources which will help you get started with AppleScript and to modify my script for your needs as well.