Last active
May 23, 2021 05:11
-
-
Save Metaxal/e66dc7bdfadd3002252ddb5caecae124 to your computer and use it in GitHub Desktop.
A quickscript to create a new file in the same directory as the current file and opens it in DrRacket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket/base | |
;;; License: [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0) or | |
;;; [MIT license](http://opensource.org/licenses/MIT) at your option. | |
(require quickscript | |
racket/string | |
racket/file | |
racket/path | |
racket/gui/base | |
racket/class) | |
(script-help-string | |
"Create a new file in the same directory as the current file and opens it in DrRacket") | |
(define-script new-file-in-same-directory | |
#:label "New file in same directory" | |
#:menu-path ("&Utils") | |
(λ (selection #:frame fr #:file f) | |
(when f | |
(define pth (path-only f)) | |
(define name (get-text-from-user "New file in same directory" "Enter a file name" fr ".rkt")) | |
(when name | |
(let ([name (string-normalize-spaces name)]) | |
(define new-pth (build-path pth name)) | |
(cond | |
[(file-or-directory-type new-pth #f) | |
(message-box "File already exists" | |
(format "Error: file already exists: ~a" (path->string new-pth)))] | |
[else | |
(define header | |
(if (path-has-extension? new-pth #".rkt") | |
"#lang racket/base\n\n" ; TODO: look up the prefs to find the right lang line | |
"\n")) | |
(display-to-file header new-pth) | |
(send fr open-in-new-tab new-pth)])))) | |
#f)) | |
(module url2script-info racket/base | |
(provide filename url) | |
(define filename "new-file-in-same-directory.rkt") | |
(define url "https://gist.github.com/Metaxal/e66dc7bdfadd3002252ddb5caecae124")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment