Home

How to Create a Bible/Scripture/Prayer 'Take a Break' Pop-Up on Mac OS X

Take A Break Scripture/Prayer Screenshot

For some time I've been looking at ways of introducing Bible passages, Scripture and a pause for reflection/prayer into my daily life, but haven't done much to implement it.

I like routine, but I'm also not always disciplined enough to follow through with something if it's not put right in front of me.

That means merely setting an alarm or countdown timer in order to remind me to read a passage from the Bible, or pray for something, isn't enough. If I'm in the middle of something, I'll be too lazy/distracted to go and find my Bible (even if it's sitting right next to me) or think of something to pray for.

I need something which provides the essential elements - a selection of verses from the Bible plus a topic to pray about - right in front of me.

This also serves as a reminder to take a break from the computer, even if it's just for half-a-minute.

I am sure there are plenty of applications out there which can do this sort of thing. "Take a break" alerts have been around for years, and getting software to send notifications is now very familiar. However, I'm also quite fussy, so I wanted to see if I could create something myself.

The advantages of a homegrown approach is that it can be completely tailored to my needs (within the confines of what I'm capable of creating with the tools available to me). It can be tweaked whenever required. It's also free and I pretty much know exactly what it's doing, and when.

The techniques described here could be used for any kind of regular reminders, and added to or modified as you require. Hopefully the process I describe below will make it easy for you to change it to your own requirements.

The Tools and Required Solution

As with most challenges, there are a myriad of ways to complete the task. It depends in part as to what tools are available and how you want the end product to operate.

As this article's title suggests, this is a solution for those using an Apple Mac computer running a reasonably up-to-date version of OS X. Sorry, Windows users, you'll need to look elsewhere.

I've dabbled with the built-in (and free) Automator and AppleScript editors, and these seemed a good place to start.

I wanted the final solution to operate irrespective of what other software was installed, or what I was doing at the time. It also needed to be a little bit bossy and interrupt me, rather than hiding in the background.

That meant I chose the Finder alert system as the best option. It's not fancy by any stretch of the imagination. You can't do much text formatting, or include images, but for simple text alerts (which this is) it seemed the best choice. As we know, the Finder is always running, too.

I didn't want some AppleScript (even a small one) or Automator task sitting running in the background all day, so I decided to write a script which would populate and show the alert once whenever called, and then use a cron job to schedule it to run automatically every half-an-hour.

I looked at using Automator but when I saw that the AppleScript was pretty small I felt that Automator wasn't required.

The Script Requirements

Thanks to Google, searching out the bits of AppleScript I needed to piece together a complete script, I came up with the following:

  1. Set up two plain text files - one to hold scriptures and one to hold prayer topics. Each passage or prayer is separated by a carriage return.
  2. Get the script to read each of the two files into two separate lists.
  3. Pick a random list entry from each list.
  4. Display the alert with 'break' message, and the chosen scripture and prayer.

Then set up the cron job to run this AppleScript every half hour.

The AppleScript

Here we go with the code. I have added comments in which hopefully make it pretty self-explanatory.

You can copy and paste this into a new AppleScript Editor document. Once you've done, save it somewhere you can easily access it again. I put it in the /Documents/Scripts folder and called it "popup scripture prayer script". It will be given a 'scpt' extension.

(* Popup Scripture Prayer Script
Version 1.0
Andy Merrett
2015
*)

-- Set the full path to the scripture text file in the scriptureFile variable
set scriptureFile to "/Users/andymerrett/Documents/scripture and prayer/scripture/scripture.txt"
-- Now open this file for reading
set fileHandle to open for access scriptureFile
-- All separate paragraphs will be read into a list called scriptureList
set scriptureList to paragraphs of (read fileHandle)
-- And the file is closed.
close access fileHandle

-- Now we do the same for the prayer file
-- Set the full path to the prayer text file in the prayerFile variable
set prayerFile to "/Users/andymerrett/Documents/scripture and prayer/prayer/prayer.txt"
-- Now open this file for reading
set fileHandle to open for access prayerFile
-- All separate paragraphs will be read into a list called prayerList
set prayerList to paragraphs of (read fileHandle)
-- And the file is closed.
close access fileHandle

-- Get the number of scriptures loaded and store in numScriptures.
set numScriptures to count of scriptureList
-- Get the number of prayers loaded and store in numPrayers.
set numPrayers to count of prayerList
-- Now pick a scripture by picking a random number from 1 to the number of scriptures
set random_scripture to (random number from 1 to numScriptures)
-- And pick a prayer by picking a random number from 1 to the number of prayers
set random_prayer to (random number from 1 to numPrayers)
-- And then put the text of that scripture into the replyScripture variable.
set replyScripture to item random_scripture of scriptureList
-- And put the text of that prayer into the replyPrayer variable.
set replyPrayer to item random_prayer of prayerList

-- And finally, raise the alert.
-- Get the Finder to be active so the alert doesn't hide in the background.
tell application "Finder" to activate
-- And display the alert in the Finder.
-- The & syntax joins bits of the message together. The 'return' code is a carriage return. The two
-- text variables are inserted so that the scripture and prayer are displayed in the appropriate
-- places.
tell application "Finder" to display alert "Time to take a break..." & return & return & "Your scripture is: " & replyScripture & return & return & "Pray for: " & replyPrayer

-- That's it!
-- Add or remove items as required to make it your own.

Main points to note are:

The Text Files

You will need to create the two text files using a plain text editor. I use TextEdit, being sure to set it to plain text by going to the Format menu and selecting "Make Plain Text". (I haven't tested it but Rich Text Files may not play well with the script)

As stated previously, these contain separate lines for each scripture or prayer you want to be randomly selected from.

The Cron Job

The bit which keeps the whole thing running on schedule is the cron job.

Although cron is deprecated on OS X now in favour of launchd, I haven't got my head around launchd yet. So while cron still works (which it should do as it's pretty core in the Linux world), I've opted for this.

You can find online tutorials on cron if you really want to delve down, but here are the steps I took (if you already have cron jobs you will likely want to add the task to that existing file):

  1. Find somewhere to put the text file which will contain your cron jobs (called a crontab). I chose the home folder of my user account, which you can get to simply by going to the Terminal application and typing cd ~. After this, check you're there by typing pwd and you should get something like /Users/andymerrett (with your username displayed after 'Users/')
  2. Create/edit a file called crontab.txt in this folder. I use the nano editor which should be in the system already. Type nano crontab.txt and hit Enter.
  3. You'll likely have a plain text file unless you've already created or are editing this job. On a line of its own enter this: 0,30 * * * * osascript ~/Documents/Scripts/popup\ scripture\ prayer\ script.scpt.

    This is based on the assumption that you saved your AppleScript file in the same place and with the same name as I gave it (see the code section above). If not, you'll need to replace it with the path to your script. The ~/ at the start of the file path is shorthand for your home directory.

    Note there is a tab after the asterisk '*' and before the 'osascript', not spaces. This is important.

    If you have spaces in your filename, or any other 'special' characters, you'll need to 'escape' them by using a backslash (\) character beforehand. Don't forget the scpt file extension.

    The numbers at the beginning denote when you want the script to run. Putting a 0,30 at the start tells cron you want the script to run both on the hour and on the half-hour, all day, every day. You could make it run only on the hour by replacing with a single zero (0). Make it run every 15 minutes with 0,15,30,45 (no spaces)

  4. Now exit out of the nano text editor by holding 'Ctrl' and pressing 'X', then answer 'Y' when it asks if you want to save it.
  5. On the command line type crontab crontab.txt. If there are any errors, it will tell you now.
  6. Once no errors, you're good to go.
  7. One point to note is that each time the script is run, an email is sent to your account. You may never see this but after a while it's going to clog up your system. To stop it doing this, you can add a line to the top of your crontab.txt file as follows: MAILTO="". This will suppress the mail.
  8. That's it. Now you just wait, and at your allotted times, a Finder alert is raised with your scripture and prayer.
  9. If you ever change the crontab.txt file with new settings, just type crontab crontab.txt again, check there are no errors, and you're good to go.

Conclusion

I hope you found this little tutorial useful. This isn't the most whizzy or foolproof solution, but it does seem to do the job I wanted it to. I hope it works for you, too.

If you have any questions or need help, you can contact me and I'll do my best to assist you.

Home