forked from ReaTeam/ReaScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLokasenna_Create action to open a file... .lua
77 lines (59 loc) · 2.61 KB
/
Lokasenna_Create action to open a file... .lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
--[[
Description: Create action to open a file...
Version: 1.3.0
Author: Lokasenna
Donation: https://paypal.me/Lokasenna
Changelog:
Add: Linux compatibility (uses xdg-open)
Links:
Forum Thread http://forum.cockos.com/showthread.php?t=189152
Lokasenna's Website http://forum.cockos.com/member.php?u=10417
About:
Automates the creation of ReaScripts that will open files or folders
in their native app. Handy for shortcuts to plugin manuals, or commonly
used folders, etc.
Note: Due to Reaper limitations, creating a shortcut to a folder requires you
to select a file IN that folder, and then manually erase the filename
from the path. Sorry. :/
--]]
-- Licensed under the GNU GPL v3
local function osOpenCommand()
local commands = {
{os = "Win", cmd = 'start ""'},
{os = "OSX", cmd = 'open ""'},
{os = "Other", cmd = 'xdg-open'},
}
local OS = reaper.GetOS()
for _, v in ipairs(commands) do
if OS:match(v.os) then return v.cmd end
end
end
local ret, path, csv
reaper.ShowMessageBox("To create an action that opens a folder:\n\n1. Use the next window to select a file in that folder.\n2. Click 'OK'.\n3. Another window will pop up; manually erase the filename there.\n\nThis is a Reaper limitation - sorry for the inconvenience.", "Create action to open a file...", 0)
ret, path = reaper.GetUserFileNameForRead("", "Select a file", "")
if not ret then return 0 end
-- Cheers to @mpl for this.
local num = path:reverse():find('[%/%\\]')
local alias = path:sub(-num + 1) .. " "
ret, csv = reaper.GetUserInputs("Create action to open a file... ", 2, "File/folder path:,File alias:,extrawidth=128", path..","..alias)
if not ret or csv == "" then return 0 end
path, alias = string.match(csv, "([^,]+),([^,]*)")
if not path then return 0 end
local str = "-- Created with Lokasenna_Create action to open a file... .lua\n"
.. [[os.execute(']] .. osOpenCommand() .. [[ "]] .. path .. [["')]]
str = string.gsub(str, [[\]], [[\\]])
local info = debug.getinfo(1,'S');
local script_path = info.source:match[[^@?(.*[\/])[^\/]-$]]
local file_name = script_path .. "Open a file - " .. alias .. ".lua"
local file, err = io.open( file_name , "w+")
if not file then
reaper.ShowMessageBox("Couldn't create file:\n" .. file_name .. "\n\nError: " .. tostring(err), "Whoops", 0)
return 0
end
file:write(str)
reaper.ShowMessageBox( "Successfully created file:\n"
.. ( string.len(file_name) > 64 and ( "..." .. string.sub(file_name, -56) )
or file_name),
"Yay!", 0)
io.close(file)
reaper.AddRemoveReaScript( true, 0, file_name, true )