Skip to content

Commit b56c1c8

Browse files
authored
update removing to move file to trash
1 parent a0cb595 commit b56c1c8

File tree

1 file changed

+32
-20
lines changed

1 file changed

+32
-20
lines changed

trashfileonend.lua

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@ local msg = require 'mp.msg'
88
local utils = require 'mp.utils'
99
local settings = {
1010
--all settings values need to be true or false
11-
--deletefile and deleteoneonly in this variable is the default behaviour, both will change when using toggle or message
11+
--deletefile and deleteoneonly in this variable is the default behaviour of the script, both will change when using toggle or message
12+
1213
--linux/windows/auto(empty string)
1314
system = "",
15+
1416
--activate file removing, default is good to keep as false
1517
deletefile = false,
18+
1619
--remove only one file(next closed file), changes deletefile to false after deleting one
1720
deleteoneonly = true,
21+
1822
--display osd messages for toggles
1923
osd_message = true,
24+
2025
--https://mpv.io/manual/stable/#lua-scripting-end-file
2126
--accepted EOF reasons to delete a file, change to false to disallow file deletion.
2227
--if a eof reason is not allowed and deleteoneonly is true it will trigger without deleting the file
@@ -41,10 +46,6 @@ if settings.system=="" then
4146
else
4247
linux = true
4348
end
44-
elseif settings.system:lower()=="linux" then
45-
linux = true
46-
else
47-
linux = false
4849
end
4950

5051
--run when any file is opened
@@ -55,8 +56,6 @@ function on_load()
5556
if p:match("^%a%a+:%/%/") then path = nil ; return end
5657
--get always absolute path to file
5758
path = utils.join_path(utils.getcwd(), p)
58-
--convert slashes to backslashes for windows
59-
if linux==false then path = path:gsub("/", "\\") end
6059
end
6160

6261
--run when any file is closed
@@ -67,20 +66,13 @@ function on_close(reason)
6766
output(true)
6867
end
6968
if settings.accepted_reasons[reason.reason] then
70-
local rm = 'rm'
71-
if not linux then rm = 'del' end
72-
local response = utils.subprocess({ cancellable=false, args = { rm, path } })
73-
if response.error == nil and response.status == 0 then
69+
local remove_function = linux and os.remove or removefile_windows
70+
local success, error = remove_function(path)
71+
if success then
7472
msg.info('File removed: '..path)
7573
else
76-
if response.error == nil then response.error = "" end
77-
msg.error("There was an error deleting the file: ")
78-
msg.error(" Status: "..response.status)
79-
msg.error(" Error: "..response.error)
80-
msg.error(" stdout: "..response.stdout)
81-
msg.error("Possible errors are permissions or failure in locating file")
82-
msg.error("The command that produced the error was the following:")
83-
msg.error(" "..rm.." "..path)
74+
msg.error("There was an error deleting the file: "..path)
75+
if error then msg.error(" Error: "..error) end
8476
end
8577
else
8678
msg.info('Unallowed EOF: '..reason.reason)
@@ -118,6 +110,27 @@ function outputhelper(string, silent)
118110
if settings.osd_message and not silent then mp.osd_message(string) end
119111
end
120112

113+
-- windows cannot use os.remove with unicode characters
114+
function removefile_windows(file)
115+
local args = {
116+
'powershell', '-NoProfile', '-Command', [[& {
117+
Trap {
118+
Write-Error -ErrorRecord $_
119+
Exit 1
120+
}
121+
Add-Type -AssemblyName Microsoft.VisualBasic
122+
[Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile("]]..file..[[",'OnlyErrorDialogs','SendToRecycleBin')
123+
}]]
124+
}
125+
local response = utils.subprocess({ args = args, cancellable = false })
126+
if response.error == nil and response.status == 0 then
127+
return true, nil
128+
else
129+
if response.error == nil then response.error = "Unexpected error" end
130+
return nil, response.error
131+
end
132+
end
133+
121134
--read settings from a script message
122135
function trashsend(delete, single)
123136
settings.deletefile = ( delete:lower() == 'true' )
@@ -129,4 +142,3 @@ mp.register_script_message("trashfileonend", trashsend)
129142
mp.add_key_binding("ctrl+alt+x", "toggledeletefile", toggledelete)
130143
mp.register_event('file-loaded', on_load)
131144
mp.register_event('end-file', on_close)
132-

0 commit comments

Comments
 (0)