Skip to content

Instantly share code, notes, and snippets.

@steinuil
Last active March 14, 2019 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steinuil/5e1a3592aad77344dbb85cbfb7134b29 to your computer and use it in GitHub Desktop.
Save steinuil/5e1a3592aad77344dbb85cbfb7134b29 to your computer and use it in GitHub Desktop.
local function message(msg)
print(msg)
mp.osd_message(msg)
end
local function get_platform()
local o = {}
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'
else
return 'linux'
end
end
local function tmpname_windows()
return os.getenv 'TEMP' .. os.tmpname() .. 'mpv-livetweet.vbs'
end
local function system(cmd)
local proc = io.popen(cmd)
local body = proc:read('*all')
local ok = proc:close()
return ok, body
end
local function prompt_linux(default_msg)
return system(
'zenity ' ..
'--title mpv-livetweet ' ..
'--text "Tweet body" ' ..
'--entry ' ..
'--entry-text "' .. default_msg .. '"'
)
end
local function prompt_macos(default_msg)
return system(
[[osascript ]] ..
[[-e 'set tweet to text returned of (]] ..
[[display dialog "Tweet body" with title "mpv-livetweet" ]] ..
[[buttons {"Cancel", "Tweet"} ]] ..
[[default button "Tweet" cancel button "Cancel" ]] ..
[[default answer "]] .. default_msg .. [["]] ..
[[)' ]] ..
[[-e 'do shell script "echo " & quoted form of tweet']]
)
end
local function prompt_windows(default_msg)
local script_file = tmpname_windows()
local script =
'body = InputBox("Tweet body", "mpv-livetweet", "' .. default_msg .. '")\r\n' ..
'If IsEmpty(body) Then ' ..
'WScript.Quit(-1) ' ..
'Else ' ..
'WScript.Stdout.Write(body) ' ..
'End If'
local f = io.open(script_file, 'w')
f:write(script)
f:close()
local ok, body = system('cscript /Nologo ' .. script_file)
os.remove(script_file)
return ok, body
end
local function prompt(OS, default_msg)
assert(OS)
local ok, body
if OS == 'linux' then
ok, body = prompt_linux(default_msg)
elseif OS == 'macos' then
ok, body = prompt_macos(default_msg)
elseif OS == 'windows' then
ok, body = prompt_windows(default_msg)
end
if ok then
return body
end
end
mp.add_key_binding('Alt+a', function()
local t = prompt(get_platform(), '#tfw')
if t then message(t) end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment