Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kisaragi-hiu/96c3a6becf0e935d4a1a17321ac02e56 to your computer and use it in GitHub Desktop.
Save kisaragi-hiu/96c3a6becf0e935d4a1a17321ac02e56 to your computer and use it in GitHub Desktop.
-- * Header
-- Author: Kisaragi Hiu
-- Date: 2022-03-17T02:54:44+0900
-- License: The Unlicense (<https://unlicense.org/>). Full text below:
--
-- This is free and unencumbered software released into the public domain.
--
-- Anyone is free to copy, modify, publish, use, compile, sell, or
-- distribute this software, either in source code form or as a compiled
-- binary, for any purpose, commercial or non-commercial, and by any
-- means.
--
-- In jurisdictions that recognize copyright laws, the author or authors
-- of this software dedicate any and all copyright interest in the
-- software to the public domain. We make this dedication for the benefit
-- of the public at large and to the detriment of our heirs and
-- successors. We intend this dedication to be an overt act of
-- relinquishment in perpetuity of all present and future rights to this
-- software under copyright law.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-- IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
-- OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
-- ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-- OTHER DEALINGS IN THE SOFTWARE.
--
-- For more information, please refer to <http://unlicense.org/>
-- * Library functions
-- From
-- https://github.com/moteus/lua-path/blob/5a32c705/lua/path.lua#L147
function splitext(P)
local s1, s2 = string.match(P, "(.-[^\\/.])(%.[^\\/.]*)$")
if s1 then
return s1, s2
end
return P, ""
end
-- From
-- https://github.com/moteus/lua-path/blob/5a32c705/lua/path.lua#L153
function splitpath(P)
return string.match(P, "^(.-)[\\/]?([^\\/]*)$")
end
-- Modified from
-- https://github.com/moteus/lua-path/blob/5a32c705/lua/path.lua#L134
--
-- No, I have zero idea what select('#') or #var means
if reaper.GetOS():match("^Win") then
PATH_SEPARATOR = "\\"
else
PATH_SEPARATOR = "/"
end
function joinpaths(...)
local t, n = {...}, select("#", ...)
local r = t[1]
for i = 2, #t do
r = r .. PATH_SEPARATOR .. t[i]
end
return r
end
-- * Main function
function main()
local selected_item = reaper.GetSelectedMediaItem(0, 0)
local active_take = reaper.GetActiveTake(selected_item)
local source = reaper.GetMediaItemTake_Source(active_take)
local source_path = reaper.GetMediaSourceFileName(source)
local source_dir, filename = splitpath(source_path)
local base = splitext(filename)
local cand = {}
-- Intention: root/abc.wav -> root/abc.ust
cand[1] = joinpaths(source_dir, base .. ".ust")
-- Intention: root/abc.wav -> root/ust/abc.ust
cand[2] = joinpaths(source_dir, "ust", base .. ".ust")
-- Intention: root/wav/abc.wav -> root/ust/abc.ust
cand[3] = joinpaths(source_dir, "..", "ust", base .. ".ust")
local ret
-- ret = reaper.file_exists(source_dir .. cdr)
for _, path in next, cand do
if reaper.file_exists(path) then
ret = path
break
end
end
local OS = reaper.GetOS()
local command
if OS:match("^Win") then
command = "start"
elseif OS:match("^macOS") or OS:match("^OSX") then
command = "open"
elseif OS:match("^Other") then
command = "xdg-open"
end
if ret then
os.execute(command .. " " .. '"' .. ret .. '"')
end
end
-- * Call it
main()
-- Local Variables:
-- eval: (if (commandp #'outshine-mode) (outshine-mode) (outline-minor-mode))
-- End:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment