Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Put the script `copyTime.lua` in your scripts folder, usually in:
To work, the script needs:
* Windows: `Powershell`.
* Linux/X11: `xclip`.
* Linux/Wayland : `xclip` or `wl-clipboard`.
* Linux/Wayland : `wl-clipboard`.
* MacOS: `pbcopy` (not tested).

# Screenshot
Expand Down
23 changes: 20 additions & 3 deletions copyTime.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,30 @@ local function command_exists(cmd)
local pipe = io.popen("type " .. cmd .. " > /dev/null 2> /dev/null; printf \"$?\"", "r")
exists = pipe:read() == "0"
pipe:close()

-- show error if command not exists
if not exists and cmd == "pbcopy" then
mp.msg.error(cmd .. " package not found! please install it (MacOS-only).")
elseif not exists then
mp.msg.error(cmd .. " package not found! please install it.")
end

return exists
end

-- reference for io.popen:
-- https://pubs.opengroup.org/onlinepubs/009695399/functions/popen.html
local function display_servers(cmd)
local pipe = io.popen("echo $XDG_SESSION_TYPE", "r")
exists = pipe:read() == cmd
pipe:close()
return exists
end

local function get_clipboard_cmd()
if command_exists("xclip") then
if display_servers("x11") and command_exists("xclip") then
return "xclip -silent -in -selection clipboard"
elseif command_exists("wl-copy") then
elseif display_servers("wayland") and command_exists("wl-copy") then
return "wl-copy"
elseif command_exists("pbcopy") then
return "pbcopy"
Expand All @@ -40,7 +57,7 @@ local function divmod(a, b)
return a / b, a % b
end

local function set_clipboard(text)
local function set_clipboard(text)
if platform == WINDOWS then
mp.commandv("run", "powershell", "set-clipboard", text)
return true
Expand Down