@@ -8,15 +8,20 @@ local msg = require 'mp.msg'
8
8
local utils = require ' mp.utils'
9
9
local settings = {
10
10
-- 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
+
12
13
-- linux/windows/auto(empty string)
13
14
system = " " ,
15
+
14
16
-- activate file removing, default is good to keep as false
15
17
deletefile = false ,
18
+
16
19
-- remove only one file(next closed file), changes deletefile to false after deleting one
17
20
deleteoneonly = true ,
21
+
18
22
-- display osd messages for toggles
19
23
osd_message = true ,
24
+
20
25
-- https://mpv.io/manual/stable/#lua-scripting-end-file
21
26
-- accepted EOF reasons to delete a file, change to false to disallow file deletion.
22
27
-- 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
41
46
else
42
47
linux = true
43
48
end
44
- elseif settings .system :lower ()== " linux" then
45
- linux = true
46
- else
47
- linux = false
48
49
end
49
50
50
51
-- run when any file is opened
@@ -55,8 +56,6 @@ function on_load()
55
56
if p :match (" ^%a%a+:%/%/" ) then path = nil ; return end
56
57
-- get always absolute path to file
57
58
path = utils .join_path (utils .getcwd (), p )
58
- -- convert slashes to backslashes for windows
59
- if linux == false then path = path :gsub (" /" , " \\ " ) end
60
59
end
61
60
62
61
-- run when any file is closed
@@ -67,20 +66,13 @@ function on_close(reason)
67
66
output (true )
68
67
end
69
68
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
74
72
msg .info (' File removed: ' .. path )
75
73
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
84
76
end
85
77
else
86
78
msg .info (' Unallowed EOF: ' .. reason .reason )
@@ -118,6 +110,27 @@ function outputhelper(string, silent)
118
110
if settings .osd_message and not silent then mp .osd_message (string ) end
119
111
end
120
112
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
+
121
134
-- read settings from a script message
122
135
function trashsend (delete , single )
123
136
settings .deletefile = ( delete :lower () == ' true' )
@@ -129,4 +142,3 @@ mp.register_script_message("trashfileonend", trashsend)
129
142
mp .add_key_binding (" ctrl+alt+x" , " toggledeletefile" , toggledelete )
130
143
mp .register_event (' file-loaded' , on_load )
131
144
mp .register_event (' end-file' , on_close )
132
-
0 commit comments