Skip to content

Commit 8433289

Browse files
feat(open-dialog): add OSAScript version
1 parent b174919 commit 8433289

File tree

3 files changed

+111
-8
lines changed

3 files changed

+111
-8
lines changed

.github/workflows/issues.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ jobs:
6161
project-name: Open Dialog (PowerShell)
6262
project-column: To do
6363
project-scope: repository
64+
- name: Add to Open Dialog (OSAScript) project
65+
if: contains(github.event.issue.body, '`open-dialog/osascript.lua`')
66+
uses: jeffdanielperso/[email protected]
67+
with:
68+
project-name: Open Dialog (OSAScript)
69+
project-column: To do
70+
project-scope: repository
6471
- name: Add to Discord project
6572
if: contains(github.event.issue.body, '`discord.lua`')
6673
uses: jeffdanielperso/[email protected]

open-dialog/README.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,12 @@ If not, you should download
3434

3535
### Windows
3636

37-
You just have to download [powershell.lua](powershell.lua).
37+
Download [powershell.lua](powershell.lua).
3838

3939
### MacOS
4040

41-
You can install Zenity from [MacPorts][ports]
42-
or [Homebrew][brew] and download [zenity_nox.lua][zenity-nox].
43-
44-
**TODO**: Write an AppleScript version.
41+
Download [osascript.lua](osascript.lua).
4542

4643
[kdialog]: https://github.com/KDE/kdialog
4744
[zenity]: https://github.com/GNOME/zenity
4845
[xdotool]: https://github.com/jordansissel/xdotool
49-
[zenity-nox]: https://git.io/JeZZL
50-
[brew]: https://formulae.brew.sh/formula/zenity
51-
[ports]: https://ports.macports.org/port/zenity/summary

open-dialog/osascript.lua

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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

Comments
 (0)