View Single Post
Old 04-19-2013, 01:03 PM   #96
spk77
Human being with feelings
 
Join Date: Aug 2012
Location: Finland
Posts: 2,668
Default

Edit.
added: remove trailing \r\n
fixed: newname ->newName (thanks, Argitoth)

Load/apply item names from file (Actually take names because item names don't exist in reaper).

Code:
# Load/apply item names from file

from reaper_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("Load/apply item names from file"):

    def msg(m):
        RPR_ShowConsoleMsg(m)

    actTakeIdL = []

    selItemCount = RPR_CountSelectedMediaItems(0)

    for i in range(selItemCount):
        selItemId = RPR_GetSelectedMediaItem(0, i)
        takeId = RPR_GetActiveTake(selItemId)
        actTakeIdL.append(takeId)

    a = RPR_GetUserFileNameForRead("Select item name list", "Load item names from file", ".txt")
    path = a[1]
    if a[0] == 1:
        try:
            f = open(path, "r")
            for i, newName in enumerate(f):
                newName = newName.rstrip("\r\n")  # remove trailing cr(carriage return) and n(new line)
                RPR_GetSetMediaItemTakeInfo_String(actTakeIdL[i], "P_NAME", str(newName), 1)
                if i + 1 == selItemCount:
                    break
            f.close()
        except IOError as e:
            msg(e)

Last edited by spk77; 06-02-2013 at 01:12 AM. Reason: added: remove trailing \r\n and fixed newname ->newName
spk77 is offline   Reply With Quote