poulhoi

phoi_Create custom variable action with toggle

Jun 8th, 2022 (edited)
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.94 KB | None | 0 0
  1. -- Made by phoi.
  2. -- Heavily based on code from cfillion's "Select track fx by name".
  3. -- Assumes that both actions will belong to the same context as this script.
  4.   --To use it with actions for a different context, load this script into that context first.
  5.  
  6. --FUNCTIONS FOR DEBUG
  7. function msg(msg)
  8.   reaper.ShowConsoleMsg(tostring(msg) .. "\n")
  9. end
  10.  
  11. local _
  12. local script_path
  13. local script_section
  14. local script_id
  15. _, script_path, script_section, script_id, _, _, _ = reaper.get_action_context()
  16.  
  17. local script_name = script_path:match("([^/\\_]+)%.lua$")
  18.  
  19. function sanitizeString(str) -- clean strings to avoid characters that create or mess with filepaths
  20.   local out = str:gsub("[*\\:<>?/|\"%c]+", '-')
  21.   return out
  22. end
  23.  
  24. function getCmdName(id)
  25.   if id:sub(1,1) == "_" then -- if custom action or script
  26.     name = sanitizeString( reaper.CF_GetCommandText( script_section, reaper.NamedCommandLookup(id) ) )
  27.   else
  28.     name = sanitizeString( reaper.CF_GetCommandText( script_section, id ) )
  29.   end
  30.   return name
  31. end
  32.  
  33. function formatID(id)
  34.     if id:sub(1,1) == "_" then -- if custom action or script
  35.         id = "reaper.NamedCommandLookup('" .. id .. "')"
  36.     end
  37.     return id
  38. end
  39.  
  40. function main()
  41.  
  42.   local ok, csv = reaper.GetUserInputs(script_name, 3,
  43.     "ID of action when toggle is on:,ID of action when toggle is off:,Name of toggle action:,extrawidth=100",
  44.     ",,")
  45.   if not ok or csv:len() <= 1 then return end
  46.  
  47.   local cmd_on_orig, cmd_off_orig, tog_action_name = csv:match("^(.*),(.*),(.*)$")
  48.   if not cmd_on_orig or not cmd_off_orig or not tog_action_name then return end
  49.  
  50.   local cmd_on, cmd_off = formatID(cmd_on_orig), formatID(cmd_off_orig)
  51.  
  52.   tog_action_name = "phoi_ToggleAction_" .. sanitizeString(tog_action_name:lower())
  53.   local tog_output_fn = string.format('%s/Scripts/%s.lua', reaper.GetResourcePath(), tog_action_name)
  54.  
  55.   local cmd_on_name = getCmdName(cmd_on_orig)
  56.   local cmd_off_name = getCmdName(cmd_off_orig)
  57.   local act_output_name = "If " .. tog_action_name .. " is on: " .. cmd_on_name .. ". Else, ".. cmd_off_name
  58.   local act_output_fn = string.format('%s/Scripts/%s.lua', reaper.GetResourcePath(), act_output_name)
  59.  
  60.   local base_name = script_path:match('([^/\\]+)$')
  61.   local rel_path = script_path:sub(reaper.GetResourcePath():len() + 2)
  62.  
  63.   local codeHeader = string.format(
  64.     [[-- This file was created by %s on %s
  65.  
  66.     -- Toggle name: %q
  67.     -- Action when toggle is on: %q
  68.     -- Action when toggle is ooff: %q
  69.     ]], base_name, os.date('%c'), tog_action_name, cmd_on_name, cmd_off_name)
  70.  
  71.   -- add toggle script first to get ID
  72.  
  73.   local codeToggle = "\n" ..
  74.     "local _" .. "\n" ..
  75.     "local script_id" .. "\n" ..
  76.     "local script_section" .. "\n" ..
  77.     "_, _, script_section, script_id, _, _, _ = reaper.get_action_context()" .. "\n" ..
  78.     "if reaper.GetToggleCommandStateEx(script_section, script_id) == 1 then" .. "\n" ..
  79.     "reaper.SetToggleCommandState(script_section, script_id, 0)" .. "\n" ..
  80.     "else" .. "\n" ..
  81.     "reaper.SetToggleCommandState(script_section, script_id, 1)" .. "\n" ..
  82.     "end" .. "\n" ..
  83.     "reaper.RefreshToolbar2(script_section, script_id)" .. "\n"
  84.  
  85.   local tog_file = assert(io.open(tog_output_fn, 'w'))
  86.   tog_file:write(codeHeader .. codeToggle)
  87.   tog_file:close()
  88.  
  89.   tog_script_id = reaper.AddRemoveReaScript(true, script_section, tog_output_fn, true)
  90.  
  91.   if tog_script_id == 0 then
  92.     reaper.ShowMessageBox(
  93.       'Failed to create or register the toggle action.', script_name, 0)
  94.     return
  95.   end
  96.  
  97.   tog_script_id = "reaper.NamedCommandLookup('_" .. reaper.ReverseNamedCommandLookup( tog_script_id ) .. "')"
  98.  
  99.   if script_section == 0 or script_section == 100 then -- if main context
  100.     codeAction = string.format("\n" ..
  101.       "if reaper.GetToggleCommandStateEx( 0, %s ) == 1 then" .. "\n" ..
  102.       "reaper.Main_OnCommand(%s, 0)" .. "\n" ..
  103.       "else" .. "\n" ..
  104.       "reaper.Main_OnCommand(%s, 0)"  .. "\n" ..
  105.       "end", tog_script_id, cmd_on, cmd_off
  106.     )
  107.   elseif script_section == 32060 then -- if midi editor
  108.     codeAction = string.format("\n" ..
  109.       "if reaper.GetToggleCommandStateEx( 0, %s ) == 1 then" .. "\n" ..
  110.       "reaper.MIDIEditor_LastFocused_OnCommand(%s, false)" .. "\n" ..
  111.       "else" .. "\n" ..
  112.       "reaper.MIDIEditor_LastFocused_OnCommand(%s, false)"  .. "\n" ..
  113.       "end", tog_script_id, cmd_on, cmd_off
  114.     )
  115.   else
  116.     reaper.ShowMessageBox(
  117.       'Invalid script context. Load script under Main or Midi Editor contexts', script_name, 0)
  118.   end
  119.  
  120.   local act_file = assert(io.open(act_output_fn, 'w'))
  121.   act_file:write(codeHeader .. codeAction)
  122.   act_file:close()
  123.  
  124.   act_script_id = reaper.AddRemoveReaScript(true, script_section, act_output_fn, true)
  125.  
  126.   if act_script_id == 0 then
  127.     reaper.ShowMessageBox(
  128.       'Failed to create or register the action.', script_name, 0)
  129.     return
  130.   end
  131.  
  132.   reaper.ShowMessageBox(
  133.     string.format('Created the action "%s".', act_output_name), script_name, 0)
  134. end
  135.  
  136. main()
Add Comment
Please, Sign In to add comment