diff --git a/frontend/apps/filemanager/filemanagerutil.lua b/frontend/apps/filemanager/filemanagerutil.lua index b9cfc67fb..6d694f01f 100644 --- a/frontend/apps/filemanager/filemanagerutil.lua +++ b/frontend/apps/filemanager/filemanagerutil.lua @@ -269,43 +269,49 @@ end -- Generate "Execute script" file dialog button function filemanagerutil.genExecuteScriptButton(file, caller_callback) - local InfoMessage = require("ui/widget/infomessage") return { -- @translators This is the script's programming language (e.g., shell or python) text = T(_("Execute %1 script"), util.getScriptType(file)), callback = function() - caller_callback() - local script_is_running_msg = InfoMessage:new{ - -- @translators %1 is the script's programming language (e.g., shell or python), %2 is the filename - text = T(_("Running %1 script %2…"), util.getScriptType(file), BD.filename(ffiUtil.basename(file))), - } - UIManager:show(script_is_running_msg) - UIManager:scheduleIn(0.5, function() - local rv - if Device:isAndroid() then - Device:setIgnoreInput(true) - rv = os.execute("sh " .. ffiUtil.realpath(file)) -- run by sh, because sdcard has no execute permissions - Device:setIgnoreInput(false) - else - rv = os.execute(ffiUtil.realpath(file)) - end - UIManager:close(script_is_running_msg) - if rv == 0 then - UIManager:show(InfoMessage:new{ - text = _("The script exited successfully."), - }) - else - --- @note: Lua 5.1 returns the raw return value from the os's system call. Counteract this madness. - UIManager:show(InfoMessage:new{ - text = T(_("The script returned a non-zero status code: %1!"), bit.rshift(rv, 8)), - icon = "notice-warning", - }) - end - end) + filemanagerutil.executeScript(file, caller_callback) end, } end +function filemanagerutil.executeScript(file, caller_callback) + if caller_callback then + caller_callback() + end + local InfoMessage = require("ui/widget/infomessage") + local script_is_running_msg = InfoMessage:new{ + -- @translators %1 is the script's programming language (e.g., shell or python), %2 is the filename + text = T(_("Running %1 script %2…"), util.getScriptType(file), BD.filename(ffiUtil.basename(file))), + } + UIManager:show(script_is_running_msg) + UIManager:scheduleIn(0.5, function() + local rv + if Device:isAndroid() then + Device:setIgnoreInput(true) + rv = os.execute("sh " .. ffiUtil.realpath(file)) -- run by sh, because sdcard has no execute permissions + Device:setIgnoreInput(false) + else + rv = os.execute(ffiUtil.realpath(file)) + end + UIManager:close(script_is_running_msg) + if rv == 0 then + UIManager:show(InfoMessage:new{ + text = _("The script exited successfully."), + }) + else + --- @note: Lua 5.1 returns the raw return value from the os's system call. Counteract this madness. + UIManager:show(InfoMessage:new{ + text = T(_("The script returned a non-zero status code: %1!"), bit.rshift(rv, 8)), + icon = "notice-warning", + }) + end + end) +end + function filemanagerutil.showChooseDialog(title_header, caller_callback, current_path, default_path, file_filter) local is_file = file_filter and true or false local path = current_path or default_path diff --git a/plugins/bookshortcuts.koplugin/main.lua b/plugins/bookshortcuts.koplugin/main.lua index bf14d49d7..dac4a1f2e 100644 --- a/plugins/bookshortcuts.koplugin/main.lua +++ b/plugins/bookshortcuts.koplugin/main.lua @@ -1,5 +1,6 @@ local ConfirmBox = require("ui/widget/confirmbox") local DataStorage = require("datastorage") +local Device = require("device") local Dispatcher = require("dispatcher") local LuaSettings = require("luasettings") local PathChooser = require("ui/widget/pathchooser") @@ -47,8 +48,13 @@ function BookShortcuts:onBookShortcut(path) file = path end if file then - local FileManager = require("apps/filemanager/filemanager") - FileManager.openFile(self.ui, file) + if Device:canExecuteScript(file) then + local filemanagerutil = require("apps/filemanager/filemanagerutil") + filemanagerutil.executeScript(file) + else + local FileManager = require("apps/filemanager/filemanager") + FileManager.openFile(self.ui, file) + end end end end