Created
January 31, 2023 00:51
-
-
Save egradman/fe31edaa9ddf7e3374438cea4c9648e4 to your computer and use it in GitHub Desktop.
Copies the current document to the Mac clipboard. It preserves formatting, because it uses pandoc/HTML as an intermediate step.I use https://github.com/Taitava/obsidian-shellcommands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import re | |
import os | |
import tempfile | |
import shutil | |
import uuid | |
from subprocess import Popen, PIPE, STDOUT | |
pwd = os.getcwd() | |
filename = sys.argv[1] | |
dirname = os.path.dirname(filename) | |
def make_embed(match): | |
target = os.path.join(pwd, dirname, match.group(1)) | |
target = target.replace(" ", "%20") | |
if match.group(2): | |
size = match.group(2)[1:] | |
return f"{{width={size}px}}" | |
else: | |
return f"" | |
result = re.sub(r"!\[\[(.*?)(\|\d+)?\]\]", make_embed, open(filename, "r").read(), re.MULTILINE) | |
output_html_path = os.path.join(os.getenv("TMPDIR"), f"{uuid.uuid1()}.html") | |
cmd = f"/opt/homebrew/bin/pandoc -f commonmark_x -t html --embed-resources --standalone -s --o {output_html_path}".split(" ") | |
p = Popen(cmd, stdin=PIPE, stdout=PIPE) | |
stdout = p.communicate(input=result.encode("utf-8"))[0] | |
output = stdout.decode() | |
script = f""" | |
tell application "TextEdit" | |
activate | |
open POSIX file "{output_html_path}" | |
delay 1 | |
tell application "System Events" | |
keystroke "a" using command down | |
keystroke "c" using command down | |
end tell | |
delay 1 | |
quit | |
end tell | |
""" | |
p = Popen(["osascript"], stdin=PIPE, stdout=PIPE, stderr=STDOUT) | |
stdout = p.communicate(input=script.encode("utf-8"))[0] | |
os.unlink(output_html_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment