ReaScript

From CockosWiki

Revision as of 10:39, 26 October 2009 by MikeLacey (Talk | contribs)
Jump to: navigation, search

Contents

Introduction

ReaScript is a feature that allows you to run Perl or Python scripts within REAPER. From within the script, you can call any REAPER action, and also call back into most of the REAPER Extensions SDK functions. ReaScript can be used to create anything from advanced macros to full-featured REAPER Extensions.

Perl and Python are freely available and widely used scripting languages; details at the sites below.

* http://www.python.org/
* http://www.perl.org/

Requirements

To use ReaScript, you must have either Python or Perl installed on your computer. Both are freely available in several free distributions. OSX comes with both Python and Perl already installed, though older OSX installs may not have the most recent versions of Python or Perl.

Installing either Python or Perl is usually a simple, straightforward process. Although both are open-source projects, meaning the source code is available for anyone to build, most users will simply run a binary installer just like installing any other program (such as REAPER).

The company ActiveState make free versions of both Perl & Python available on their web site www.activestate.com and whilst they're not the only source of these two utilities they are very good.

Python: ReaScript requires Python 3.1. (2.5 and 2.6 may also work.)

Perl: ReaScript requires Perl 5.10, and a standard Perl module called FFI.

If the FFI module is not included with your version of Perl (it's part of the ActiveState distribution) then it's also here. Win32 http://www.reaper.fm/sdk/reascript/perl/FFI-W32.zip, OSX http://www.reaper.fm/sdk/reascript/perl/FFI-OSX.zip. FFI.pm and FFI.dll (or FFI.dylib on OSX) should both be placed in the Perl/lib directory of your Perl installation.

Running ReaScripts

To run a ReaScript, first you will need to either write a script, or copy a script from another user. Scripts can be placed anywhere on your disk, but it's convenient to keep them all in the REAPER/Scripts application data directory.

To write a new script, show the Actions list (bound to the ? key by default), and click ReaScript: Load. You will be prompted for the location to save your new script, the default location is recommended. Name your script something like test.py or test.pl, then click ReaScript: Edit. A text editor will open.

The simplest possible ReaScript in Python is:

RPR_APITest()

or in Perl:

RPR_APITest();

Please note - in Perl you do not have to specify use FFI; as REAPER will do that for you

Save the ReaScript, and then click Run. You should see a window that says "Test OK". Common reasons for the script failing are:

   * Python or Perl not installed
   * Wrong version of Python or Perl installed
   * Multiple versions of Python or Perl installed, and the system picks up the wrong version
   * Syntax error (typo, missing newline, incorrect indentation in Python) 

You can treat a ReaScript just like any custom action: bind it to a key shortcut, MIDI controller, or toolbar button. ReaScripts can also be run a la carte, via the actions "ReaScript: run..." and "ReaScript: run last script".

Calling REAPER Actions

You can call any REAPER action from ReaScript by using the API function

RPR_Main_OnCommand(actionnumber, 0)

Find the action number for a given action by opening the Actions list, and scrolling the window right to reveal an extra column. For example, from ReaScript you would call the REAPER action "Item: Split item under mouse cursor" like this, in Python:

RPR_Main_OnCommand(40746, 0)

This is the only API function you need to know if you're going to use ReaScript as an advanced macro language. Unless you're planning to memorise all of the Action Numbers you'll want to put a comment in your code explaining what the action is, or use a constant, so that you can read it later.

ReaScript API

ReaScripts also have access to most of the REAPER Extensions SDK functions. There is a list of all the API functions available to ReaScripts available whilst running, run the REAPER action "ReaScript: open ReaScript help (html)".

The help page will explain the basics of how to call API functions, which may require idiosyncratic syntax, because API functions can return information in the parameter list as well as the function return value. For convenience, there are also basic API functions for getting information from, and showing information to, the user.

NOTE: API function names are commonly referred to without specifying RPR_ -- but you have to specify RPR_ in your scripts

For more help on ReaScript, to report bugs, to request additional API functions, to share ReaScripts, or just to discuss the possibilities, please use the REAPER Developer Forum. http://forum.cockos.com/forumdisplay.php?f=35

General ReaScript API
Control Surface ReaScript API Calls

Language specific documentation and examples