Old 08-20-2018, 12:50 PM   #1
vanhaze
Human being with feelings
 
vanhaze's Avatar
 
Join Date: Jul 2012
Location: Netherlands
Posts: 4,561
Default REQ: Select only last Item of all selected Items.

Dear Reaper Scripters !

Is this maybe possible to script:

Situation:
- Having some Items selected. (can be on same track, can be on different tracks).

When executing the Script, only the last Item (so the Item on the far right of all the selected Items) will be selected.

Many thanks in advance !
Warm Regards.
vanhaze is online now   Reply With Quote
Old 08-20-2018, 01:09 PM   #2
spk77
Human being with feelings
 
Join Date: Aug 2012
Location: Finland
Posts: 2,462
Default

Try this:
Code:
-- Select last item of selected items by spk77 (20 August 2018)
-- https://forum.cockos.com/showthread.php?t=210311

function main()
  local item_count = reaper.CountSelectedMediaItems(0)
  if item_count == 0 then
    return
  end
  local max_item_end = -1
  local last_item
  for i = 1, item_count do
    local item = reaper.GetSelectedMediaItem(0, i-1)
    if item ~= nil then
      local item_pos = reaper.GetMediaItemInfo_Value(item, "D_POSITION")
      local item_len = reaper.GetMediaItemInfo_Value(item, "D_LENGTH")
      local item_end = item_pos + item_len
      if item_end > max_item_end then
        max_item_end = item_end
        last_item = item
      end
    end
  end
  reaper.Main_OnCommand(40289, 0) -- Unselect items
  if last_item ~= nil then
    reaper.SetMediaItemSelected(last_item, true)
  end
  reaper.UpdateArrange()
  reaper.Undo_OnStateChangeEx("Select last of selected items)", -1, -1)
end

reaper.defer(main)
spk77 is offline   Reply With Quote
Old 08-20-2018, 01:29 PM   #3
vanhaze
Human being with feelings
 
vanhaze's Avatar
 
Join Date: Jul 2012
Location: Netherlands
Posts: 4,561
Default

My God, are you some kind of Reaper Scripting Robot ?!
Beautiful, just Beautiful, thanks so much !
vanhaze is online now   Reply With Quote
Old 08-20-2018, 03:36 PM   #4
Edgemeal
Human being with feelings
 
Edgemeal's Avatar
 
Join Date: Apr 2016
Location: USA
Posts: 673
Default

Quote:
Originally Posted by spk77 View Post
Try this:
Is there something wrong by doing it this way? Just trying to learn better.

Code:
count_sel_items = reaper.CountSelectedMediaItems(0)
if count_sel_items < 2 then return end
lastitem = reaper.GetSelectedMediaItem(0, count_sel_items-1) 
reaper.Main_OnCommand(40289, 0) -- Item: Unselect all items
reaper.SetMediaItemSelected(lastitem, true) -- Select last item
reaper.UpdateArrange()
THANKS!

Last edited by Edgemeal; 08-20-2018 at 04:05 PM.
Edgemeal is offline   Reply With Quote
Old 08-21-2018, 11:49 AM   #5
spk77
Human being with feelings
 
Join Date: Aug 2012
Location: Finland
Posts: 2,462
Default

Quote:
Originally Posted by vanhaze View Post
My God, are you some kind of Reaper Scripting Robot ?!
Beautiful, just Beautiful, thanks so much !
Glad it worked
Quote:
Originally Posted by Edgemeal View Post
Is there something wrong by doing it this way? Just trying to learn better.

Code:
count_sel_items = reaper.CountSelectedMediaItems(0)
if count_sel_items < 2 then return end
lastitem = reaper.GetSelectedMediaItem(0, count_sel_items-1) 
reaper.Main_OnCommand(40289, 0) -- Item: Unselect all items
reaper.SetMediaItemSelected(lastitem, true) -- Select last item
reaper.UpdateArrange()
THANKS!
That would work if selected items are on a single track, but count_sel_items-1 isn't necessarily the last item in timeline order:


In this case, count_sel_items-1 would be the first item in timeline. (reaper.GetSelectedMediaItem doesn't return items in chronological order)
spk77 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 12:30 AM.


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