Advertisement
Guest User

Untitled

a guest
May 14th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.08 KB | None | 0 0
  1. local mp = require 'mp'
  2. local utils = require 'mp.utils'
  3.  
  4. function detect_platform()
  5.     local o = {}
  6.     -- Kind of a dumb way of detecting the platform but whatever
  7.     if mp.get_property_native('options/vo-mmcss-profile', o) ~= o then
  8.         return 'windows'
  9.     elseif mp.get_property_native('options/input-app-events', o) ~= o then
  10.         return 'macos'
  11.     end
  12.     return 'linux'
  13. end
  14.  
  15. local platform = detect_platform()
  16.  
  17. clipboard_module = {}
  18.  
  19. function clipboard_module.set_clipboard(content)
  20.     if platform == 'linux' then
  21.         local res = utils.subprocess({ args = {
  22.             'echo', content .. ' | xclip -selection c'
  23.         } })
  24.     elseif platform == 'windows' then
  25.         local res = utils.subprocess({ args = {
  26.             'powershell', '-NoProfile', '-Command', 'Set-Clipboard ' .. content
  27.         } })
  28.     elseif platform == 'macos' then
  29.         local res = utils.subprocess({ args = { 'echo', content .. ' | pbcopy' } })
  30.     end
  31. end
  32.  
  33. function clipboard_module.get_clipboard(clip)
  34.     if platform == 'linux' then
  35.         local res = utils.subprocess({ args = {
  36.             'xclip', '-selection', clip and 'clipboard' or 'primary', '-out'
  37.         } })
  38.         if not res.error then
  39.             return res.stdout
  40.         else
  41.             return 'error'
  42.         end
  43.     elseif platform == 'windows' then
  44.         local res = utils.subprocess({ args = {
  45.             'powershell', '-NoProfile', '-Command', [[& {
  46.                 Trap {
  47.                     Write-Error -ErrorRecord $_
  48.                     Exit 1
  49.                 }
  50.                 $clip = ""
  51.                 if (Get-Command "Get-Clipboard" -errorAction SilentlyContinue) {
  52.                     $clip = Get-Clipboard -Raw -Format Text -TextFormatType UnicodeText
  53.                 } else {
  54.                     Add-Type -AssemblyName PresentationCore
  55.                     $clip = [Windows.Clipboard]::GetText()
  56.                 }
  57.                 $clip = $clip -Replace "`r",""
  58.                 $u8clip = [System.Text.Encoding]::UTF8.GetBytes($clip)
  59.                 [Console]::OpenStandardOutput().Write($u8clip, 0, $u8clip.Length)
  60.             }]]
  61.         } })
  62.         if not res.error then
  63.             return res.stdout
  64.         else
  65.             return 'error'
  66.         end
  67.     elseif platform == 'macos' then
  68.         local res = utils.subprocess({ args = { 'pbpaste' } })
  69.         if not res.error then
  70.             return res.stdout
  71.         else
  72.             return 'error'
  73.         end
  74.     end
  75.     return ''
  76. end
  77.  
  78. return clipboard_module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement