Skip to content

Instantly share code, notes, and snippets.

@X-Raym

X-Raym/Parent script.lua

Last active Mar 11, 2021
Embed
What would you like to do?
Custom Preset Files for ReaScripts

ReaScript Preset File

Documentation

Concept

Preset script files allow you to modify main variables and functions used by a script without modifying its source, so that your modifications will be preserved even if the parent script is updated by its developer. It only works with scripts written with this concept in mind.

For User

  1. Download the Preset script.lua next to script you want to create a preset for (the parent script) and name it as you want. For that, press and RAW button and do CTRL+S for saving the file on your computer.
  2. Copy paste parent script file name to the User Config Area 1/2 section of the preset script.
  3. Copy paste parent script User Config Area variables to the User Config Area 2/2 section of the preset script.
  4. Alter the User Config Area 2/2 variables as you want.
  5. Import your preset in REAPER via Actions window New Action -> Load ReaScript.

For Devs

  1. Wrap main code in a Init() as in Preset script.lua.
  2. Add boolean to prevent auto load the function if run from preset file.
  3. Have a user config area at top of the script.
  4. Prevent saving last input if necessary (for script with GUI) so that this feature stay for main script (not the preset, which usually will not have any popup).
--[[
* ReaScript Name: Original script
* About: For devs
* Author: X-Raym
* Author URI: https://www.extremraym.com
* Repository: X-Raym/REAPER-ReaScripts
* Licence: GPL v3
* REAPER: 5.0
* Version: 1.0
--]]
-- USER CONFIG AREA ------------------------------------------------------
-- Typical global variables names. This will be out global variables which could be altered in the preset file.
popup = false
console = false
-------------------------------------------------- END OF USER CONFIG AREA
function Init() -- The Init function of the script.
end
if not preset_file_init then -- If the file is run directly, it will execute Init(), else it will wait for Init() to be called explicitely from the preset scripts (usually after having modified some global variable states).
Init()
end
--[[
* ReaScript Name: Name of the Original Script - Preset 1 (or any any you want)
* About: Edit the User Config Areas to make it work.
* Author: X-Raym
* Author URI: https://www.extremraym.com
* Licence: GPL v3
* REAPER: 5.0
* Version: 1.0
--]]
-- USER CONFIG AREA 1/2 ------------------------------------------------------
-- Dependency Name
local script = "X-Raym_Set selected items position and order.lua" -- 1. The target script path relative to this file. If no folder, then it means preset file is right to the target script.
-------------------------------------------------- END OF USER CONFIG AREA 1/2
-- PARENT SCRIPT CALL --------------------------------------------------------
-- Get Script Path
local script_folder = debug.getinfo(1).source:match("@?(.*[\\|/])")
local script_path = script_folder .. script -- This can be erased if you prefer enter absolute path value above.
-- Prevent Init() Execution
preset_file_init = true
-- Run the Script
if reaper.file_exists( script_path ) then
dofile( script_path )
else
reaper.MB("Missing parent script.\n" .. script_path, "Error", 0)
return
end
---------------------------------------------------- END OF PARENT SCRIPT CALL
-- USER CONFIG AREA 2/2 ------------------------------------------------------
-- 2. Put your variables there, so that it overrides the default ones.
-- You can usually copy the User Config Area variable of the target script. Examples below.
-- Typical global variables names
popup = false
console = false
-------------------------------------------------- END OF USER CONFIG AREA 2/2
-- RUN -------------------------------------------------------------------
Init() -- run the init function of the target script.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment