-
Notifications
You must be signed in to change notification settings - Fork 3
/
_powershell.jl
50 lines (43 loc) · 1.54 KB
/
_powershell.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
_powershell() -> AbstractMatrix{<:Colorant}
Paste an image from clipboard using `powershell`
"""
function _powershell()
mktempdir() do dir
# Define path
path_png = joinpath(dir, "clipboard.png")
# Compose command & run
addtype = `Add-Type -AssemblyName System.Windows.Forms\;`
getimg = `\$img=\[Windows.Forms.Clipboard\]::GetImage\(\)\;`
saveimg = `if \(\$img -ne \$null\)\{\$img.Save\(\"$(path_png)\"\)\}`
cmd = `powershell.exe -NoProfile $addtype $getimg $saveimg`
run(cmd)
# Paste from clipboard
if isfile(path_png)
img = load(path_png)
return img
else
error("No image in clipboard")
end
end
end
"""
_powershell(img::AbstractMatrix{<:Colorant})
Copy an image to clipboard using `powershell`
"""
function _powershell(img::AbstractMatrix{<:Colorant})
mktempdir() do dir
# Define path
filename = "clipboard.png"
# Save image
save(joinpath(dir, filename), img)
# Compose command & run
addtype = `Add-Type -AssemblyName System.Windows.Forms\;`
adddrawing = `\[Reflection.Assembly\]::LoadWithPartialName\(\'System.Drawing\'\)\;`
getfile = `\$file = get-item\(\"$(filename)\"\)\;`
getimg = `\$img = \[System.Drawing.Image\]::Fromfile\(\$file\)\;`
copyimg = `\[System.Windows.Forms.Clipboard\]::SetImage\(\$img\)\;`
cmd = Cmd(`powershell.exe -NoProfile $addtype $adddrawing $getfile $getimg $copyimg`; dir)
run(cmd)
end
end