Skip to content

Instantly share code, notes, and snippets.

@Sneakpeakcss
Last active August 8, 2025 06:14
Show Gist options
  • Select an option

  • Save Sneakpeakcss/05a97d509b8be67a6f11400b0bee54ab to your computer and use it in GitHub Desktop.

Select an option

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 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