summary refs log tree commit diff
path: root/dot_config/mpv/scripts/appendURL.lua
diff options
context:
space:
mode:
authorAlen <alen@dotfiles.xyz>2025-01-19 19:06:44 +0400
committerAlen <alen@dotfiles.xyz>2025-01-19 19:06:44 +0400
commit94ad728f49ceb99ab9c75a8ac5edbfcd708066dd (patch)
tree038a430cbe4bf7223d661fb60a3408f0b56828f1 /dot_config/mpv/scripts/appendURL.lua
parentf73915ba4fd151b6fc2e4a74b8c54cc7b6e23042 (diff)
Add mpv config
Diffstat (limited to 'dot_config/mpv/scripts/appendURL.lua')
-rw-r--r--dot_config/mpv/scripts/appendURL.lua81
1 files changed, 81 insertions, 0 deletions
diff --git a/dot_config/mpv/scripts/appendURL.lua b/dot_config/mpv/scripts/appendURL.lua
new file mode 100644
index 0000000..ebb69f9
--- /dev/null
+++ b/dot_config/mpv/scripts/appendURL.lua
@@ -0,0 +1,81 @@
+-- appendurl - Tsubajashi
+
+local platform = nil --set to 'linux', 'windows' or 'macos' to override automatic assign
+
+if not platform then
+  local o = {}
+  if mp.get_property_native('options/vo-mmcss-profile', o) ~= o then
+    platform = 'windows'
+  elseif mp.get_property_native('options/input-app-events', o) ~= o then
+    platform = 'macos'
+  else
+    platform = 'linux'
+  end
+end
+
+local utils = require 'mp.utils'
+local msg = require 'mp.msg'
+
+--main function
+function append(primaryselect)
+  local clipboard = get_clipboard(primaryselect or false)
+  if clipboard then
+    mp.commandv("loadfile", clipboard, "append-play")
+    mp.osd_message("URL appended: "..clipboard)
+    msg.info("URL appended: "..clipboard)
+  end
+end
+
+--handles the subprocess response table and return clipboard if it was a success
+function handleres(res, args, primary)
+  if not res.error and res.status == 0 then
+      return res.stdout
+  else
+    --if clipboard failed try primary selection
+    if platform=='linux' and not primary then
+      append(true)
+      return nil
+    end
+    msg.error("There was an error getting "..platform.." clipboard: ")
+    msg.error("  Status: "..(res.status or ""))
+    msg.error("  Error: "..(res.error or ""))
+    msg.error("  stdout: "..(res.stdout or ""))
+    msg.error("args: "..utils.to_string(args))
+    return nil
+  end
+end
+
+function get_clipboard(primary) 
+  if platform == 'linux' then
+    local args = { 'xclip', '-selection', primary and 'primary' or 'clipboard', '-out' }
+    return handleres(utils.subprocess({ args = args }), args, primary)
+  elseif platform == 'windows' then
+    local args = {
+      'powershell', '-NoProfile', '-Command', [[& {
+        Trap {
+          Write-Error -ErrorRecord $_
+          Exit 1
+        }
+
+        $clip = ""
+        if (Get-Command "Get-Clipboard" -errorAction SilentlyContinue) {
+          $clip = Get-Clipboard -Raw -Format Text -TextFormatType UnicodeText
+        } else {
+          Add-Type -AssemblyName PresentationCore
+          $clip = [Windows.Clipboard]::GetText()
+        }
+
+        $clip = $clip -Replace "`r",""
+        $u8clip = [System.Text.Encoding]::UTF8.GetBytes($clip)
+        [Console]::OpenStandardOutput().Write($u8clip, 0, $u8clip.Length)
+      }]]
+    }
+    return handleres(utils.subprocess({ args =  args }), args)
+  elseif platform == 'macos' then
+    local args = { 'pbpaste' }
+    return handleres(utils.subprocess({ args = args }), args)
+  end
+  return nil
+end
+
+mp.add_key_binding("ctrl+v", "appendURL", append)
\ No newline at end of file