Skip to content

Commit

Permalink
Merge pull request #20 from hyrodium/feature/mac
Browse files Browse the repository at this point in the history
Add support for mac
  • Loading branch information
hyrodium committed Jun 15, 2021
2 parents aff6a09 + 904674f commit cd607ba
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobs:
os:
- ubuntu-latest
- windows-latest
- macos-latest
arch:
- x64
include:
Expand Down
6 changes: 5 additions & 1 deletion src/ImageClipboard.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ include("_xclip.jl")
# linux-wayland
include("_wlclipboard.jl")
# mac
# include("_osascript.jl")
include("_osascript.jl")
# windows
include("_powershell.jl")

Expand All @@ -35,6 +35,8 @@ function clipboard_img()
end
elseif Sys.iswindows()
img = _powershell()
elseif Sys.isapple()
img = _osascript()
else
error("Currently, only linux and windows are supported")
end
Expand Down Expand Up @@ -63,6 +65,8 @@ function clipboard_img(img::Matrix{<:Colorant})
end
elseif Sys.iswindows()
_powershell(img)
elseif Sys.isapple()
_osascript(img)
else
error("Currently, only linux and windows are supported")
end
Expand Down
42 changes: 42 additions & 0 deletions src/_osascript.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
_osascript() -> Matrix{<:Colorant}
Paste an image from clipboard using osascript
"""
function _osascript()
mktempdir() do dir
# Define path
path_png = joinpath(dir, "clipboard.png")

# Compose command & run
cmd = Cmd(["osascript", "-e", "write (the clipboard as «class PNGf») to (open for access \"$(path_png)\" with write permission)"])
run(cmd)

# Paste from clipboard
if isfile(path_png)
img = load(path_png)
return img
else
error("No image in clipboard")
end
end
end

"""
_osascript(img::Matrix{<:Colorant})
Copy an image to clipboard using osascript
"""
function _osascript(img::Matrix{<:Colorant})
mktempdir() do dir
# Define path
path_png = joinpath(dir, "clipboard.png")

# Save image
save(path_png, img)

# Compose command & run
cmd = Cmd(["osascript", "-e", "set the clipboard to (read \"$(path_png)\" as «class PNGf»)"])
run(cmd)
end
end

0 comments on commit cd607ba

Please sign in to comment.