Go Back   Cockos Incorporated Forums > REAPER Forums > ReaScript, JSFX, REAPER Plug-in Extensions, Developer Forum

Reply
 
Thread Tools Display Modes
Old 10-13-2020, 07:07 PM   #1
juan_r
Human being with feelings
 
juan_r's Avatar
 
Join Date: Oct 2019
Posts: 1,075
Default Rename and renumber selected tracks name by name (My first ReaScript)

This is my first script, and my first Lua program. I've had to look up lots of details about the language and the functions specific to Reaper.

I wrote it so that when I duplicate a track to record, the new track gets renamed by increasing its final number (if the original name ends with a number) or by appending the number 1 (if it doesn't). The script works for multiple tracks, too, and each name gets renumbered separately, so that {Gtr02, Vox005} become {Gtr03, Vox006}.

I use this script as the final step in a custom action to duplicate tracks to record - just the settings, not the content.

Custom: Duplicate and renumber tracks (settings only, no content)

Track: Duplicate tracks
SWS: Delete all items on selected track(s)
Script: Rename and renumber selected tracks name by name.lua

Code:
-- Rename and renumber selected tracks, name by name.lua
-- juan_r
-- v1.0, 2020.10.14

-- rename selected tracks by increasing their final number (if they have one)
-- or adding a number, starting from 1 (if they don't)

Action_Name = "Rename and renumber selected tracks, name by name"

-- divide Track_name into initial basename and final number,
-- noting how many digits are in number
function parse_name(trackname)
    local number = string.match (trackname, "(%d*)$")
    local numberdigits = #number
    local basename = string.sub(trackname, 1 , -numberdigits-1)
    return basename, tonumber(number), tonumber(numberdigits)
end


function Main()
  reaper.Undo_BeginBlock()
  reaper.ClearConsole()
  Num_of_tracks = reaper.CountSelectedTracks(0)
  
  -- maxnumber[basename] = maximum number found in track names after basename
  maxnumber = {}
  Tracks = {}
  
  -- collect names and possibly numbers

  for i = 0, Num_of_tracks - 1 do
    Tracks[i] = {}
    Tracks[i].mediatrack = reaper.GetSelectedTrack(0,i)
    _, Tracks[i].name = reaper.GetTrackName(Tracks[i].mediatrack, "")

    local basename, number, numberdigits = parse_name(Tracks[i].name)
    if numberdigits == 0 then -- no final number in trackname, we say it's 0 (1 digit)
      number = 0
      numberdigits = 1
    end
    Tracks[i].basename = basename
    Tracks[i].number = number
    Tracks[i].ndigits = numberdigits

    -- find out the max number associated to the given basename

    -- first occurrence of basename? Initialize maxnumber to wimpy maximum
    if maxnumber[basename] == nil then maxnumber[basename] = -1 end
    if number > maxnumber[basename] then maxnumber[basename] = number end
  end

  -- rename the tracks
  for i = 0, Num_of_tracks - 1 do
    new_number = maxnumber[Tracks[i].basename] + 1
    maxnumber[Tracks[i].basename] = new_number
    format = string.format("%%0%dd", Tracks[i].ndigits); -- e.g, "%03d" if it was 3 digits
    new_name = Tracks[i].basename .. string.format(format, new_number);
    
    reaper.GetSetMediaTrackInfo_String(Tracks[i].mediatrack, "P_NAME", new_name, true)
  end
  reaper.Undo_EndBlock(Action_Name, -1)
end

Main()
Advice on style/substance, comments etc. are welcome!
juan_r is offline   Reply With Quote
Old 10-14-2020, 08:39 AM   #2
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,621
Default

Nice. Saves some tedious renaming in these usecases
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is online now   Reply With Quote
Old 12-21-2021, 11:13 AM   #3
norbury brook
Human being with feelings
 
norbury brook's Avatar
 
Join Date: Mar 2007
Location: London UK
Posts: 3,378
Default

Just tried to make this but the last action isn't doing anything.

I copied the text into a text file then renamed it with the .lua extension and put it in the scripts folder.

I can see it in the actions menu but it doesn't do anything.

I presume I haven't made the .lua correctly.

Anyone?


M
__________________
https://www.marcuscliffe.com/
norbury brook is offline   Reply With Quote
Old 12-22-2021, 04:18 AM   #4
norbury brook
Human being with feelings
 
norbury brook's Avatar
 
Join Date: Mar 2007
Location: London UK
Posts: 3,378
Default

Edit: a bit of googling and I've worked out how to do it.

the more I get into this stuff the more amazing reaper is. I think most people wont get beyond the initial basics though and will miss all the amazing possibilities and customisation.



M
__________________
https://www.marcuscliffe.com/
norbury brook is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 04:32 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.