mpv-player / mpv

🎥 Command line video player
https://mpv.io
Other
28.63k stars 2.92k forks source link

user script: execute .ps1 file #13785

Closed ghost closed 7 months ago

ghost commented 7 months ago

I have this file called ALT+TAB.ps1 which has the following:

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force
$wshell = New-Object -ComObject wscript.shell
$wshell.SendKeys('%{TAB}')

this script sends ALT+TAB keystroke. testing this script alone without mpv - works.

I'm trying to make a user script that will execute this script, here's the script so far (not working):

local function read_time1(_, time_pos)
    if time_pos and time_pos > mp.get_property_native('duration', math.huge) - 15 then
        local url = mp.get_property("path")
        if string.match(url, "youtube") then
            if mp.get_property_number('playlist-count') == 1 then
                mp.command_native({name = 'subprocess',args = { mp.command_native({"expand-path", "~~/ALT+TAB.ps1"})} })
                mp.unobserve_property(read_time1)
            end
        end
    end
end

mp.register_event('file-loaded', function ()
    mp.observe_property('time-pos', 'native', read_time1)
end)

Any ideas?

ghost commented 7 months ago

Solution: Created a ALT+TAB.bat file with:

powershell -c "$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys('%%{TAB}')
Sneakpeakcss commented 7 months ago

You need "powershell -File"

local function read_time1(_, time_pos)
    if time_pos and time_pos > mp.get_property_native('duration', math.huge) - 15 then
        local url = mp.get_property("path")
        if string.match(url, "youtube") then
            if mp.get_property_number('playlist-count') == 1 then
                mp.command_native({name = 'subprocess',args = { 'powershell', '-File', mp.command_native({"expand-path", "~~/ALT+TAB.ps1"})} })
                mp.unobserve_property(read_time1)
            end
        end
    end
end

mp.register_event('file-loaded', function ()
    mp.observe_property('time-pos', 'native', read_time1)
end)

You can also do it directly from lua without any additional files, but sometimes it can get tricky to properly escape everything with more complicated commands:

local function read_time1(_, time_pos)
    if time_pos and time_pos > mp.get_property_native('duration', math.huge) - 15 then
        local url = mp.get_property("path")
        if string.match(url, "youtube") then
            if mp.get_property_number('playlist-count') == 1 then
                mp.command_native({
                    name = 'subprocess',
                    capture_stdout=true,
                    args = { 
                        'powershell', 
                        '-NoProfile',
                        '-Command', 
                        'Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force; $wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys(\'%{TAB}\')' 
                    }
                })
                mp.unobserve_property(read_time1)
            end
        end
    end
end

mp.register_event('file-loaded', function ()
    mp.observe_property('time-pos', 'native', read_time1)
end)
ghost commented 7 months ago

Uh, lovely solution, thank you! Now using:

local function read_time1(_, time_pos)
    if time_pos and time_pos > mp.get_property_native('duration', math.huge) - 15 then
        local url = mp.get_property("path")
        if string.match(url, "youtube") then
            if mp.get_property_number('playlist-count') == 1 then
                mp.command_native({
                    name = 'subprocess',
                    capture_stdout=true,
                    args = { 
                        'powershell', 
                        '-NoProfile',
                        '-Command', 
                        '$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys(\'%{TAB}\')' 
                    }
                })
                mp.unobserve_property(read_time1)
            end
        end
    end
end

mp.register_event('file-loaded', function ()
    mp.observe_property('time-pos', 'native', read_time1)
end)