Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Made by phoi.
- -- Heavily based on code from cfillion's "Select track fx by name".
- -- Assumes that both actions will belong to the same context as this script.
- --To use it with actions for a different context, load this script into that context first.
- --FUNCTIONS FOR DEBUG
- function msg(msg)
- reaper.ShowConsoleMsg(tostring(msg) .. "\n")
- end
- local _
- local script_path
- local script_section
- local script_id
- _, script_path, script_section, script_id, _, _, _ = reaper.get_action_context()
- local script_name = script_path:match("([^/\\_]+)%.lua$")
- function sanitizeString(str) -- clean strings to avoid characters that create or mess with filepaths
- local out = str:gsub("[*\\:<>?/|\"%c]+", '-')
- return out
- end
- function getCmdName(id)
- if id:sub(1,1) == "_" then -- if custom action or script
- name = sanitizeString( reaper.CF_GetCommandText( script_section, reaper.NamedCommandLookup(id) ) )
- else
- name = sanitizeString( reaper.CF_GetCommandText( script_section, id ) )
- end
- return name
- end
- function formatID(id)
- if id:sub(1,1) == "_" then -- if custom action or script
- id = "reaper.NamedCommandLookup('" .. id .. "')"
- end
- return id
- end
- function main()
- local ok, csv = reaper.GetUserInputs(script_name, 3,
- "ID of action when toggle is on:,ID of action when toggle is off:,Name of toggle action:,extrawidth=100",
- ",,")
- if not ok or csv:len() <= 1 then return end
- local cmd_on_orig, cmd_off_orig, tog_action_name = csv:match("^(.*),(.*),(.*)$")
- if not cmd_on_orig or not cmd_off_orig or not tog_action_name then return end
- local cmd_on, cmd_off = formatID(cmd_on_orig), formatID(cmd_off_orig)
- tog_action_name = "phoi_ToggleAction_" .. sanitizeString(tog_action_name:lower())
- local tog_output_fn = string.format('%s/Scripts/%s.lua', reaper.GetResourcePath(), tog_action_name)
- local cmd_on_name = getCmdName(cmd_on_orig)
- local cmd_off_name = getCmdName(cmd_off_orig)
- local act_output_name = "If " .. tog_action_name .. " is on: " .. cmd_on_name .. ". Else, ".. cmd_off_name
- local act_output_fn = string.format('%s/Scripts/%s.lua', reaper.GetResourcePath(), act_output_name)
- local base_name = script_path:match('([^/\\]+)$')
- local rel_path = script_path:sub(reaper.GetResourcePath():len() + 2)
- local codeHeader = string.format(
- [[-- This file was created by %s on %s
- -- Toggle name: %q
- -- Action when toggle is on: %q
- -- Action when toggle is ooff: %q
- ]], base_name, os.date('%c'), tog_action_name, cmd_on_name, cmd_off_name)
- -- add toggle script first to get ID
- local codeToggle = "\n" ..
- "local _" .. "\n" ..
- "local script_id" .. "\n" ..
- "local script_section" .. "\n" ..
- "_, _, script_section, script_id, _, _, _ = reaper.get_action_context()" .. "\n" ..
- "if reaper.GetToggleCommandStateEx(script_section, script_id) == 1 then" .. "\n" ..
- "reaper.SetToggleCommandState(script_section, script_id, 0)" .. "\n" ..
- "else" .. "\n" ..
- "reaper.SetToggleCommandState(script_section, script_id, 1)" .. "\n" ..
- "end" .. "\n" ..
- "reaper.RefreshToolbar2(script_section, script_id)" .. "\n"
- local tog_file = assert(io.open(tog_output_fn, 'w'))
- tog_file:write(codeHeader .. codeToggle)
- tog_file:close()
- tog_script_id = reaper.AddRemoveReaScript(true, script_section, tog_output_fn, true)
- if tog_script_id == 0 then
- reaper.ShowMessageBox(
- 'Failed to create or register the toggle action.', script_name, 0)
- return
- end
- tog_script_id = "reaper.NamedCommandLookup('_" .. reaper.ReverseNamedCommandLookup( tog_script_id ) .. "')"
- if script_section == 0 or script_section == 100 then -- if main context
- codeAction = string.format("\n" ..
- "if reaper.GetToggleCommandStateEx( 0, %s ) == 1 then" .. "\n" ..
- "reaper.Main_OnCommand(%s, 0)" .. "\n" ..
- "else" .. "\n" ..
- "reaper.Main_OnCommand(%s, 0)" .. "\n" ..
- "end", tog_script_id, cmd_on, cmd_off
- )
- elseif script_section == 32060 then -- if midi editor
- codeAction = string.format("\n" ..
- "if reaper.GetToggleCommandStateEx( 0, %s ) == 1 then" .. "\n" ..
- "reaper.MIDIEditor_LastFocused_OnCommand(%s, false)" .. "\n" ..
- "else" .. "\n" ..
- "reaper.MIDIEditor_LastFocused_OnCommand(%s, false)" .. "\n" ..
- "end", tog_script_id, cmd_on, cmd_off
- )
- else
- reaper.ShowMessageBox(
- 'Invalid script context. Load script under Main or Midi Editor contexts', script_name, 0)
- end
- local act_file = assert(io.open(act_output_fn, 'w'))
- act_file:write(codeHeader .. codeAction)
- act_file:close()
- act_script_id = reaper.AddRemoveReaScript(true, script_section, act_output_fn, true)
- if act_script_id == 0 then
- reaper.ShowMessageBox(
- 'Failed to create or register the action.', script_name, 0)
- return
- end
- reaper.ShowMessageBox(
- string.format('Created the action "%s".', act_output_name), script_name, 0)
- end
- main()
Add Comment
Please, Sign In to add comment