An Anti-Distraction System For Authors

Like most authors, we have problems concentrating. In these days of the internet, of social networking, it is easy to get distracted. Facebook, Twitter and other social networks never sleep, they’re always there, always calling us; we decide to check our newsfeed (or whatever) and what seems to be a two-minute tasks leads us down the rabbit hole and ends up being an hour or more. The solution, it seems to me, is to block access to such sites during specific periods but blocking software is expensive.

So, being a computer freak and ex-IT Professional, I decided to turn some of my scripting skills to creating a system that would act as a barrier to such sites.

Please Do Not Disturb

Initially I envisaged this as a writer's tool to allow them to write with less distraction but it is easy enough to see how this could benefit almost anyone working from home, perhaps even used by parents wishing to ensure their children do homework during certain periods.

Introduction

Let’s start from basics.

The file we need to concern ourselves with is called hosts and, on a Windows system and assuming Windows is installed to your C: drive, you would find it in the C:\Windows\System32\drivers\etc\ directory.

Without going into too much technical detail, the names you type into browsers (facebook.com for example) are translated into what are called IP addresses. For example, Facebook.com has (at time of writing) an IP address of 157.240.240.35 but, more importantly (for my blocking system), your local computer (all local computers to my knowledge) will always understand that the IP address 127.0.0.1 points to itself. Windows will ask other devices and resources where various internet sites are but it will check the hosts file first and if that file directs it to look elsewhere, that is where it will look. In other words, if we can tell your computer that facebook.com is actually at 127.0.0.1, it will look at itself for Facebook and, of course, will be unable to find it because Facebook isn’t on your computer, it's on the internet. So, all a blocking system has to do is change the hosts file to block specific sites for the period we want it to and, "Hey Presto!", we have a scheduled website blocking system.

The normal hosts file, at least on a relatively new install of Windows, might look like this:


										
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#	127.0.0.1       localhost
#	::1             localhost
									

In order to block Facebook and Twitter you would need to edit the hosts file to include the following lines:

	127.0.0.1	www.facebook.com
	127.0.0.1	www.twitter.com
								
These two lines tell your system that both sites are on your computer (which they aren't) which means the sites are blocked. You could do this manually but it would be extremely tiresome to do it each and every time which is where my RSQ Anti-Distraction system comes in; it does these things automatically and to a specified schedule.

Pre-Requisites

The RSQ Anti-Distract system comprises three files. These are:
  • The script file designed to do the work (default name, antidistract.cmd)
  • The site blocking file (default name, block.txt)
  • A copy of your system hosts file (default name, hosts.aok)

  1. Locate your computer’s hosts file. Typically this is found here: C:\Windows\System32\drivers\etc\hosts You will need to alter its properties so that you (the user) have full control:

    Editing The 'hosts' file

    • Right-click the hosts file, select "Properties" and, in the "hosts Properties" dialogue, select the "Security" tab.
    • Click "Edit" then, in the "Permissions for hosts" dialog, "Add", type in your username and click "OK".
    • Make sure your username is selected, click "Full control", then "OK" twice.

    Precautionary Measures
    • Copy the hosts file in case something goes wrong.
    • Right-click hosts, hold, drag to your desktop and release selecting to create a shortcut. Right-click the new shortcut and, in the "Target" box, modify it to start "notepad " (trailing space intentional), then click "OK" (the icon will change on your desktop). Click in the shortcut's name to rename it to something suitable such as "Edit 'hosts'".
    • Finally, double-click your new shortcut and make sure that you can edit and save the changed file but make sure you can revert back.
  2. Create a new folder for your Anti-Distraction project
  3. Copy the hosts file to a new folder from where you will run your anti-distracter (I put mine in d:\Utilities\AntiDistracter).
  4. Rename the copied file to hosts.aok.
  5. Create a new text file called block.txt in the same folder and edit it to contain sites you want to block e.g.
    
    	www.facebook.com
    	www.twitter.com
    										
  6. Create a new text file in the same folder as your two new files and rename it to something meaningful with an extension of ".cmd" (I named mine, antidistract.cmd). You will need to ensure that extensions are not hidden as you want to actually change the file's extension from "txt" to "cmd" but don't worry, there will be a link to a zip file with sample files at the end of this article.
  7. Right-click the new file and select "Edit" then paste the following into it (not required if you've downloaded the files and extracted them to a folder):
    
    												
    @ECHO OFF
    REM
    REM Check for time parameters
    REM
    IF "%1"=="" GOTO noparam
    IF "%2"=="" GOTO noparam
    
    REM
    REM Set the variables 'time-start' & 'time_end' as per command line parameters
    REM Get current time and store it in the variable, 'time_now'
    REM
    SET time_start=%1:00:00
    SET time_end=%2:00:00
    SET "time_now=%time: =0%"
    
    REM
    REM Test to see if 'time_now' is less than 'time_start' (the first IF statement)
    REM or greater than 'time_end' (the second IF statement) and UNBLOCK if they are.
    REM
    IF "%time_now%" LSS "%time_start%" GOTO unblocksites
    IF "%time_now%" GEQ "%time_end%" GOTO unblocksites
    
    :blocksites
        REM
        REM Block Sites. This is the default action if neither IF statement above
        REM has been actioned. Once done, exit batch file. 
        REM
        FOR /f "tokens=*" %%a IN (block.txt) DO CALL :processline %%a
        GOTO :eof
    
    :unblocksites
        REM
        REM Unblock Sites. This occurs if either IF statement above has been actioned.
        REM Once done, exit batch file. 
        REM
        COPY hosts.aok %SystemRoot%\System32\drivers\etc\hosts
        GOTO :eof
    
    :processline
        REM
        REM Adds lines of blocked sites to hosts file
        REM
        ECHO 127.0.0.1	%* >> C:\Windows\System32\drivers\etc\hosts
        GOTO :eof
    
    :noparam
        CLS
        ECHO.
        ECHO.
        ECHO **************************************************************************************
        ECHO *
        ECHO * You Need To Supply Start And End Time Parameters in the form XX:XX (24 hour clock)
        ECHO * For Example (to block distracting sites from 10am to 6pm):
        ECHO *     antidistract 10:00 18:00
        ECHO *
        ECHO **************************************************************************************
        ECHO.
        ECHO.
        pause
        GOTO :eof											

  8. You should test the batch file to ensure it works. To do so, open a command prompt (preferably as administrator), move within it to the directory where your batch file is and run it. Assuming it is the same as mine (in d:\Utilities\AntiDistracter):
    
    	d:
    	cd \Utilities\AntiDistracter
    										
    Two command line parameters are required:
    • The start time (which ends up as the variable 'time_start').
    • The end time ('time_end').
    Both parameters are of the format "XX:XX" where "XX:XX" is a time e.g. "08:00" (24 hour clock). The batch file works by assembling the times into something it can work with and then testing them against the system time ('time_now') for example:
    
    	AntiDistract 08:00 18:00
    										
    The above line should block websites from 8am to 6pm. To check it has worked open the system hosts file (remember the shortcut you created above) and, if the current time is between those cited, you should see the following lines added:
    
    	127.0.0.1	www.facebook.com
    	127.0.0.1	www.twitter.com
    										
    Assuming the above has tested correctly, you could try browsing to the sites you have blocked and should find yourself unable to reach them. On a Windows 10 machine using the Edge browser you should get something like this:

    A Blocked Site

  9. Once you're sure the batch file works (if you've followed these instructions correctly, it should do) then you can move on to the final stage, scheduling.


  10. Scheduling

    The remaining task is to schedule the batch file in order that it will apply the changes on boot and between the times you have specified and, in Task Scheduler, Windows provides the means to do so.

    • Open Windows Task Scheduler (just type “Task Scheduler” in the search box on the Windows taskbar).
      The Windows 10 Task Scheduler

    • Under the "Action" menu, select "Create Task" and, on the "General" tab, give the task a name you're happy with (I named mine, "Anti-Distract").
    • On the "Actions" tab of the “Create Task” dialogue, select "New", then “Browse” to find your new batch file then click "OK". In the "Add arguments (optional)" box add the times you want to block sites between, for example, for 8am to 6pm:

      
      	08:00 18:00
      										
      In the “Start in (Optional)” box you MUST enter the folder in which your batch file exists (in my case that is D:\Utilities\AntiDistracter\); if you don’t enter this, everything will appear to be OK but it probably won’t work properly. Click "OK" when done.
    • On the "Triggers" tab click “New” each time to create the following three entries:

      • Begin the task”: "On a schedule", “Settings”: “Daily” and “Start”: just after your start time (for 8am, I used "08:01").
      • Begin the task”: "On a schedule", “Settings”: “Daily” and “Start”: just after your end time (for 6pm, I used "18:01").
      • "Begin the task": "At log on." Other settings are optional. I used myself as "Specific user" but if you are the sole user of the machine (or you are happy to include other users) you could make it "Any user".
      • Triggers

      NOTE: These three entries should cover all normal eventualities, when you log on or when you are using the system but bear in mind, I have made no provision for weekends; I may choose to update the batch file to cover that at some later date.

      Click "OK" to exit. You should end up with something like this:

      Task Scheduler Completion

    Conclusion

    At this point you should be ready to go, hopefully this will serve you as well as it has (so far) served me.

    • In its current form, the RSQ Anti-Distraction system only works on Windows but I would welcome someone contacting me with the aim of developing a parallel system for alternative operating systems such as Mac or Linux.
    • The relevant files for this project are available HERE.
    • Please feel free to contact me if you have issues, suggestions or are willing to cooperate with me to adapt or improve the utility. I can be contacted at james @ either jamescrocks.com or rocksquad.co.uk (or via either site's contact form).
Thanks for reading.

James C. Rocks, Author ("The Abyssal Void" Series)

Buy my first book, "Stars Hide Your Fires":
Amazon UK
or
Other Bookstores

Quotes

It can only be attributable to human error.

HAL (2001: A Space Odyssey, 1968)

Send A Message...