|
| 1 | +local utils = require 'mp.utils' |
| 2 | + |
| 3 | +local MULTIMEDIA = utils.format_json({ |
| 4 | + 'AAC', |
| 5 | + 'AVI', |
| 6 | + 'FLAC', |
| 7 | + 'FLV', |
| 8 | + 'M3U', |
| 9 | + 'M3U8', |
| 10 | + 'M4V', |
| 11 | + 'MKV', |
| 12 | + 'MOV', |
| 13 | + 'MP3', |
| 14 | + 'MP4', |
| 15 | + 'MPEG', |
| 16 | + 'MPG', |
| 17 | + 'OGA', |
| 18 | + 'OGG', |
| 19 | + 'OGV', |
| 20 | + 'OPUS', |
| 21 | + 'WAV', |
| 22 | + 'WEBM', |
| 23 | + 'WMV', |
| 24 | +}) |
| 25 | + |
| 26 | +local SUBTITLES = utils.format_json({ |
| 27 | + 'ASS', |
| 28 | + 'SRT', |
| 29 | + 'SSA', |
| 30 | + 'SUB', |
| 31 | + 'TXT', |
| 32 | +}) |
| 33 | + |
| 34 | +---@class OSAOpts |
| 35 | +---@field title string |
| 36 | +---@field text string|nil |
| 37 | +---@field args string[] |
| 38 | +---@field language? string |
| 39 | +---@field template? string |
| 40 | + |
| 41 | +---@param opts OSAOpts |
| 42 | +---@return fun() |
| 43 | +local function OSAScript(opts) |
| 44 | + return function() |
| 45 | + local template = opts.template or [[ |
| 46 | + var app = Application.currentApplication() |
| 47 | + app.includeStandardAdditions = true |
| 48 | + app.chooseFile({ |
| 49 | + ofType: %s, |
| 50 | + withPrompt: %q, |
| 51 | + defaultLocation: %q, |
| 52 | + multipleSelectionsAllowed: true |
| 53 | + }).join("\n\r") |
| 54 | + ]] |
| 55 | + local language = opts.language or 'AppleScript' |
| 56 | + local path = mp.get_property('path') |
| 57 | + path = path == nil and '.' or utils.split_path( |
| 58 | + utils.join_path(utils.getcwd(), path) |
| 59 | + ) |
| 60 | + local ontop = mp.get_property_native('ontop') |
| 61 | + mp.set_property_native('ontop', false) |
| 62 | + local osascript = utils.subprocess { |
| 63 | + args = { |
| 64 | + 'osascript', '-l', language, '-e', |
| 65 | + template:format(opts.text, opts.title, path) |
| 66 | + }, cancellable = false |
| 67 | + } |
| 68 | + mp.set_property_native('ontop', ontop) |
| 69 | + if osascript.status ~= 0 then return end |
| 70 | + for file in osascript.stdout:gmatch('[^\r\n]+') do |
| 71 | + mp.commandv(opts.args[1], file, opts.args[2]) |
| 72 | + end |
| 73 | + end |
| 74 | +end |
| 75 | + |
| 76 | +mp.add_key_binding('Ctrl+f', 'open-files', OSAScript { |
| 77 | + title = 'Select Media Files', |
| 78 | + text = MULTIMEDIA, |
| 79 | + args = {'loadfile', 'append-play'}, |
| 80 | + language = 'JavaScript' |
| 81 | +}) |
| 82 | +mp.add_key_binding('Ctrl+F', 'open-url', OSAScript { |
| 83 | + title = 'Open URL', |
| 84 | + text = 'Enter the URL to open:', |
| 85 | + args = {'loadfile', 'replace'}, |
| 86 | + template = [[ |
| 87 | + try |
| 88 | + return text returned of ( ¬ |
| 89 | + display dialog "Enter the URL to open:" ¬ |
| 90 | + with title "Open URL" default answer "" ¬ |
| 91 | + buttons {"Cancel", "OK"} default button 2) |
| 92 | + on error number -128 |
| 93 | + return "" |
| 94 | + end try |
| 95 | + ]], |
| 96 | +}) |
| 97 | +mp.add_key_binding('Alt+f', 'open-subs', OSAScript { |
| 98 | + title = 'Select Subtitles', |
| 99 | + text = SUBTITLES, |
| 100 | + args = {'sub-add', 'select'}, |
| 101 | + language = 'JavaScript' |
| 102 | +}) |
0 commit comments