Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local mp = require 'mp'
- local utils = require 'mp.utils'
- function detect_platform()
- local o = {}
- -- Kind of a dumb way of detecting the platform but whatever
- if mp.get_property_native('options/vo-mmcss-profile', o) ~= o then
- return 'windows'
- elseif mp.get_property_native('options/input-app-events', o) ~= o then
- return 'macos'
- end
- return 'linux'
- end
- local platform = detect_platform()
- clipboard_module = {}
- function clipboard_module.set_clipboard(content)
- if platform == 'linux' then
- local res = utils.subprocess({ args = {
- 'echo', content .. ' | xclip -selection c'
- } })
- elseif platform == 'windows' then
- local res = utils.subprocess({ args = {
- 'powershell', '-NoProfile', '-Command', 'Set-Clipboard ' .. content
- } })
- elseif platform == 'macos' then
- local res = utils.subprocess({ args = { 'echo', content .. ' | pbcopy' } })
- end
- end
- function clipboard_module.get_clipboard(clip)
- if platform == 'linux' then
- local res = utils.subprocess({ args = {
- 'xclip', '-selection', clip and 'clipboard' or 'primary', '-out'
- } })
- if not res.error then
- return res.stdout
- else
- return 'error'
- end
- elseif platform == 'windows' then
- local res = utils.subprocess({ 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)
- }]]
- } })
- if not res.error then
- return res.stdout
- else
- return 'error'
- end
- elseif platform == 'macos' then
- local res = utils.subprocess({ args = { 'pbpaste' } })
- if not res.error then
- return res.stdout
- else
- return 'error'
- end
- end
- return ''
- end
- return clipboard_module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement