简体   繁体   中英

How to copy a GIF to clipboard in Mac using python

I would like to write a python function to obtain the gif file given the URL, and store it in Mac's clipboard. Could anyone please help me figure out how to copy GIF into clipboard?

(Let's say the URL is very simple and direct to the GIF I want, "a.com/this.gif".)

您可以使用以下 applescript 将 gif 复制到剪贴板:

osascript -e 'set the clipboard to (read (POSIX file "/Users/auser/yourgif.gif") as GIF picture)'

You can point the clipboard directly at the file instead of reading its contents:

osascript -e "set the clipboard to (POSIX file \"$f\")"

You can combine this in a script to automatically do the work:

#!/usr/bin/env bash
set -euo pipefail
f="$(mktemp).gif"
curl -o "$f" --fail "${1?Usage: $0 <url-of-gif-file>}"
osascript -e "set the clipboard to (POSIX file \"$f\")"
# Don't delete the tempfile: it holds the GIF. It will be cleaned up on reboot.

Inspired by https://stackoverflow.com/a/51447031/4359699

This would work for any file type by the way, not just GIF.

In Mac OS X, you can use the pbcopy command to copy content to the system clipboard. To achieve this from your Python script, I imagine you would want to fetch the resource (I imagine using urlopen ), and then use the subprocess module to invoke pbcopy with the content of the GIF file that you fetched.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM