Skip to content

Show in Directory does not work for certain file-names. #1069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
AziRizvi opened this issue Mar 13, 2025 · 3 comments
Open

Show in Directory does not work for certain file-names. #1069

AziRizvi opened this issue Mar 13, 2025 · 3 comments

Comments

@AziRizvi
Copy link

MPV Version: v0.38.0-743-gad7976c3
Script Version: 5.7.0
Platform: Windows 10 22H2

I'm using this in my input.conf
MBTN_MID_DBL script-binding uosc/show-in-directory

When I initiate a double middle-click it opens This PC instead of opening the directory where the file is located.

Folder Name: Even Given the Worthless Appraiser Class, I'm Actually the Strongest
File Name: [ToonsHub] Even Given the Worthless Appraiser Class Im Actually the Strongest S01E07 1080p B-Global WEB-DL AAC2.0 H.264 (Fuguushoku [Kanteishi] ga Jitsu wa Saikyou Datta - Naraku de Kitaeta Saikyou no [Shingan] de Musou Suru, Multi-Subs)

Directory Path of the File:
K:\Media\Anime\Even Given the Worthless Appraiser Class, I'm Actually the Strongest\[ToonsHub] Even Given the Worthless Appraiser Class Im Actually the Strongest S01E07 1080p B-Global WEB-DL AAC2.0 H.264 (Fuguushoku [Kanteishi] ga Jitsu wa Saikyou Datta - Naraku de Kitaeta Saikyou no [Shingan] de Musou Suru, Multi-Subs).mkv

The only reason why I can think it might not be working is because perhaps the filename is too long.

@tomasklaen
Copy link
Owner

It can be the length, or escaping. Using these windows utilities is generally PITA. There was already a PR that tried to help with opening paths with commas in them (#800). But maybe there are issues with other characters too.

The snippet that handles this is here:

uosc/src/uosc/main.lua

Lines 1031 to 1047 in 0b65802

bind_command('show-in-directory', function()
-- Ignore URLs
if not state.path or is_protocol(state.path) then return end
if state.platform == 'windows' then
utils.subprocess_detached({args = {'explorer', '/select,', state.path .. ' '}, cancellable = false})
elseif state.platform == 'darwin' then
utils.subprocess_detached({args = {'open', '-R', state.path}, cancellable = false})
elseif state.platform == 'linux' then
local result = utils.subprocess({args = {'nautilus', state.path}, cancellable = false})
-- Fallback opens the folder with xdg-open instead
if result.status ~= 0 then
utils.subprocess({args = {'xdg-open', serialize_path(state.path).dirname}, cancellable = false})
end
end
end)

You can try playing around with it.

@Sneakpeakcss
Copy link
Contributor

Even with long paths enabled explorer.exe won't even touch paths above the 260 character limit.

From what i know, your only option here is to either move the files to C:\ drive, where those paths are displayed in 8.3 format (and in effect breaking every possible title display), or rename those files below the limit, because at this point you even have issues with the default mpv functionality, like drag and drop over the window not working.

@Sneakpeakcss
Copy link
Contributor

This works:

local ffi = require("ffi")

ffi.cdef[[
typedef long HRESULT;
typedef unsigned long DWORD;
typedef struct _ITEMIDLIST {
    unsigned short cb;
    unsigned char abID[1];
} ITEMIDLIST, *LPITEMIDLIST, *LPCITEMIDLIST;

HRESULT SHOpenFolderAndSelectItems(LPCITEMIDLIST pidlFolder, unsigned int cidl, LPCITEMIDLIST* apidl, DWORD dwFlags);
HRESULT SHParseDisplayName(const wchar_t* pszName, void* pbc, LPITEMIDLIST* ppidl, DWORD sfgaoIn, DWORD* psfgaoOut);
void CoTaskMemFree(void* pv);
int MultiByteToWideChar(unsigned int CodePage, DWORD dwFlags, const char* lpMultiByteStr, int cbMultiByte, wchar_t* lpWideCharStr, int cchWideChar);
]]

local CP_UTF8 = 65001
local shell = ffi.load("Shell32.dll")
local ole32 = ffi.load("Ole32.dll")

local function open_folder_and_select_item(utf8_path)
    local utf16_len = ffi.C.MultiByteToWideChar(CP_UTF8, 0, utf8_path, -1, nil, 0)
    if utf16_len == 0 then
        return
    end

    local utf16_path = ffi.new("wchar_t[?]", utf16_len)
    if ffi.C.MultiByteToWideChar(CP_UTF8, 0, utf8_path, -1, utf16_path, utf16_len) == 0 then
        return
    end

    local pidl = ffi.new("LPITEMIDLIST[1]")
    if shell.SHParseDisplayName(utf16_path, nil, pidl, 0, nil) ~= 0 or pidl[0] == nil then
        return
    end

    shell.SHOpenFolderAndSelectItems(pidl[0], 0, nil, 0)
    ole32.CoTaskMemFree(pidl[0])
end


mp.add_key_binding(nil, "open-in-explorer", function()
    local path = mp.get_property("path")

    if path then
        -- mpv adds '\\?\' prefix when long paths aren't enabled in Windows
        if path:sub(1, 4) == "\\\\?\\" then
            path = path:sub(5)
        end

        if path:match("^%a:[/\\]") and not path:match("^%a[%a%d-_]+://") then
            open_folder_and_select_item(path:gsub("/", "\\"))
        else
            mp.osd_message("Invalid path: " .. path)
        end
    end
end)

script-binding open-in-explorer

On top of that, if it's still open it will focus the same window rather than always opening a new one like the explorer /select solution. However, this has a slight delay if it's selecting a different file in the same folder.
 

Surely all Windows builds use LuaJIT :^)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants