Skip to content

Instantly share code, notes, and snippets.

@seqis
Last active March 28, 2025 12:56
Show Gist options
  • Save seqis/b043ce792820b560b750898b443a1b6b to your computer and use it in GitHub Desktop.
Save seqis/b043ce792820b560b750898b443a1b6b to your computer and use it in GitHub Desktop.
This script will take anything in your clipboard, including graphics and create a quick Obsidian note. The graphics will be placed in the `Media` folder under the `Notes` folder. The paths are hard-coded for your current Obsidian vault Notes folder.
#!/bin/bash
# This script will take anything in your clipboard, including graphics and create a quick Obsidian note. The graphics will be placed in the `Media` folder under the `Notes` folder. The paths are hard-coded for your current Obsidian vault Notes folder. This script presumes a "Media" foler exists under your ""Notes" folder.
import os
import subprocess
from datetime import datetime
from PIL import Image
import pyperclip
# Hard-coded path to your Obsidian vault's directory
obsidian_vault_path = "/path/2/your/notes/folder"
media_folder_path = os.path.join(obsidian_vault_path, "Media")
# Ensure the media folder exists
if not os.path.exists(media_folder_path):
os.makedirs(media_folder_path)
# Function to save text content to a markdown file
def save_text_to_markdown(content):
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
file_name = f"Highlight_{timestamp}.md"
file_path = os.path.join(obsidian_vault_path, file_name)
with open(file_path, 'w') as file:
file.write("#### **MapOfContent:** [[Scraps]]\n")
file.write("---\n\n")
file.write(content)
print(f"Saved text highlight to {file_path}")
# Function to save image content to a markdown file
def save_image_to_markdown(image_path):
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
image_name = os.path.basename(image_path)
# Create a markdown file with a link to the image
file_name = f"Image_Highlight_{timestamp}.md"
file_path = os.path.join(obsidian_vault_path, file_name)
with open(file_path, 'w') as file:
file.write("#### **MapOfContent:** [[Scraps]]\n")
file.write("---\n\n")
file.write(f"![{image_name}](./Media/{image_name})\n")
print(f"Saved image highlight to {file_path}")
# Function to check for image in clipboard using xclip
def get_clipboard_image():
image_path = os.path.join(media_folder_path, f"ClipboardImage_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.png")
try:
# Command to check if there is an image in the clipboard
command = f"xclip -selection clipboard -t image/png -o > {image_path}"
subprocess.run(command, shell=True, check=True)
# Check if the image was saved
if os.path.exists(image_path):
return image_path
else:
return None
except subprocess.CalledProcessError:
return None
# Main script logic
def main():
clipboard_content = pyperclip.paste().strip()
if clipboard_content:
save_text_to_markdown(clipboard_content)
else:
# Attempt to grab an image from the clipboard
image_path = get_clipboard_image()
if image_path:
save_image_to_markdown(image_path)
else:
print("Clipboard is empty or contains unsupported content. Nothing to save.")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment