Old 03-02-2017, 08:12 AM   #1
DaveKeehl
Human being with feelings
 
DaveKeehl's Avatar
 
Join Date: Nov 2015
Location: Switzerland
Posts: 1,966
Default Scripts with custom actions?

Hi,

I'm quite new to scripting (just a little background of JavaScript and jQuery), especially to Reaper scripts.

I know that to perform actions I need to use Main_OnCommand(actionID, 0), but I've noticed that if I copy a custom action's ID, I get an error when I try to save the script.

Am I doing something wrong or custom actions can't be performed via ReaScripts?
__________________
REAPER Contest

Last edited by DaveKeehl; 03-02-2017 at 08:19 AM.
DaveKeehl is offline   Reply With Quote
Old 03-02-2017, 08:20 AM   #2
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

For custom actions and scripts you need to get action ID that Reaper has the script's GUID - "_ah4wp93pa98uf1awhatever" assigned to.

This is how I do it in Lua, along with some error checking:
Code:
-- 'act' is the action/script ID in a string

-- Scripts and custom actions all use the same format for their GUID,
-- so you just need to see if the string starts with a '_'
if string.sub(act, 1, 1) == "_" then
  act = reaper.NamedCommandLookup(act) 
end


-- Not strictly necessary, but Lua doesn't always like it when you try
-- to use a string as a number
act = tonumber(act)

-- Make sure that: 
-- a) tonumber() worked, and 
-- b) we have a useable action ID
if act and act > 0 then 
  reaper.Main_OnCommand(act, 0) 
end
__________________
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 03-02-2017, 08:36 AM   #3
DaveKeehl
Human being with feelings
 
DaveKeehl's Avatar
 
Join Date: Nov 2015
Location: Switzerland
Posts: 1,966
Default

Sorry I'm not sure where I have to put the ID (_93e0a9b2d00c0b4ebc0e93ae2ba39519) or the action name
__________________
REAPER Contest
DaveKeehl is offline   Reply With Quote
Old 03-02-2017, 08:48 AM   #4
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

Just add this before the code above:

Code:
local act = "_93e0a9b2d00c0b4ebc0e93ae2ba39519"
__________________
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 03-02-2017, 08:51 AM   #5
DaveKeehl
Human being with feelings
 
DaveKeehl's Avatar
 
Join Date: Nov 2015
Location: Switzerland
Posts: 1,966
Default

Oh ok. So the whole bunch of code would be:


local act = "_93e0a9b2d00c0b4ebc0e93ae2ba39519"

if string.sub(act, 1, 1) == "_" then
act = reaper.NamedCommandLookup(act)
end

act = tonumber(act)
if act and act > 0 then
reaper.Main_OnCommand(act, 0)
end


Correct ?
__________________
REAPER Contest
DaveKeehl is offline   Reply With Quote
Old 03-02-2017, 08:54 AM   #6
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

Correct.
__________________
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 03-02-2017, 09:13 AM   #7
DaveKeehl
Human being with feelings
 
DaveKeehl's Avatar
 
Join Date: Nov 2015
Location: Switzerland
Posts: 1,966
Default

Ok thanks and how can I create a for cycle?

edit: well it doesn't really matter... I can look it up online
__________________
REAPER Contest

Last edited by DaveKeehl; 03-02-2017 at 09:32 AM.
DaveKeehl is offline   Reply With Quote
Old 03-02-2017, 12:51 PM   #8
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

In case you haven't found it yet (you probably have), a basic for loop in Lua looks like this:
Code:
for i = 1, 10 do
  reaper.ShowConsoleMsg("i = "..i.."\n")
end
You can also provide a third number to use as the 'step' amount:
Code:
for i = 10, 1, -1 do
 ...
end

for i = 1, 10, 0.5 do
 ...
end
Also useful is being able to exit the loop early:
Code:
for i = 1, 10 do
  reaper.ShowConsoleMsg("i = "..i.."\n")
  if i == 8 then
    break
  end
end
__________________
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 03-02-2017, 02:09 PM   #9
DaveKeehl
Human being with feelings
 
DaveKeehl's Avatar
 
Join Date: Nov 2015
Location: Switzerland
Posts: 1,966
Default

Yes, I had found something but I didn't have the time to sit down and learn how to code in Lua. Thanks anyway, that's very helpful.

My goal is to create a script that can perform an action as many times as the number of the tracks. In JS would be something like this (using a bit of the Reaper API):

Code:
var trackNumber = integer reaper.GetNumTracks()

for (var i = 0; i < trackNumber; i++) {
  custom action();
}
__________________
REAPER Contest
DaveKeehl is offline   Reply With Quote
Old 03-02-2017, 02:24 PM   #10
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

Easy peasy:
Code:
local trackNumber = reaper.GetNumTracks()

for i = 0, (trackNumber - 1) do
  custom_action()
end
__________________
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 03-02-2017, 02:55 PM   #11
DaveKeehl
Human being with feelings
 
DaveKeehl's Avatar
 
Join Date: Nov 2015
Location: Switzerland
Posts: 1,966
Default

thanks!
__________________
REAPER Contest
DaveKeehl 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 09:09 AM.


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