ReaLive-ProjectTabsList/Realive_ProjectTabsList_CloseAllTabs.lua

269 lines
8.4 KiB
Lua

--------------------------------------
----------// HEADER //----------------
--------------------------------------
--[[
* REALIVE: Reaper Setup for a Complete Live Playback Engine
* ReaScript Name: Realive_ProjectTabsList_CloseAllTabs.lua
* Description: Close All Project Tabs
* Instructions:
* Author: Alex Paquette, Thanks to Claude Fillion for the Large contribution
* Repository:
* Repository URI:
* File URI:
* Licence: GPL v3
* Forum Thread:
* Forum Thread URI: https://forum.cockos.com/showthread.php?p=1854775#post1854775
* REAPER: 5
* Extensions: SWS/S&M
* Version: 2.00
--]]
--[[
* Changelog:
* v1.0 (2019-06-02)
+ Initial Release
--]]
--------------------------------------
----------// VARIABLES //-------------
--------------------------------------
local reaper = reaper -------// Performance Enhancement //----------
local EXT_SECTION = 'ProjectTabs_Reaper_list'
local EXT_SECTION_PLAYLIST = 'ProjectTabs_Realive_list'
-----// Window Set Numbers //------------------------------
local PlayViewWindowSet = 01 -- Enter Window Set Number Used for the "Live Play" View
local AllTracksViewWindowSet = 02 -- Enter the Window Set Number for the "Show All Tracks" View
local WindowSetBaseNumber = 40453
local PlayView_cmdID = WindowSetBaseNumber+PlayViewWindowSet
local AllView_cmdID = WindowSetBaseNumber+AllTracksViewWindowSet
local RegionWindow_cmdID = reaper.NamedCommandLookup("_RS05396ad2c5c53e928799e707652dea37cc588b3c")
local ResetPlayView_cmdID = reaper.NamedCommandLookup("_RS08d5f2749008cc0f6f53feb223d1638fde598566")
local REAPERPath = reaper.GetResourcePath()
local ProjectPath = (REAPERPath.."//ProjectTemplates")
local ProjectName = "AudioRouting.RPP"
local debug = false
-----// Project Templates //------------------------------
local Template_AudioRouting_cmdID = reaper.NamedCommandLookup("_RS46bebf2345d0c062f8ec5e72df8bc80190970cfb")
local Template_LineCheck_cmdID = reaper.NamedCommandLookup("_RS9799aed89bdfcc32214f3bd1e17ec57ba42ae7eb")
local Template_Song_cmdID = reaper.NamedCommandLookup("_RS62e37ab3059a6dab6616675ecd3c4c83162a0859")
-----// gmem Variables //------------------------------
local gmemSect = 'Realive_Gmem'
local gmem_idx_PlaylistState = 11
local gmem_idx_Management = 12
local gmem_idx_PlaylistState_Clean = 32
local gmem_idx_PlaylistCount_Reaper = 30
local gmem_idx_PlaylistCount_Realive = 31
local gmemPlaylist_SaveRequest = 1
local gmemPlaylist_SaveRefused = 2
local gmemPlaylist_Initialize = 3
local gmemPlaylist_RealiveListItem_Remove = 4
local gmemPlaylist_RealiveListItem_Add = 5
local gmemPlaylist_RealiveListItem_Update = 6
local gmemPlaylist_ProjectTabs_CleanStatus = 9
local gmem_idxRead_Mngmt = reaper.gmem_read(gmem_idx_Management)
local playlistState = reaper.GetExtState('Realive_Playlist', 'State')
local realivePlaylist_compare = 0
-----------------------------------------------------------
-----// DEBUG FUNCTION //----------------------------------
-----------------------------------------------------------
if debug then reaper.ShowConsoleMsg("") end
function Msg(value)
if debug then
reaper.ShowConsoleMsg(tostring(value) .. "\n")
end
end
-----------------------------------------------------------
-----// FUNCTIONS //---------------------------------------
-----------------------------------------------------------
function Playlist_CountProjectTabs()
local ProjCount=-1
local Aretval="t"
while Aretval~=nil do
local Aretval, Aprojfn = reaper.EnumProjects(ProjCount+1, "")
if Aretval~=nil then ProjCount=ProjCount+1
else break
end
end
return ProjCount+1
end
function Actions_ResetAllProjects()
local prjct_Counter = Playlist_CountProjectTabs()
for p = 0, prjct_Counter do
local proj = reaper.EnumProjects(p, 0)
if not proj then break
else
reaper.SetEditCurPos2(proj, 0, true, true)
end
end
end
-----------------------------------------------------------
-----// Realive List Management //-------------------------
-----------------------------------------------------------
function makeKey(i)
return string.format('project%d', i)
end
function enum_RealivePlaylist()
local i = 1
return function()
local key = makeKey(i)
i = i + 1
if reaper.HasExtState(EXT_SECTION_PLAYLIST, key) then
return key
end
end
end
function get_RealivePlaylist()
local Realive_list = {}
for Realive_key in enum_RealivePlaylist() do
local Realive_projfn = reaper.GetExtState(EXT_SECTION_PLAYLIST, Realive_key)
table.insert(Realive_list, Realive_projfn)
end
return Realive_list
end
-----------------------------------------------------------
-----// SAVE RPL FUNCTION //-------------------------------
-----------------------------------------------------------
function SaveRPL()
local out_str = ''
local targetPath = ''
if reaper.HasExtState('Realive_PlaylistFolder', 'lastPath') then
lastPath = reaper.GetExtState('Realive_PlaylistFolder', 'lastPath')
end
if reaper.HasExtState('Realive_PlaylistFolder', 'defaultPath') then
defaultPath = reaper.GetExtState('Realive_PlaylistFolder', 'defaultPath')
end
if lastPath ~= '' then targetPath = lastPath
elseif defaultPath ~= '' then targetPath = defaultPath
end
local unsavedConfirm = 0
local playlist = get_RealivePlaylist()
local projcnt = Playlist_CountProjectTabs()
local UnsavedProj_cnt = 0
for i = 1, projcnt do
local retval, projfn= reaper.EnumProjects( i-1, '' )
if projfn =='' then UnsavedProj_cnt = UnsavedProj_cnt+1 end
Msg('projfn: '..projfn)
end
Msg('UnsavedProject: '..UnsavedProj_cnt)
if UnsavedProj_cnt > 0 then
unsavedConfirm = reaper.ShowMessageBox("Your list contains project(s) that have no name. They will not be saved in the playlist. Do you still want to proceed? ", 'Warning', 4) -- Yes = 6, No = 7, Cancel = 2
Msg('unsavedConfirm: '..unsavedConfirm)
if unsavedConfirm ~= 6 then return end
end
local SaveDialogAnswer, saving_folder = reaper.JS_Dialog_BrowseForSaveFile('Save RPL', targetPath, '', 'Reaper Playlist files (.RPL)\0*.RPL\0\0')
Msg('SaveDialogAnswer: '..SaveDialogAnswer)
if SaveDialogAnswer == 1 then -- if a file was selected
extst_path = string.match(saving_folder, ".+/")
reaper.SetExtState('Realive_PlaylistFolder', 'lastPath', extst_path, false)
Msg('path: '..extst_path)
for i = 1, #playlist do
out_str = out_str..playlist[i]..'\n'
end
local f = io.open(saving_folder, 'w')
f:write(out_str)
f:close()
CloseAllTabs()
return true
else
return false
end
end
-----------------------------------------------------------
-----------------------------------------------------------
function RefreshPlaylist()
-- reaper.gmem_write(gmem_idx_Management, gmemPlaylist_Initialize)
---[[
local cmd_id = reaper.NamedCommandLookup('_RS2071ccff7199c897478c49da97ca1422fc732d4f')
local state = reaper.GetToggleCommandState(cmd_id)
if state == 1 then
reaper.Main_OnCommand(cmd_id, 0)
reaper.Main_OnCommand(cmd_id, 0)
reaper.SetCursorContext(1) --Focus Arrange Window
end
--]]
end
function CloseAllTabs()
reaper.Main_OnCommand(40886, 0) -- Close All Projects
reaper.Main_OnCommand(41929, 0) -- Open empty project
reaper.Main_OnCommand(reaper.NamedCommandLookup("_SWS_FIRSTPROJTAB"), 0) -- Switch to 1st Tab
reaper.Main_OnCommand(40860, 0) -- Close tab
-- reaper.SetExtState('Realive_Playlist', 'audioRouting_Validation', 'NotValid', false)
reaper.Main_openProject('template:'..ProjectPath.."/"..ProjectName)
reaper.Main_OnCommand(40859, 0) -- New Tab Ignore Default Template
-- reaper.Main_OnCommand(Template_Song_cmdID, 0) -- Open Song Tab
RefreshPlaylist()
reaper.Main_OnCommand(ResetPlayView_cmdID, 0) -- Reset Play View
-- reaper.gmem_write(gmem_idx_Management, gmemPlaylist_ProjectTabs_CleanStatus)
-- reaper.SetExtState('Realive_Playlist', 'State', 'Cln', false)
end
------------------------------------------
---------// RUN FUNCTION //--------------
------------------------------------------
function run()
if playlistState == 'Drty' then
realivePlaylist_compare = reaper.ShowMessageBox("Do you want to save your Playlist? ", 'Playlist was Changed', 3) -- Yes = 6, No = 7, Cancel = 2
end
if realivePlaylist_compare == 2 then return end -- if Cancel then Do Nothing
if realivePlaylist_compare == 6 then -- if Yes then Save RPL
SaveRPL()
else
CloseAllTabs()
end
end
------------------------------------------
---------// RUNTIME //-------------------
------------------------------------------
reaper.gmem_attach(gmemSect)
run()