Old 08-20-2018, 12:50 PM   #1
vanhaze
Human being with feelings
 
vanhaze's Avatar
 
Join Date: Jul 2012
Location: Netherlands
Posts: 5,247
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.
__________________
Macbook Pro INTEL | Reaper, always latest version | OSX Ventura | Presonus Studio 24c
My Reaper Tips&Tricks YouTube Channel: https://www.youtube.com/user/vanhaze2000/playlists
vanhaze is offline   Reply With Quote
Old 08-20-2018, 01:09 PM   #2
spk77
Human being with feelings
 
Join Date: Aug 2012
Location: Finland
Posts: 2,668
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: 5,247
Default

My God, are you some kind of Reaper Scripting Robot ?!
Beautiful, just Beautiful, thanks so much !
__________________
Macbook Pro INTEL | Reaper, always latest version | OSX Ventura | Presonus Studio 24c
My Reaper Tips&Tricks YouTube Channel: https://www.youtube.com/user/vanhaze2000/playlists
vanhaze is offline   Reply With Quote
Old 08-20-2018, 03:36 PM   #4
Edgemeal
Human being with feelings
 
Edgemeal's Avatar
 
Join Date: Apr 2016
Location: ASU`ogacihC
Posts: 3,921
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,668
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
Old 05-11-2022, 09:32 AM   #6
MonkeyBars
Human being with feelings
 
MonkeyBars's Avatar
 
Join Date: Feb 2016
Location: Hollyweird
Posts: 2,637
Default

Hey I'm doing research for my script project and was wondering, what exactly is Undo_OnStateChangeEx() doing here? I can't find an explanation in any docs.
MonkeyBars is offline   Reply With Quote
Old 05-11-2022, 11:33 AM   #7
MonkeyBars
Human being with feelings
 
MonkeyBars's Avatar
 
Join Date: Feb 2016
Location: Hollyweird
Posts: 2,637
Default

Amalgama explained to me that this command creates an additional undo point within an existing undo block
MonkeyBars is offline   Reply With Quote
Old 07-15-2023, 05:12 PM   #8
Phfraiengngchkxzcs
Human being with feelings
 
Join Date: Jul 2019
Posts: 24
Default Get Latest/Newest

Quote:
Originally Posted by spk77 View Post
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)
This is great. I am just learning the most basic elements of scripting in reaper, but i was wondering if there is a way to tweak this to select the most recently added item. Ideally, MIDI item. My hope would be to have the MIDI editor always focus to the MIDI item that was most recently recorded.
Phfraiengngchkxzcs 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 05:11 PM.


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