Reaper-Tools

Audiokinetic Reaper Tools Repository
Log | Files | Refs

Utilities.lua (3968B)


      1 --[[
      2   @description Utilities
      3   @author Audiokinetic
      4   @noindex
      5   @provides
      6     [nomain] . https://raw.githubusercontent.com/audiokinetic/Reaper-Tools/$commit/OpenAssociatedReaperProject/Utilities.lua
      7   @about
      8     Contains utility functions and constant used by the others scripts of the Open Associated REAPER Project package.
      9   @license
     10     Copyright (c) 2023 AUDIOKINETIC Inc.
     11 
     12     The script in this file is licensed to use under the license available at:
     13     https://raw.githubusercontent.com/audiokinetic/Reaper-Tools/main/License.txt (the "License").
     14     You may not use this file except in compliance with the License.
     15 
     16     Unless required by applicable law or agreed to in writing, software distributed
     17     under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
     18     CONDITIONS OF ANY KIND, either express or implied.  See the License for the
     19     specific language governing permissions and limitations under the License.
     20 ]]--
     21 
     22 local Utilities = {}
     23 
     24 -- constants --
     25 Utilities.OK_RESULT = 1
     26 Utilities.CANCEL_RESULT = 2
     27 Utilities.OK_MESSAGE_BOX = 0
     28 Utilities.OK_CANCEL_MESSAGE_BOX = 1
     29 Utilities.TRACK_VIEW_ID = 1000
     30 Utilities.CURRENT_PROJECT = 0
     31 Utilities.SEPARATOR_CHAR = string.find(reaper.GetOS(), "Win") ~= nil and "\\" or "/"
     32 local BROWSER_CMD = string.find(reaper.GetOS(), "Win") ~= nil and "start" or "open"
     33 
     34 -- helper functions --
     35 function Utilities.WindowsOS()
     36   return string.find(reaper.GetOS(), "Win") == 1
     37 end
     38 
     39 function Utilities.openUrl(url)
     40   os.execute(BROWSER_CMD .. ' "" "' .. url .. '"')
     41 end
     42 
     43 function Utilities.getReaperPath()
     44   local reaperPath = tostring(reaper.GetExePath())
     45   if Utilities.WindowsOS() then
     46     reaperPath = reaperPath.."\\"
     47     reaperPath = reaperPath:gsub("\\", "\\\\")
     48   else
     49     reaperPath = reaperPath.."/"
     50   end
     51   return reaperPath
     52 end
     53 
     54 function Utilities.getAppData()
     55   local appData = ""
     56   if not Utilities.WindowsOS() then
     57     appData = os.getenv("HOME").."/Library/Application Support"
     58   else
     59     appData = os.getenv("APPDATA")
     60   end
     61   return appData
     62 end
     63 
     64 function Utilities.getMetadata(pcmSource, list)
     65   for i = 1, #list do
     66     local _, candidate = reaper.GetMediaFileMetadata(pcmSource, list[i])
     67 
     68     if candidate and candidate ~= "" then
     69       return candidate
     70     end
     71   end
     72 
     73   return nil
     74 end
     75 
     76 function Utilities.getParentDirectory(path)
     77   local pathReversed = string.reverse(path)
     78 
     79   local nextSeperatorChar = string.find(pathReversed, Utilities.SEPARATOR_CHAR, 1)
     80 
     81   return string.sub(path, 0, string.len(path) - nextSeperatorChar)
     82 end
     83 
     84 -- JS dependency check --
     85 function Utilities.JSDependencyCheck()
     86   if not reaper.JS_Window_FindChildByID or not reaper.JS_Window_Find or not reaper.JS_Window_SetScrollPos or not reaper.JS_Dialog_BrowseForFolder then
     87       local selectedOption = reaper.ShowMessageBox("The script requires the latest version of js_ReaScriptAPI be installed.\n\nView in ReaPack?", "Open Associated REAPER Project: Failed", Utilities.OK_CANCEL_MESSAGE_BOX)
     88 
     89       if selectedOption == Utilities.OK_RESULT then
     90         if reaper.ReaPack_BrowsePackages then
     91           reaper.ReaPack_BrowsePackages("js_ReaScriptAPI")
     92         else
     93           selectedOption = reaper.ShowMessageBox("Unable to locate ReaPack. You can download ReaPack by going to https://reapack.com.\n\nGo there now?", "Open Associated REAPER Project: Failed", Utilities.OK_CANCEL_MESSAGE_BOX)
     94 
     95           if selectedOption == Utilities.OK_RESULT then
     96             openUrl("https://reapack.com")
     97           end
     98         end
     99       end
    100       return false
    101   end
    102   return true
    103 end
    104 
    105 -- REAPER minimum version requirement check
    106 function Utilities.ReaperMinimumVersionCheck()
    107   local version = reaper.GetAppVersion()
    108   version = version:match("[0-9]+%.[0-9]+")
    109   if tonumber(version) < 6.80 then
    110     reaper.ShowMessageBox("The required minimum version of REAPER is v6.80. Please update REAPER before running this script.", "Open Associated REAPER Project", 0)
    111     return false
    112   end
    113   return true
    114 end
    115 
    116 return Utilities