Page 1 of 1

Having trouble scripting in scheme, ra:* functions not defined

Posted: 28 Apr 2023 19:12
by trumpetrespas
I've been trouble shooting this for a bit now, and I think it is time to just ask. Just for your information, I'm not an experienced scheme programmer, so I'm learning as I go.

Anyway, here is my current situation. I'm having trouble doing basic radium functions with a custom radium script.

Here is my config file:

Code: Select all

C CTRL_L SHIFT_L ALT_L FOCUS_SEQUENCER : ra.evalScheme "(duplicate-seqblock-and-block)"
B ALT_L FOCUS_EDITOR : ra.markRange
V ALT_L FOCUS_EDITOR : ra.pasteRange
0R2 CTRL_L SHIFT_L FOCUS_SEQUENCER : ra.evalScheme "(delete-all-pauses-in-seqtrack)"
S CTRL_L SHIFT_L ALT_L FOCUS_EDITOR FOCUS_MIXER FOCUS_SEQUENCER : ra.saveWithEmbeddedSamples
D SHIFT_L FOCUS_EDITOR : ra.generalTransposeRangeDown

*
ra.evalScheme('(load "~/exportMxl.scm")')
*

E CTRL_L SHIFT_L: ra.evalScheme "(exportMxl \"~/tmp.mxl\")"
Here is "~/exportMxl.scm"

Code: Select all

(define (exportMxl whereToExport)
  ( (define exportFile (ra:openFileForWriting whereToExport))
    (define whatToWrite "")
    (set! whatToWrite (string-append whatToWrite "this is a test"))
    (ra:writeToFile exportFile whatToWrite)
  )
)
Here is the output from the messaging window after I open radium:

Code: Select all

2023-04-28 11:46:50:
========== READER Warning: ~/exportMxl.scm: 8: "ra:openFileForWriting" has not been defined in function call (ra:openFileForWriting whereToExport)=========

2023-04-28 11:46:50:
========== READER Warning: ~/exportMxl.scm: 8: "ra:openFileForWriting" has not been defined when parsing atom=========

2023-04-28 11:46:50:
========== READER Warning: ~/exportMxl.scm: 8: "ra:writeToFile" has not been defined in function call (ra:writeToFile exportFile whatToWrite)=========

2023-04-28 11:46:50:
========== READER Warning: ~/exportMxl.scm: 8: "ra:writeToFile" has not been defined when parsing atom=========

2023-04-28 11:46:52:
========== READER Warning: ~/exportMxl.scm: 8: "ra:openFileForWriting" has not been defined in function call (ra:openFileForWriting whereToExport)=========

2023-04-28 11:46:52:
========== READER Warning: ~/exportMxl.scm: 8: "ra:openFileForWriting" has not been defined when parsing atom=========

2023-04-28 11:46:52:
========== READER Warning: ~/exportMxl.scm: 8: "ra:writeToFile" has not been defined in function call (ra:writeToFile exportFile whatToWrite)=========

2023-04-28 11:46:52:
========== READER Warning: ~/exportMxl.scm: 8: "ra:writeToFile" has not been defined when parsing atom=========

2023-04-28 11:46:55:
========== READER Warning: ~/exportMxl.scm: 8: "ra:openFileForWriting" has not been defined in function call (ra:openFileForWriting whereToExport)=========

2023-04-28 11:46:55:
========== READER Warning: ~/exportMxl.scm: 8: "ra:openFileForWriting" has not been defined when parsing atom=========

2023-04-28 11:46:55:
========== READER Warning: ~/exportMxl.scm: 8: "ra:writeToFile" has not been defined in function call (ra:writeToFile exportFile whatToWrite)=========

2023-04-28 11:46:55:
========== READER Warning: ~/exportMxl.scm: 8: "ra:writeToFile" has not been defined when parsing atom=========
And here is the out put from the messages window after I hit Ctrl-Shift-E

Code: Select all

2023-04-28 12:10:18:
error: unbound-variable unbound variable ra:openFileForWriting in ((define exportFile (ra:openFileForWriting whereToExport)) (define whatToWrite "") (set! whatToWrite (string-append whatToWrite "this is a test")) (ra:writeToFile exportFile whatToWrite))

error-code: ((define exportFile (ra:openFileForWriting whereToExport)) (define whatToWrite "") (set! whatToWrite (string-append whatToWrite "this is a test")) (ra:writeToFile exportFile whatToWrite))

error-file/line: /home/miles/exportMxl.scm[683]

error-history: 

"((define exportFile (ra:openFileForWriting whereToExport)) (define whatToWrite "") (set! whatToWrite (string-append whatToWrite "this is a test")) (ra:writeToFile exportFile whatToWrite))"

1:    
"((define exportFile (ra:openFileForWriting whereToExport)) (define whatToWrite "") (set! whatToWrite (string-append whatToWrite "this is a test")) (ra:writeToFile exportFile whatToWrite))"
;/home/miles/exportMxl.scm[3] 
2:    
"(exportMxl "~/tmp.mxl")"

3:    
"(exportMxl "~/tmp.mxl")"

4:    
"(apply func args)"
;scheme/init.scm[322] 
6:    
"(FROM-C-catch-all-errors-and-display-backtrace-automatically eval-string "(exportMxl \\"~/tmp.mxl\\")")"


(whereToExport . "~/tmp.mxl") 
Also, in case it is of any use, I'm using Arch Linux.

Re: Having trouble scripting in scheme, ra:* functions not defined

Posted: 29 Apr 2023 09:16
by kjetil
Hi, when using scheme, function names are available in typical lisp format, so ra:openFileForWriting is available as ra:open-file-for-writing, and so forth. Hope this helps. :-)

Re: Having trouble scripting in scheme, ra:* functions not defined

Posted: 29 Apr 2023 16:41
by trumpetrespas
Thank you, that got me unstuck.

Also, there where some other bugs in the code that I had to fix, so I'll put the fixed version here:

Code: Select all

(define (export-mxl whereToExport)
    (define exportFilePath (ra:get-path whereToExport))
    (define exportFile (ra:open-file-for-writing exportFilePath))
    (define whatToWrite "testing + ")
    (set! whatToWrite (string-append whatToWrite "this is a test"))
    (ra:write-to-file exportFile whatToWrite)
    (ra:close-file exportFile))
Also, in the configuration file, the path's need to start from "/", not "~". (Just figured I should correct myself in case anybody else having trouble reads this.)

Thank you again for getting me un-stuck :).

Re: Having trouble scripting in scheme, ra:* functions not defined

Posted: 01 May 2023 09:29
by kjetil
Looks great. Just want to give a warning about the ra:open-file-for-writing function: It should be placed in a try/finally-block to prevent file handler never being closed in case code fails before ra:write-to-file has been called. E.g. like this:

Code: Select all

(define (write-file-block path block)
  (define exportFile (<ra> :open-file-for-writing path))
  (try-finally :try (lambda ()
                      (block exportFile))
               :finally (lambda ()
                          (<ra> :close-file exportFile))))


(define (export-mxl whereToExport)
  (write-file-block (ra:get-path whereToExport)
                    (lambda (exportFile)
                      (define whatToWrite "testing + ")
                      (set! whatToWrite (string-append whatToWrite "this is a test"))
                      (ra:write-to-file exportFile whatToWrite))))
I'll add a note about this to protos.conf.