|
|
|
05-12-2013, 01:46 PM
|
#1
|
Human being with feelings
Join Date: Aug 2012
Location: Finland
Posts: 2,668
|
ReaScript: Edit/manipulate track names(replace strings, uppercase etc.)
Edit track names (leave tracks unselected to show instructions):
Code:
instructions ="""
String methods for track name manipulating (empty edit box = do nothing):
upper or up
Converts lowercase letters in string to uppercase
lower or lo
Converts all uppercase letters in string to lowercase
swapcase or sc
Inverts case for all letters in string
capitalize or cap
Capitalizes first letter of string
titlecase or tc
Returns "titlecased" version of string, that is, all words begin with
uppercase, and the rest are lowercase
strip or st
Removes all leading and trailing whitespace of string
delete or del
Delete track names
Replace (old)/Replace with (new) (empty edit boxes = do nothing):
Replaces all occurrences of "old" in string with "new":
"""
from sws_python import *
from contextlib import contextmanager
@contextmanager
def undoable(message):
RPR_Undo_BeginBlock2(0)
try:
yield
finally:
RPR_Undo_EndBlock2(0, message, -1)
with undoable("Edit track names"):
def msg(m):
RPR_ShowConsoleMsg(m)
def dialog():
names = "String method:,Replace (old string):,Replace with (new string):"
maxreturnlen = 200 # one more than what you expect to get back
defvalues = ",," # default values
nitems = len(defvalues.split(',')) # number of input boxes
Query = RPR_GetUserInputs("Edit track names",nitems,names,defvalues,maxreturnlen) # call dialog and get result
if Query[0] == 1: # user clicked OK
UserValues = Query[4].split(',')
method = str(UserValues[0])
replaceOld = str(UserValues[1])
replaceWith = str(UserValues[2])
return method, replaceOld, replaceWith
def applyMethod(currentName, method):
if method == "upper" or method == "up":
newName = currentName.upper()
return newName
elif method == "lower" or method == "lo":
newName = currentName.lower()
return newName
elif method == "swapcase" or method == "sc":
newName = currentName.swapcase()
return newName
elif method == "capitalize" or method == "cap":
newName = currentName.capitalize()
return newName
elif method == "titlecase"or method == "tc":
newName = currentName.title()
return newName
elif method == "strip" or method == "st":
newName = currentName.strip()
return newName
elif method == "delete" or method == "del":
newName = ""
return newName
elif method == "":
newName = currentName
return newName
else:
return -1
msg(instructions)
def mainF():
trackCount = RPR_CountSelectedTracks(0)
if trackCount < 1:
msg("")
msg(instructions)
msg("Select tracks first")
return
try:
method, replaceOld, replaceWith = dialog()
except:
TypeError
return
for i in range(trackCount):
trackId = RPR_GetSelectedTrack(0, i)
currentName = str(RPR_GetSetMediaTrackInfo_String(trackId, "P_NAME", "", 0)[3])
newName = applyMethod(currentName, method)
if newName == -1:
msg("")
msg(instructions)
break
if newName != currentName:
RPR_GetSetMediaTrackInfo_String(trackId, "P_NAME", newName, 1)
if str(replaceOld) != "" or str(replaceWith) != "":
newName = currentName.replace(replaceOld, replaceWith)
RPR_GetSetMediaTrackInfo_String(trackId, "P_NAME", newName, 1)
mainF()
Last edited by spk77; 05-12-2013 at 09:18 PM.
|
|
|
05-24-2013, 12:23 PM
|
#2
|
Human being with feelings
Join Date: Feb 2012
Posts: 12
|
Thank you
This script is exactly what i am looking for.
You are the best reaper scripts programmer to date.
Good job spk77
|
|
|
05-24-2013, 10:37 PM
|
#3
|
Human being with feelings
Join Date: Aug 2012
Location: Finland
Posts: 2,668
|
Quote:
Originally Posted by crocobit
This script is exactly what i am looking for.
You are the best reaper scripts programmer to date.
Good job spk77
|
Well, I'm still a beginner in Python, but thanks for the compliments
|
|
|
03-16-2017, 05:47 PM
|
#4
|
Human being with feelings
Join Date: Apr 2011
Posts: 3,451
|
Could this script be ported to Lua?
I tried to make a start:
Code:
instructions = [[
String methods for track name manipulating (empty edit box = do nothing):
upper or up
Converts lowercase letters in string to uppercase
lower or lo
Converts all uppercase letters in string to lowercase
swapcase or sc
Inverts case for all letters in string
capitalize or cap
Capitalizes first letter of string
titlecase or tc
Returns "titlecased" version of string, that is, all words begin with
uppercase, and the rest are lowercase
strip or st
Removes all leading and trailing whitespace of string
delete or del
Delete track names
Replace (old)/Replace with (new) (empty edit boxes = do nothing):
Replaces all occurrences of "old" in string with "new": ]]
function dialog()
names = "String method:,Replace (old string):,Replace with (new string):"
maxreturnlen = 200 -- one more than what you expect to get back
defvalues = ",," -- default values
nitems = len(defvalues.split(',')) -- number of input boxes
Query = reaper.GetUserInputs("Edit track names",nitems,names,defvalues,maxreturnlen) -- call dialog and get result
if Query[0] == 1 then -- user clicked OK
UserValues = Query[4].split(',')
method = str(UserValues[0])
replaceOld = str(UserValues[1])
replaceWith = str(UserValues[2])
end
return method, replaceOld, replaceWith
end
function applyMethod(currentName, method)
if method == "upper" or method == "up" then
newName = string.upper(currentName)
return newName
elseif method == "lower" or method == "lo" then
newName = string.lower(currentName)
return newName
elseif method == "swapcase" or method == "sc" then
newName = currentName.swapcase()
return newName
elseif method == "capitalize" or method == "cap" then
newName = (currentName:gsub("^%l", string.upper))
return newName
elseif method == "titlecase"or method == "tc" then
newName = string.gsub(" "..currentName, "%W%l", string.upper):sub(2)
return newName
elseif method == "strip" or method == "st" then
newName = currentName.strip()
return newName
elseif method == "delete" or method == "del" then
newName = ""
return newName
elseif method == "" then
newName = currentName
return newName
else
return -1
reaper.ShowConsoleMsg(instructions)
end
end
function mainF()
trackCount = reaper.CountSelectedTracks(0)
if trackCount < 1 then
msg("")
msg(instructions)
msg("Select tracks first")
return
try:
method, replaceOld, replaceWith = dialog()
except:
TypeError
return
for i in range(trackCount) do
trackId = reaper.GetSelectedTrack(0, i)
currentName = str(reaper.GetSetMediaTrackInfo_String(trackId, "P_NAME", "", 0))
newName = applyMethod(currentName, method)
if newName == -1 then
msg("")
msg(instructions)
break
if newName ~= currentName then
reaper.GetSetMediaTrackInfo_String(trackId, "P_NAME", newName, 1)
if str(replaceOld) ~= "" or str(replaceWith) ~= "" then
newName = currentName.replace(replaceOld, replaceWith)
reaper.GetSetMediaTrackInfo_String(trackId, "P_NAME", newName, 1)
end
mainF()
Could someone finish it? Thanks!
|
|
|
04-04-2017, 10:38 AM
|
#5
|
Human being with feelings
Join Date: Apr 2011
Posts: 3,451
|
If anyone is interested, here is my lua version of the script:
Code:
-- modified spk77 python script by amagalma
local instructions = [[
String methods for track name manipulating (empty edit box = do nothing):
---- "upper" or "up"
Converts lowercase letters in string to uppercase
---- "lower" or "low"
Converts all uppercase letters in string to lowercase
---- "swapcase" or "sc"
Inverts case for all letters in string
---- "capitalize" or "cap"
Capitalizes first letter of string
---- "titlecase" or "tc"
Capitalizes first letter of each word
---- "strip" or "st"
Removes all leading and trailing whitespace of string
----"delete" or "del"
Delete track names
----Replace (old) / Replace with (new):
Replaces all occurrences of "old" in string with "new": ]]
local function has_value(table, val)
for index, value in ipairs(table) do
if value == val then
return true
end
end
return false
end
local function swapcase(str)
local t={}
str:gsub(".",function(c) table.insert(t,c) end)
for i=1, #t do
if t[i] == t[i]:match("%l") then t[i] = t[i]:upper()
elseif t[i] == t[i]:match("%u") then t[i] = t[i]:lower()
end
end
return table.concat(t)
end
local function applyMethod(currentName, method)
if method == "upper" or method == "up" then
newName = string.upper(currentName)
return newName
elseif method == "lower" or method == "low" then
newName = string.lower(currentName)
return newName
elseif method == "swapcase" or method == "sc" then
newName = swapcase(currentName)
return newName
elseif method == "capitalize" or method == "cap" then
newName = (currentName:gsub("^%l", string.upper))
return newName
elseif method == "titlecase"or method == "tc" then
newName = string.gsub(" "..currentName, "%W%l", string.upper):sub(2)
return newName
elseif method == "strip" or method == "st" then
newName = currentName:match("^%s*(.-)%s*$")
return newName
elseif method == "delete" or method == "del" then
newName = ""
return newName
end
end
local function mainF()
local trackCount = reaper.CountSelectedTracks(0)
if trackCount < 1 then
reaper.MB("------- Please select at least one track! -------\n\n\n"..instructions, "Instructions", 0)
else
local names = "a: String method:,b: Replace (old string):,b: Replace with (new string):"
local Query, retvals = reaper.GetUserInputs("Edit track names",3,names,"")
if Query == true then
if retvals ~= ",," then -- user clicked OK & entered values
words = {}
for word in retvals:gmatch("[^,]+") do table.insert(words, word) end
method, replaceOld, replaceWith = words[1], words[2], words[3]
local methods = {"upper", "up", "lower", "low", "swapcase", "sc", "capitalize", "cap", "titlecase", "tc", "strip", "st", "delete", "del"}
if has_value(methods, method) == false then
method = ""
end
reaper.Undo_BeginBlock()
for i=0, trackCount-1 do
local trackId = reaper.GetSelectedTrack(0, i)
_, currentName = reaper.GetSetMediaTrackInfo_String(trackId, "P_NAME", "", 0)
newName = applyMethod(currentName, method)
if method == "" then
newName = currentName
replaceOld, replaceWith = words[1], words[2]
end
if newName ~= currentName then
reaper.GetSetMediaTrackInfo_String(trackId, "P_NAME", tostring(newName), 1)
_, currentName = reaper.GetSetMediaTrackInfo_String(trackId, "P_NAME", "", 0)
end
if replaceOld ~= nil then
if not replaceWith then replaceWith = "" end
newName = string.gsub(currentName, replaceOld, replaceWith)
reaper.GetSetMediaTrackInfo_String(trackId, "P_NAME", tostring(newName), 1)
end
end
reaper.Undo_EndBlock("Manipulate track names", -1)
else
reaper.MB(instructions, "Instructions", 0)
end
end
end
end
mainF()
That was the last python script I had.. Going to uninstall Python now
Last edited by amagalma; 04-05-2017 at 04:53 PM.
|
|
|
04-04-2017, 01:50 PM
|
#6
|
Human being with feelings
Join Date: Oct 2007
Location: home is where the heart is
Posts: 12,096
|
Nice, comes in handy here.
Thanks spk77 and amagalma.
Consider putting it in ReaPack. .
|
|
|
04-05-2017, 04:48 PM
|
#7
|
Human being with feelings
Join Date: Apr 2011
Posts: 3,451
|
I have updated the code a bit
|
|
|
04-05-2017, 05:03 PM
|
#8
|
Human being with feelings
Join Date: Nov 2015
Location: Germany
Posts: 1,015
|
Thank you and spk77!
|
|
|
04-10-2017, 09:22 AM
|
#9
|
Human being with feelings
Join Date: Jan 2013
Posts: 1,126
|
Finally a decent Renamer. Thanks a lot to Both SPK77 and Amagalma for this.
|
|
|
Thread Tools |
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -7. The time now is 04:25 AM.
|