According to the code you show me in PM,
the problem is that item position change,
so GetSelectedMediaItems doesnt work as expected.
Use this item editing template instead :
https://github.com/ReaTeam/ReaScript...%20editing.lua
By storing items in a table first, they can me move freely.