forked from nimatrueway/mpv-locatefile-lua-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocatefile.lua
72 lines (66 loc) · 2.19 KB
/
locatefile.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
--// Extract file dir from url
function GetFileName(url)
return url:match("^.+/([^/]+)$")
end
function GetDirectory(url)
return url:match("^(.+)/[^/]+$")
end
-- for ubuntu
-- file_browser_linux_cmd = "xdg-open \"$dir\""
file_browser_linux_cmd = "dbus-send --print-reply --dest=org.freedesktop.FileManager1 /org/freedesktop/FileManager1 org.freedesktop.FileManager1.ShowItems array:string:\"file:$path\" string:\"\""
-- for macos
file_browser_macos_cmd_osascript_content = 'tell application "Finder"' .. '\n' .. 'set frontmost to true' .. '\n' .. 'reveal (POSIX file "$path")' .. '\n' .. 'end tell' .. '\n'
file_browser_macos_cmd = 'osascript "$osascript_file"'
-- for windows
file_browser_windows_cmd = "explorer /select,\"$path\""
--// check if macos
function is_macos()
local homedir = os.getenv("HOME")
if homedir ~= nil and string.sub(homedir,1,6) == "/Users" then
return true
else
return false
end
end
--// check if windows
function is_windows()
local windir = os.getenv("windir")
if windir~=nil then
return true
else
return false
end
end
--// create temporary script
function create_temp_file(content)
local tmp_filename = os.tmpname()
local tmp_file = io.open(tmp_filename, "wb")
tmp_file:write(content)
io.close(tmp_file)
return tmp_filename
end
--// create temporary script
function GetDirectory(url)
return url:match("^(.*)/[^/]+$")
end
--// handle "locate-current-file" function triggered by a key in "input.conf"
mp.register_script_message("locate-current-file", function()
file_browser_cmd = file_browser_linux_cmd
local filepath = mp.get_property("path")
local filedir = GetDirectory(filepath)
if is_windows() then
file_browser_cmd = file_browser_windows_cmd
filepath = filepath:gsub("/", "\\")
elseif is_macos() then
local content = file_browser_macos_cmd_osascript_content:gsub("$path", filepath)
file_browser_cmd = file_browser_macos_cmd:gsub("$osascript_file", create_temp_file(content))
else
file_browser_cmd = file_browser_linux_cmd
end
if filepath ~= nil then
cmd = file_browser_cmd:gsub("$path", filepath)
cmd = cmd:gsub("$dir", filedir)
mp.osd_message('Browse \n' .. filepath)
os.execute(cmd)
end
end)