Skip to content

Commit 398b4cc

Browse files
committed
chore: make pngpaste mandatory and remove osascript
1 parent f459467 commit 398b4cc

File tree

4 files changed

+6
-105
lines changed

4 files changed

+6
-105
lines changed

lua/img-clip/clipboard.lua

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ M.get_clip_cmd = function()
1818
elseif util.has("mac") then
1919
if util.executable("pngpaste") then
2020
M.clip_cmd = "pngpaste"
21-
elseif util.executable("osascript") then
22-
M.clip_cmd = "osascript"
2321
end
2422

2523
-- Linux (Wayland)
@@ -50,16 +48,11 @@ M.content_is_image = function()
5048
local output = util.execute("wl-paste --list-types")
5149
return output ~= nil and output:find("image/png") ~= nil
5250

53-
-- MacOS (pngpaste) which is faster than osascript
51+
-- MacOS (pngpaste)
5452
elseif cmd == "pngpaste" then
5553
local _, exit_code = util.execute("pngpaste -")
5654
return exit_code == 0
5755

58-
-- MacOS (osascript) as a fallback
59-
elseif cmd == "osascript" then
60-
local output = util.execute("osascript -e 'clipboard info'")
61-
return output ~= nil and output:find("class PNGf") ~= nil
62-
6356
-- Windows
6457
elseif cmd == "powershell.exe" then
6558
local output =
@@ -90,26 +83,12 @@ M.save_image = function(file_path)
9083
local _, exit_code = util.execute(command)
9184
return exit_code == 0
9285

93-
-- MacOS (pngpaste) which is faster than osascript
86+
-- MacOS (pngpaste)
9487
elseif cmd == "pngpaste" then
9588
local command = string.format('pngpaste - %s> "%s"', process_cmd:gsub("%%", "%%%%"), file_path)
9689
local _, exit_code = util.execute(command)
9790
return exit_code == 0
9891

99-
-- MacOS (osascript) as a fallback
100-
elseif cmd == "osascript" then
101-
local command = string.format(
102-
[[osascript -e 'set theFile to (open for access POSIX file "%s" with write permission)' ]]
103-
.. [[-e 'try' -e 'write (the clipboard as «class PNGf») to theFile' -e 'end try' ]]
104-
.. [[-e 'close access theFile' -e 'do shell script "cat %s %s> %s"']],
105-
file_path,
106-
file_path,
107-
process_cmd:gsub("%%", "%%%%"),
108-
file_path
109-
)
110-
local _, exit_code = util.execute(command)
111-
return exit_code == 0
112-
11392
-- Windows
11493
elseif cmd == "powershell.exe" then
11594
local command = string.format(
@@ -145,17 +124,12 @@ M.get_content = function()
145124
end
146125

147126
-- MacOS
148-
elseif cmd == "pngpaste" or cmd == "osascript" then
127+
elseif cmd == "pngpaste" then
149128
local output, exit_code = util.execute("pbpaste")
150129
if exit_code == 0 then
151130
return output:match("^[^\n]+")
152131
end
153132

154-
output, exit_code = util.execute([[osascript -e 'get the clipboard as text']])
155-
if exit_code == 0 then
156-
return output:match("^[^\n]+")
157-
end
158-
159133
-- Windows
160134
elseif cmd == "powershell.exe" then
161135
local output, exit_code = util.execute([[powershell -command "Get-Clipboard"]])
@@ -196,19 +170,6 @@ M.get_base64_encoded_image = function()
196170
return output
197171
end
198172

199-
-- MacOS (osascript)
200-
elseif cmd == "osascript" then
201-
local output, exit_code = util.execute(
202-
[[osascript -e 'set theFile to (open for access POSIX file "/tmp/image.png" with write permission)' ]]
203-
.. [[-e 'try' -e 'write (the clipboard as «class PNGf») to theFile' -e 'end try' -e 'close access theFile'; ]]
204-
.. [[/tmp/image.png ]]
205-
.. process_cmd
206-
.. [[ | base64 | tr -d "\n" ]]
207-
)
208-
if exit_code == 0 then
209-
return output
210-
end
211-
212173
-- Windows
213174
elseif cmd == "powershell.exe" then
214175
local output, exit_code = util.execute(

lua/img-clip/health.lua

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,10 @@ M.check = function()
2828

2929
-- MacOS
3030
elseif util.has("mac") then
31-
if util.executable("osascript") then
32-
ok("`osascript` is installed")
33-
else
34-
error("`osascript` is not installed")
35-
end
3631
if util.executable("pngpaste") then
3732
ok("`pngpaste` is installed")
3833
else
39-
warn("`pngpaste` is not installed")
34+
error("`pngpaste` is not installed")
4035
end
4136

4237
-- Windows

tests/clipboard_spec.lua

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ describe("clipboard", function()
153153
return nil
154154
end
155155
util.executable = function(cmd)
156-
return cmd == "pngpaste" or cmd == "osascript"
156+
return cmd == "pngpaste"
157157
end
158158
end)
159159

@@ -198,61 +198,6 @@ describe("clipboard", function()
198198
assert.is_true(clipboard.save_image("/path/to/image.png"))
199199
end)
200200
end)
201-
202-
describe("osascript", function()
203-
before_each(function()
204-
util.has = function(feature)
205-
return feature == "mac"
206-
end
207-
os.getenv = function()
208-
return nil
209-
end
210-
util.executable = function(cmd)
211-
return cmd == "osascript"
212-
end
213-
end)
214-
215-
it("returns 'osascript' as the clipboard command", function()
216-
assert.equals("osascript", clipboard.get_clip_cmd())
217-
end)
218-
219-
it("returns true if clipboard content is an image", function()
220-
util.execute = function(command)
221-
assert(command:match("osascript"))
222-
return "class PNGf", 0 -- output of osascript
223-
end
224-
225-
assert.is_true(clipboard.content_is_image())
226-
end)
227-
228-
it("returns false if clipboard content is not an image", function()
229-
util.execute = function(command)
230-
assert(command:match("osascript"))
231-
return "Text", 1 -- output of osascript
232-
end
233-
234-
assert.is_false(clipboard.content_is_image())
235-
end)
236-
237-
it("gets first line of clipboard content", function()
238-
util.execute = function(command)
239-
assert(command:match("pbpaste"))
240-
return [[first line
241-
second line]], 0
242-
end
243-
244-
assert.equals("first line", clipboard.get_content())
245-
end)
246-
247-
it("successfully saves an image", function()
248-
util.execute = function(command)
249-
assert(command:match("osascript"))
250-
return nil, 0 -- simulate successful execution
251-
end
252-
253-
assert.is_true(clipboard.save_image("/path/to/image.png"))
254-
end)
255-
end)
256201
end)
257202

258203
-- Windows (including WSL)

vimdoc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ https://github.com/HakonHarnes/img-clip.nvim/assets/89907156/ab4edc10-d296-4532-
2020
## 🔧 Requirements
2121

2222
- **Linux:** [xclip](https://github.com/astrand/xclip) (x11) or [wl-clipboard](https://github.com/bugaevc/wl-clipboard) (wayland)
23-
- **MacOS:** [pngpaste](https://github.com/jcsalterego/pngpaste) (optional, but recommended)
23+
- **MacOS:** [pngpaste](https://github.com/jcsalterego/pngpaste)
2424
- **Windows:** No additional requirements
2525

2626
> [!IMPORTANT]

0 commit comments

Comments
 (0)