Skip to content

Instantly share code, notes, and snippets.

@Sneakpeakcss
Last active November 21, 2023 15:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sneakpeakcss/05a97d509b8be67a6f11400b0bee54ab to your computer and use it in GitHub Desktop.
Save Sneakpeakcss/05a97d509b8be67a6f11400b0bee54ab to your computer and use it in GitHub Desktop.
mpv player script to open directory and select currently playing file (Windows).
-- open-in-explorer.lua
mp.add_key_binding(nil, "open-in-explorer", function()
local path = mp.get_property("path")
if path ~= nil and not path:match("^%a[%a%d-_]+://") then
path = string.gsub(path, "/", "\\")
mp.command_native({
name = "subprocess",
playback_only = false,
args = { 'explorer', '/select,', path .. ' ' },
})
else
mp.osd_message("Invalid path: " .. (path or "No path provided"))
end
end)
@Sneakpeakcss
Copy link
Author

Sneakpeakcss commented Nov 19, 2023

input.conf

e  script-binding open_in_explorer/open-in-explorer

 


Here's a rudimentary PowerShell version, mostly for reference if someone wants to tinker with adding some additional actions through it:

local function open_in_explorer()
    local path = mp.get_property("path")
    if path ~= nil then
        local ps_code = [[
            explorer.exe /select,"__path__"
        ]]
        local path = string.gsub(path, "[%(%)%.%+%-%*%?%[%]%^%$%%]", "%%%1")
        local ps_path = string.gsub(path, "([$`])", "`%1")      -- escape dollar/backtick signs for PowerShell
        local ps_code = string.gsub(ps_code, "__path__", ps_path)
        mp.command_native({
            name = "subprocess",
            playback_only = false,
            args = { 'powershell', '-NoProfile', '-Command', ps_code },
        })
    end
end

mp.add_key_binding(nil, "open-in-explorer", open_in_explorer)

Otherwise, for its original purpose it's preferable to use the main version since it's faster than PS:

image

@po5
Copy link

po5 commented Nov 20, 2023

uosc has a cross-platform solution with its show-in-directory script binding.

@Sneakpeakcss
Copy link
Author

Sneakpeakcss commented Nov 20, 2023

uosc has a cross-platform solution with its show-in-directory script binding.

Well, now that's slightly amusing. This whole thing came to be because of an issue with PowerShell version in mpv.net, but for some reason it didn't hit me to check if uosc didn't already have this. I guess this is still useful as a standalone solution for people that don't use uosc.

Though going through my weirdly named test files, uosc has an issue with paths that have a comma in them:

C:\test\test2\aa,aa.mp4 this will open "This PC" directory
C:\test\test2\,aaaa.mp4 this will open "test" directory and select test2 folder

Everything starts working fine the moment there's a space anywhere in the path.

[edit]
Wait… This one also has the same issue, i'm quite sure that i've used the same files testing this thing previously, but apparently not? PowerShell version also is broken in those 2 cases.

C:\test\test2\..,aaaa.mp4 uh huh… Amazing way of navigating directories

@Sneakpeakcss
Copy link
Author

Sneakpeakcss commented Nov 20, 2023

Here's the problem in more detail: mpvnet-player/mpv.net#580 (comment)

(...)
This seems to work:

mp.add_key_binding(nil, "open-in-explorer", function()
    local path = mp.get_property("path")
    if path ~= nil and not path:match("^%a[%a%d-_]+://") then
        local ps_code = [[
            Start-Process -FilePath explorer.exe -ArgumentList "/select,`"__path__`""
        ]]
        local path = string.gsub(path, "/", "\\")
        local path = string.gsub(path, "[%(%)%.%+%-%*%?%[%]%^%$%%]", "%%%1")
        local ps_path = string.gsub(path, "([$`])", "`%1")      -- escape dollar/backtick signs for PowerShell
        local ps_code = string.gsub(ps_code, "__path__", ps_path)
        mp.command_native({
            name = "subprocess",
            playback_only = false,
            args = { 'powershell', '-NoProfile', '-Command', ps_code },
        })
    else
        mp.osd_message("Invalid path: " .. (path or "No path provided"))
    end
end)

beatingRecords

We're breaking records… At this point this seems like a case of "just Windows / mpv things" and pick your poison. It would be far more preferable to get mpv to pass it with a single set of double quotes through cmd in all cases, but i don't think it's possible.

@Sneakpeakcss
Copy link
Author

Sneakpeakcss commented Nov 20, 2023

Okay, main version seems to now work in every single case, so there's again no need to use the above PowerShell solution.

@po5
Copy link

po5 commented Nov 21, 2023

Was the fix just appending a space to the path? Please submit a PR with uosc since you have a setup to reproduce this. 🙏

@Sneakpeakcss
Copy link
Author

Yea, a trailing space was all it takes to fix this. I've submited the mentioned PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment