Files
lua-scripts-w-api/lib/dtutils/system.lua
theres 3675f4d7cd video ffmpeg - post review fixes
- rename OS_PATH_SEPARATOR -> PS and use in all needed places
- change function name from `open_file` to `launch_default_app`
- move `mkdir`, `rm` and `launch_default_app` helper functions to libs
- add executable_path_widget to export module if needed, not to the
preferences
- add show_status
- remove support_format
- change function name `format` to `format_string`, add comments
- replace apostrophes by quote marks
2019-01-27 22:43:47 +01:00

134 lines
4.7 KiB
Lua

local dtutils_system = {}
local dt = require "darktable"
require "official/yield" -- necessary for dt.control.execute()
dtutils_system.libdoc = {
Name = [[dtutils.system]],
Synopsis = [[a library of system utilities for use in darktable lua scripts]],
Usage = [[local ds = require "lib/dtutils.system"]],
Description = [[This library contains routines for interfacing to the operating system from
darktable lua scripts.]],
Return_Value = [[du - library - the darktable lua system library]],
Limitations = [[]],
Example = [[]],
See_Also = [[]],
Reference = [[]],
License = [[This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.]],
Copyright = [[Copyright (c) 2018 Bill Ferguson <wpferguson@gmail.com>]],
functions = {}
}
dtutils_system.libdoc.functions["external_command"] = {
Name = [[external_command]],
Synopsis = [[pass a command to the operating system for execution and return the result]],
Usage = [[local dsys = require "lib/dtutils.system"
local result = dsys.external_command(command)
command - string - a string containing the command and arguments to be passed to the operating system for execution.]],
Description = [[external_command passes a command to the operating system for execution and returns the results.]],
Return_Value = [[result - the return value signalling success or failure.]],
Limitations = [[]],
Example = [[]],
See_Also = [[]],
Reference = [[]],
License = [[]],
Copyright = [[]],
}
function dtutils_system.external_command(command)
local result = nil
if dt.configuration.running_os == "windows" then
result = dtutils_system.windows_command(command)
else
result = dt.control.execute(command)
end
return result
end
dtutils_system.libdoc.functions["windows_command"] = {
Name = [[windows_command]],
Synopsis = [[pass a command to the windows operating system for execution and return the result]],
Usage = [[local dsys = require "lib/dtutils.system"
local result = dsys.windows_command(command)
command - string - a string containing the command and arguments to be passed to the operating system for execution.]],
Description = [[The normal method of executing an operating system command is using dt.control.execute(), but that doesn't
work with Windows when more than one item in the command is quoted. In order to ensure command execution on Windows we
create a batch file in the temporary directory, put the command in it, execute the batch file, then return the result.]],
Return_Value = [[result - the return value signalling success or failure.]],
Limitations = [[]],
Example = [[]],
See_Also = [[]],
Reference = [[]],
License = [[]],
Copyright = [[]],
}
function dtutils_system.windows_command(command)
local result = 1
local fname = dt.configuration.tmp_dir .. "/run_command.bat"
local file = io.open(fname, "w")
if file then
dt.print_log("opened file")
command = string.gsub(command, "%%", "%%%%") -- escape % from windows shell
file:write(command)
file:close()
result = dt.control.execute(fname)
dt.print_log("result from windows command was " .. result)
os.remove(fname)
else
dt.print_error("Windows command failed: unable to create batch file")
end
return result
end
dtutils_system.libdoc.functions["launch_default_app"] = {
Name = [[launch_default_app]],
Synopsis = [[try to open file in default application]],
Usage = [[local dsys = require "lib/dtutils.file"
dsys.launch_default_app(path)
path - string - a file path]],
Description = [[launch_default_app allow to open file in application that is assigned as default
one in users's system for given filetype
]],
Return_Value = [[path - string - a file path]],
Limitations = [[]],
Example = [[]],
See_Also = [[]],
Reference = [[]],
License = [[]],
Copyright = [[]],
}
function dtutils_system.launch_default_app(path)
local open_cmd = "xdg-open"
if (dt.configuration.running_os == "windows") then
open_cmd = "start"
elseif (dt.configuration.running_os == "macos") then
open_cmd = "open"
end
return dtutils_system.external_command(open_cmd..' "'..path..'"')
end
return dtutils_system