Go Back   Cockos Incorporated Forums > REAPER Forums > ReaScript, JSFX, REAPER Plug-in Extensions, Developer Forum

Reply
 
Thread Tools Display Modes
Old 02-19-2019, 03:19 PM   #1
andyp24
Human being with feelings
 
andyp24's Avatar
 
Join Date: Mar 2016
Posts: 1,242
Default Is it possible to change what a script does depending on what was done previously?

Hi

I'm developing a script which is designed to "correct" some of the (in some circumstances) unhelpful results of inserting/pasting items with Ripple on.

So, essentially, what I've done is create a "clean up the mess" script to run after pasting or inserting.

However, if I accidentally run it twice, it creates a different kind of mess!

So, I wonder, is it possible to add some code into the script which checks what the PREVIOUS action was before it was called?

Ideally, I'd be able to know whether the previous thing that happened in the project was pasting something or inserting from Media Explorer. But if that's not possible, even being able to know whether this script was also the previous thing to be done would help.

Any ideas?

Thanks
andyp24 is offline   Reply With Quote
Old 02-19-2019, 04:54 PM   #2
Edgemeal
Human being with feelings
 
Edgemeal's Avatar
 
Join Date: Apr 2016
Location: ASU`ogacihC
Posts: 3,921
Default

Hmmm, 'reaper.Undo_CanUndo2(0)' returns string of last action. Maybe you could use that, not sure how useful that is, seems to change as soon as I do almost anything else. Test...

Code:
local script_name = 'My awesome script!'
local last_undo = reaper.Undo_CanUndo2(0) -- returns string of last action,if able,NULL if not

function myfunc() 
  reaper.Undo_BeginBlock() -- begin undo 
  
   -- >>> Call/run your scripts/code here, example... >>>
  reaper.Main_OnCommand(40175, 0) -- Item properties: Toggle mute
  -- <<<
  
  reaper.Undo_EndBlock(script_name, -1) -- end undo block
end

if last_undo == script_name then
  if reaper.MB("Are you sure you want to run this script?", script_name, 4) == 6 then
    myfunc() 
  end 
else
  myfunc() 
end
Edgemeal is offline   Reply With Quote
Old 02-19-2019, 08:37 PM   #3
Breeder
Human being with feelings
 
Breeder's Avatar
 
Join Date: Nov 2010
Posts: 2,436
Default

Quote:
Originally Posted by andyp24 View Post
Hi

I'm developing a script which is designed to "correct" some of the (in some circumstances) unhelpful results of inserting/pasting items with Ripple on.

So, essentially, what I've done is create a "clean up the mess" script to run after pasting or inserting.

However, if I accidentally run it twice, it creates a different kind of mess!

So, I wonder, is it possible to add some code into the script which checks what the PREVIOUS action was before it was called?

Ideally, I'd be able to know whether the previous thing that happened in the project was pasting something or inserting from Media Explorer. But if that's not possible, even being able to know whether this script was also the previous thing to be done would help.

Any ideas?

Thanks
You can use ExtState API to write things to memory. So create scripts for all the tasks you do and write stuff to ExtState depending on what you do. And then make those same scripts read that ExtState data and do what needs to be done depending on ExtState data. In short - use ExtState data! xd
Breeder is offline   Reply With Quote
Old 02-20-2019, 02:34 AM   #4
andyp24
Human being with feelings
 
andyp24's Avatar
 
Join Date: Mar 2016
Posts: 1,242
Default

Thank you both....

I did wonder previously about ExtState but couldn't see how it would work. If my script writes an ExtState value to say "I did this", and then the next 3 actions are Native, it will still be there just the same as if no other actions took place. I guess I could write a defer script to stop it being run again twice within 5 seconds or something....

I'll check out the CanUndo code and see if just returning the script action ID works.

Really all I'm trying to do is prevent the "double bounce" of the trigger hotkey, or (as the "clean up" movement of items may be minimal) the "did I actually press the key?" moment.

All it saves me is having to press Ctrl-Z once if I do it, so no big deal really 😁

I appreciate the help. Thanks.

Andy
andyp24 is offline   Reply With Quote
Old 02-20-2019, 03:26 AM   #5
X-Raym
Human being with feelings
 
X-Raym's Avatar
 
Join Date: Apr 2013
Location: France
Posts: 9,900
Default

Quote:
I'm developing a script which is designed to "correct" some of the (in some circumstances) unhelpful results of inserting/pasting items with Ripple on.

Developing scripts to mimic/correct some ripple editing things is not easy. I'm remember very well ^^



What are you trying to do ?
X-Raym is offline   Reply With Quote
Old 02-20-2019, 04:32 AM   #6
andyp24
Human being with feelings
 
andyp24's Avatar
 
Join Date: Mar 2016
Posts: 1,242
Default

Haha yes I know!! That's one reason why I'm (initially at least) writing something that "fixes the mess" rather than replacing the native function!!

Your scripts for me are still working fine, although I have now also developed alternatives which work via different methods to achieve subtly different results.

A.

��

Oh, sorry, I forgot to answer the final question! When pasting an item into the timeline, or inserting from Media Explorer, if Ripple All is on, then any items on different tracks which overlap the insert point get " broken in two", with the end of them Rippling and the beginning staying fixed. This is undesirable when (for example) pasting into a radio programme/podcast.

So my script will act after the paste insert to correct this so the result is as though only items which BEGIN after the insert point get moved.

Last edited by andyp24; 02-20-2019 at 04:37 AM. Reason: Forgot to answer the question!
andyp24 is offline   Reply With Quote
Old 02-20-2019, 04:37 AM   #7
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,630
Default

Why not creating a custom action, where you combine the ripple-on-action with your script?

So you would do this custom-action with 1) the ripple-on-action and then 2)the cleaning up the mess-script.

All you would need to add to your script as well would be a check, if ripple is set to on and if yes, do the cleanup, if not, do nothing.

Just add the old shortcut to the new custom-action and it should work.

If I understood right...
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is offline   Reply With Quote
Old 02-20-2019, 04:43 AM   #8
andyp24
Human being with feelings
 
andyp24's Avatar
 
Join Date: Mar 2016
Posts: 1,242
Default

Hi Mespotine

Yes I thought of that, which would work for pasting from the buffer (crtl-c, ctrl-v).

However I also need to be able to do this clean up action after inserting directly from Media Explorer and I don't think those operations are available as actions or API functions. So I'd still need the standalone clean up script anyway I think.

A.
andyp24 is offline   Reply With Quote
Old 02-20-2019, 05:48 AM   #9
X-Raym
Human being with feelings
 
X-Raym's Avatar
 
Join Date: Apr 2013
Location: France
Posts: 9,900
Default

Quote:
Your scripts for me are still working fine, although I have now also developed alternatives which work via different methods to achieve subtly different results.
Glad to hear it



Quote:
When pasting an item into the timeline, or inserting from Media Explorer, if Ripple All is on, then any items on different tracks which overlap the insert point get " broken in two", with the end of them Rippling and the beginning staying fixed. This is undesirable when (for example) pasting into a radio programme/podcast.

I definitly see what your problem here is.


We can really see that this ripple editing was design with music (loops) in mind with such problems. That's too bad cause it's also really useful in audio podcast / fictions !


IMHO Rather than a cleaning the mess script, a script which doesn't produce the mess in a first place could be better. cause Ripple can be a lot of mess (split items etc...)
X-Raym is offline   Reply With Quote
Old 02-20-2019, 05:50 AM   #10
X-Raym
Human being with feelings
 
X-Raym's Avatar
 
Join Date: Apr 2013
Location: France
Posts: 9,900
Default

A simpel approach would be a script to


"Move everything after selected item start pos next to the right edge item" (bad namming but you see what I mean).



You'll insert and paste without ripple, and use this script to do the ripple after wards. This would work with any action to insert items.

Last edited by X-Raym; 02-20-2019 at 05:55 AM.
X-Raym is offline   Reply With Quote
Old 02-20-2019, 06:26 AM   #11
andyp24
Human being with feelings
 
andyp24's Avatar
 
Join Date: Mar 2016
Posts: 1,242
Default

Absolutely x-raym. The current "cleanup" is a temp fix for something that drives me crazy when working.

Your suggestion is correct, and I've done some thinking about it already. However I can't see a way to get the concept to work when inserting directly from the Media Explorer.

What I don't want to have to do is manually turn off Ripple before every item paste/insert, then run some other script to Ripple things, then turn Ripple back on. Thus, for the moment, with my clean up script I only have to run that; I just leave Ripple alone all the time in whatever mode it's in.
andyp24 is offline   Reply With Quote
Old 02-20-2019, 06:36 AM   #12
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,630
Default

You could try to monitor, using JS-extension-functions, what of the UI has been clicked and react to it accordingly using a defer-script.
This is probably overkill, but would be a way to do it.

Unfortunately, programming the MediaExplorer is really limited... :/
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is offline   Reply With Quote
Old 02-20-2019, 06:58 AM   #13
X-Raym
Human being with feelings
 
X-Raym's Avatar
 
Join Date: Apr 2013
Location: France
Posts: 9,900
Default

@andy
toggling ripple is just a key press :P and the script could restore it afterward.



if you want to avoid that toggming off then you may prefer Insertions scripts which could work with Js reascript extension. not need for defer script.


For the ripple part I have a basic idea how it could work (pretty similar to scripts I made for you already).
X-Raym is offline   Reply With Quote
Old 02-20-2019, 07:07 AM   #14
andyp24
Human being with feelings
 
andyp24's Avatar
 
Join Date: Mar 2016
Posts: 1,242
Default

@x-raym ��

How can the script toggle it back on? It wouldn't know which mode I was in before I temporarily turned it off to make the insertion! (Since that would happen before calling the script).

The way I have it running now is:

Either from ctrl-v paste or from the Media Explorer I make an insertion in the timeline. That will potentially mess things up if either Ripple Mode is on. So after the insertion, I just press one key to run my clean up script.

If Ripple is off, it does nothing. If one of the Ripple modes is on, it sorts out the mess and leaves the Ripple Mode as it was before.

Yes, it would be cleaner to do all this as part of the insertion process but unless I can find a way to get that to work with inserting from Media Explorer, I'll still need this anyway.

I'll try and read up on those js insertions scripts to see what they are.

Thanks
andyp24 is offline   Reply With Quote
Old 02-20-2019, 07:14 AM   #15
X-Raym
Human being with feelings
 
X-Raym's Avatar
 
Join Date: Apr 2013
Location: France
Posts: 9,900
Default

You can get Set ToggleCommandState with native API :P Just pass the tggle ripple edit action command ID as parameter.
X-Raym is offline   Reply With Quote
Old 02-20-2019, 07:21 AM   #16
andyp24
Human being with feelings
 
andyp24's Avatar
 
Join Date: Mar 2016
Posts: 1,242
Default

I realise that (and my clean up script already toggles Ripple on and off a couple of times to do its work).

Unless I misunderstood you, you were suggesting that I should manually turn off Ripple (if it's on), and then make my insertion with your script that could turn Ripple back on at the end. But my point is that it could not know what state the Ripple was in before I manually turned it off... (if I did, it might have been off all the time anyway)!
andyp24 is offline   Reply With Quote
Old 02-20-2019, 07:32 AM   #17
andyp24
Human being with feelings
 
andyp24's Avatar
 
Join Date: Mar 2016
Posts: 1,242
Default

If the only scenario was that I was pasting in from the buffer, then absolutely I would write the script to do it all in one go. But since I would still need to "clean up" after insertion from Media Explorer then for now, I'm focussing on making that work with both situations.

Then, if I want to make a single keystroke do the Paste scenario, it's dead easy to adapt.
andyp24 is offline   Reply With Quote
Old 02-20-2019, 07:41 AM   #18
X-Raym
Human being with feelings
 
X-Raym's Avatar
 
Join Date: Apr 2013
Location: France
Posts: 9,900
Default

Here is the illustration of how your problem could be solved:





This works with already instered items, so matter how they get there. But they have tobe inserted without ripple editing to not SPLIT items.
X-Raym is offline   Reply With Quote
Old 02-20-2019, 07:47 AM   #19
andyp24
Human being with feelings
 
andyp24's Avatar
 
Join Date: Mar 2016
Posts: 1,242
Default

Indeed, although as with the custom scripts you did for me previously, I need the Ripple Jump to take long item fade outs into account, rather than snapping to item edges.

But, just to be clear.... I have my clean up script working already!! It does what I need. Before we got sidetracked by the discussion of whether a "do it in one go" or "clean up after" approach was better, the original question was simply "is there a way to stop me doing it accidentally twice?"

��

I can't show a video as I'm on a car journey with my phone (not driving!!) But it works to clean up the splits in items caused by inserting without turning Ripple off first, and then Ripples items which BEGIN after the insert point by the required amount.

It's just when testing it yesterday, I realised that sometimes I may run the script and it does its work, but I don't see the change (because of where the view is zoomed to) so I think I haven't pressed the key and do it again which creates a different kind of mess! I wondered if there's a way to prevent that (other than being less incompetent personally!!)
andyp24 is offline   Reply With Quote
Old 02-20-2019, 07:49 AM   #20
X-Raym
Human being with feelings
 
X-Raym's Avatar
 
Join Date: Apr 2013
Location: France
Posts: 9,900
Default

@andyp24
Ok got it :P



So the thread can be marked as solved cause there is already answers about how to do it.
X-Raym is offline   Reply With Quote
Old 02-20-2019, 07:56 AM   #21
andyp24
Human being with feelings
 
andyp24's Avatar
 
Join Date: Mar 2016
Posts: 1,242
Default

I need a chance to investigate the suggestions made about that first, but for now, we could just stop posting

Seriously, as always I appreciate your willingness to discuss and contribute.

Andy
andyp24 is offline   Reply With Quote
Old 02-20-2019, 08:10 AM   #22
mschnell
Human being with feelings
 
mschnell's Avatar
 
Join Date: Jun 2013
Location: Krefeld, Germany
Posts: 14,785
Default

Since some time scripts feature global variables (even shared with JSFXes) . I suppose those are persistent ...

-Michael
mschnell is offline   Reply With Quote
Old 02-20-2019, 01:22 PM   #23
andyp24
Human being with feelings
 
andyp24's Avatar
 
Join Date: Mar 2016
Posts: 1,242
Default

Quote:
Originally Posted by X-Raym View Post
@andy

if you want to avoid that toggming off then you may prefer Insertions scripts which could work with Js reascript extension. not need for defer script.
I've looked up the JS extensions, and I don't see what you're meaning. Can you explain further please?

Cheers
Andy
andyp24 is offline   Reply With Quote
Old 02-22-2019, 09:48 AM   #24
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,630
Default

I want to propose another idea, that is probably easier to implement.

A background script, that recognises, if something like Ripple-editing has happened, which is probably: change in length of the project and/or the number of items in the project.

I would say, you should check for the following things:

if Ripple==on
and
(ProjectLength~=oldProjectLength
or
CountMediaItems~=oldCountMediaItems)

then

run the cleanupaction



The only thing, that is important: how stable is your cleanup-script working?
Does it properly clean up, if you just insert a new item at the end of the project without affecting anything else?
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is offline   Reply With Quote
Old 02-22-2019, 01:26 PM   #25
andyp24
Human being with feelings
 
andyp24's Avatar
 
Join Date: Mar 2016
Posts: 1,242
Default

Nice idea, mespotine.

Yes, I believe the clean up action itself is working properly (it's really not all that complicated!) but I do need to test it more when I have more time.

I was trying to avoid a background script, especially as I have another idea in progress which absolutely DOES require a defer script watching for changes all the time, and I don't want to load the CPU down too much.

But I can see the advantage of monitoring whether the project has had changes made in the length of the timeline, and cleaning up accordingly automatically. I don't think just counting project items would work, as I suspect there'd be no easy way to distinguish between a new item being pasted in, and an existing item being split. Equally, just checking the length of the project wouldn't work, as dragging an item with Ripple On would affect this. So it would have to look at both.... if the number of items AND the project length has changed, then (probably) a new item has been pasted or dragged into the timeline somewhere. I'll have to think if there are exceptions to this rule. But if I can detect such changes (also for items being CUT and DELETED with similar effect on the timeline length/count) then I can automatically run a clean up action for these too.... would be good.

Could be tricky though, as Cutting one item might result in an item on another track being split in two, thus the total number of items does not change..... :-(

Thanks for the ideas. My work schedule is totally crazy for the next few weeks (although I have some nights staying away in hotels where I may be able to do some more on these), but I'm hoping to release some useful scripts (at least in Beta form) over the next few months.

As I'm definitely a newbie to coding, they're nothing too complicated, but they're all definitely useful to the kind of workflows I use for Radio/Podcast work.

Cheers
Andy

Last edited by andyp24; 02-22-2019 at 02:17 PM.
andyp24 is offline   Reply With Quote
Old 02-26-2019, 10:12 AM   #26
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,630
Default

Quote:
Originally Posted by andyp24 View Post
Nice idea, mespotine.

Yes, I believe the clean up action itself is working properly (it's really not all that complicated!) but I do need to test it more when I have more time.

I was trying to avoid a background script, especially as I have another idea in progress which absolutely DOES require a defer script watching for changes all the time, and I don't want to load the CPU down too much.

But I can see the advantage of monitoring whether the project has had changes made in the length of the timeline, and cleaning up accordingly automatically. I don't think just counting project items would work, as I suspect there'd be no easy way to distinguish between a new item being pasted in, and an existing item being split. Equally, just checking the length of the project wouldn't work, as dragging an item with Ripple On would affect this. So it would have to look at both.... if the number of items AND the project length has changed, then (probably) a new item has been pasted or dragged into the timeline somewhere. I'll have to think if there are exceptions to this rule. But if I can detect such changes (also for items being CUT and DELETED with similar effect on the timeline length/count) then I can automatically run a clean up action for these too.... would be good.

Could be tricky though, as Cutting one item might result in an item on another track being split in two, thus the total number of items does not change..... :-(

Thanks for the ideas. My work schedule is totally crazy for the next few weeks (although I have some nights staying away in hotels where I may be able to do some more on these), but I'm hoping to release some useful scripts (at least in Beta form) over the next few months.

As I'm definitely a newbie to coding, they're nothing too complicated, but they're all definitely useful to the kind of workflows I use for Radio/Podcast work.

Cheers
Andy

As far as I understood, replacing items shouldn't affect rippleing, so you should(tm) be safe(tm).

But you could also check using
https://mespotin.uber.space/Mespotin...ateChangeCount
or even monitoring all MediaItems.
Create an array with all MediaItems and compare them every one second or so with the current one.
But this means, a whole lot of processing, especially with many MediaItems in your project.

But try out my approach first, this should do it mostly, if not completely.

And if not, determine the conditions, why it fails and post them here and we'll find a way to deal with them as well
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine 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 03:36 AM.


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