Old 09-14-2019, 11:50 AM   #1
kenm
Human being with feelings
 
Join Date: Dec 2011
Location: San Jose, CA, USA
Posts: 115
Default Run a background script at startup

I know about __startup.lua and I know I can execute commands from it when Reaper starts, but what about using it to start a background script?

Would dofile() be acceptable for starting a background script that utilizes defer() or is there another or better way?


EDIT: Just to be clear... I've tested dofile() and it works. I'm just not sure it's the best way to do it.

Thanks,
Ken

Last edited by kenm; 09-14-2019 at 11:57 AM. Reason: See EDIT:
kenm is offline   Reply With Quote
Old 09-14-2019, 11:56 AM   #2
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 4,937
Default

Yes, dofile from __startup.lua is fine and also SWS startup actions. A "background script" is just a script like all others.

EDIT: As suggested by Lokasenna below, Main_OnCommand(+NamedCommandLookup) is much better than dofile in this context.

Last edited by cfillion; 09-14-2019 at 12:25 PM.
cfillion is offline   Reply With Quote
Old 09-14-2019, 12:03 PM   #3
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

dofile does run the child script in the same environment as the calling script.

I would suggest registering the script as an action and then running it from the startup script via Main_OnCommand - then it's completely separate from anything else you might want to do at startup*.

For instance, if you had five different background scripts you'd probably want them completely isolated from each other to avoid accidentally sharing any global values, not letting a crash take them all down, etc.

---

*Now that I think about it, a GUI script to help people manage startup actions without editing the startup file directly might be a good idea, similar to Nexus Mod Manager, and would really only make sense using the action approach above.
__________________
I'm no longer using Reaper or working on scripts for it. Sorry. :(
Default 5.0 Nitpicky Edition / GUI library for Lua scripts / Theory Helper / Radial Menu / Donate
Lokasenna is offline   Reply With Quote
Old 09-14-2019, 12:32 PM   #4
kenm
Human being with feelings
 
Join Date: Dec 2011
Location: San Jose, CA, USA
Posts: 115
Default

Quote:
Originally Posted by Lokasenna View Post
dofile does run the child script in the same environment as the calling script.

I would suggest registering the script as an action and then running it from the startup script via Main_OnCommand - then it's completely separate from anything else you might want to do at startup*.

For instance, if you had five different background scripts you'd probably want them completely isolated from each other to avoid accidentally sharing any global values, not letting a crash take them all down, etc.

---

*Now that I think about it, a GUI script to help people manage startup actions without editing the startup file directly might be a good idea, similar to Nexus Mod Manager, and would really only make sense using the action approach above.
True enough. The bottom of the Actions Menu shows __startup.lua still running.

Is there a way to define a consistent Action ID or should i just use NamedCommandLookup()?
kenm is offline   Reply With Quote
Old 09-14-2019, 12:55 PM   #5
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,621
Default

Always use NamedCommandLookup to be on the safe side.
__________________
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 09-14-2019, 01:22 PM   #6
kenm
Human being with feelings
 
Join Date: Dec 2011
Location: San Jose, CA, USA
Posts: 115
Default

Quote:
Originally Posted by mespotine View Post
Always use NamedCommandLookup to be on the safe side.
OK. Sorry for being "thick in the head" but what's the call expecting?

I've tried:

val = reaper.NamedCommandLookup("Custom: ATOM_polling.lua") <-- From ReaperKeyMap file
val = reaper.NamedCommandLookup("Script: ATOM_polling.lua") <-- From Action Menu Text
val = reaper.NamedCommandLookup("_RSf8e4f6430c6d03c69b2d 5f2519ef97465317e503") <-- From Action Menu Command ID

but 'val' is always '0'.
kenm is offline   Reply With Quote
Old 09-14-2019, 01:41 PM   #7
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,621
Default

https://mespotin.uber.space/Ultrasch...dCommandLookup


An example of such a call should be:

val=reaper.NamedCommandLookup("_RS6cb47ecf95a7665d f0c0de4e1c47746792fe5567")

If it returns 0, you probably either have a typo in the ActionCommandID or the ActionCommandID does not exist.

Have you tried other actions as well? You can try a number as well like 1007 and it should return 1007 as value.

val=reaper.NamedCommandLookup("1007") -- should return 1007

if it does not for some reason, then you found a bug.
__________________
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 09-14-2019, 02:32 PM   #8
kenm
Human being with feelings
 
Join Date: Dec 2011
Location: San Jose, CA, USA
Posts: 115
Default

Not sure if this is a bug, an anomaly, or just the way it is, but...

This works if run from a script AFTER Reaper is up and running:

Code:
  val = reaper.NamedCommandLookup("_RSf8e4f6430c6d03c69b2d5f2519ef97465317e503")
  reaper.ShowConsoleMsg(tostring(val))
But shows '0' if executed in __startup.lua.

I suspect custom actions haven't been loaded yet when the startup script is run.
kenm is offline   Reply With Quote
Old 09-14-2019, 03:02 PM   #9
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,621
Default

Hmm..shouldn't be the case. We use this all the time in __startup.lua

Maybe you need to report this as a bug in the bug-reports-section.
__________________
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 09-14-2019, 04:09 PM   #10
kenm
Human being with feelings
 
Join Date: Dec 2011
Location: San Jose, CA, USA
Posts: 115
Default

Quote:
Originally Posted by mespotine View Post
Hmm..shouldn't be the case. We use this all the time in __startup.lua

Maybe you need to report this as a bug in the bug-reports-section.
It is a bit odd. Do you use RedScript actions or actions created by extensions? I check and the SWS actions seems to enumerate correctly. It's only ReaScript actions that don't.

I ended up moving the NamedCommadLookup() to a function within my ATOM.lua module and it seems to work correctly now. I'll write up something in the bug-reports-section.

Thanks,
Ken
kenm is offline   Reply With Quote
Old 09-15-2019, 11:50 AM   #11
kenm
Human being with feelings
 
Join Date: Dec 2011
Location: San Jose, CA, USA
Posts: 115
Default

Hi @mespotine,

Do you happen to be running on Windows? I'm on a Mac.

As it turns out, the bug is only in the Mac version. It works as expected and as you stated in the Windows version.

Bug report forthcoming.

Ken
kenm is offline   Reply With Quote
Old 09-15-2019, 02:58 PM   #12
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,621
Default

Ok, this sounds like a bug.

Can you post a bugreport into the bugreports-subforum and include which Reaper version you use?
And can you post the accompanying scripts there as well, so the devs can test them directly?
__________________
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 09-15-2019, 04:09 PM   #13
kenm
Human being with feelings
 
Join Date: Dec 2011
Location: San Jose, CA, USA
Posts: 115
Default

bug report posted.

Ken
kenm 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 02:47 AM.


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