Opinionated guide to DrRacket scripts

DrRacket has many great features, but one of my favourites the script capability that allows you to quickly and easily extend DrRacket. The scripts are immediately accessible and - this is the best part - DrRacket is scripted in Racket!

Here is a quick example: Open terminal here:

#lang racket/base
(require racket/system
         racket/path
         quickscript)

(script-help-string "Open a terminal in the directory of the current file.")

(define-script open-terminal
  #:label "Open terminal here"
  #:menu-path ("&Utils")
  #:os-types (unix macosx windows)
  (λ (str #:file f)
    (unless f
      (set! f (current-directory)))
    (define dir (path->string (path-only f)))
    (case (system-type 'os)
      [(unix)
       (system (string-append "gnome-terminal"
                              " --working-directory=\"" dir "\""
                              " -t \"" dir "\""
                              "&"))]
      [(macosx)
       (system
        (string-append "osascript -e 'tell app \"Terminal\" to do script \"cd \\\"" dir "\\\"\"'" ))]
      [(windows)
       (shell-execute #f "cmd.exe" "" dir 'sw_shownormal)])
    #false))

This script opens a command line / terminal session in the folder of the file in the currently selected tab.

In windows the menu looks like this:

unnamed

Menus are ok, but I want a keybinding! keybindings are fast and I want to be fast!

Lets get started:

  1. click on 'quickscript' in the require and press the F1 key - DrRacket opens a local copy of the documentation to a search
    image

  2. click on 'Quickscript' for the full documentation

  3. I'll add the #:shortcut #:shortcut-prefix keywords so I can press [option]-[t] to open the command line:

(define-script open-terminal
  #:label "Open terminal here"
  #:menu-path ("&Utils")
  #:shortcut #\t
  #:shortcut-prefix (option)
  #:os-types (unix macosx windows)
  (λ (str #:file f)
  ...
  1. Save, then click Scripts>Compile scripts

Now I can click [option]-[t], and my command line opens!

The DrRacket plugin that makes this all possible is Quickscript, and it was created by @Laurent.O.
You don't need to install it as it is included when you download the Racket installer.

In addition to making your own scripts there is a nice set of scripts in the quickscripts-extra package, with more listed on the Racket wiki at https://github.com/racket/racket/wiki/Quickscript-Scripts-for-DrRacket.

I'd very much like to see what scripts you make.

Best regards

Stephen :beetle:

PS I didn't write Open terminal here - it was written by @Laurent.O but thanks also to @bdeket and @soapdog for working out the windows and linux specific calls. I'm sorry I can't remember who helped me with osascript - but thank you too.

2 Likes