Download Image in Clipboard on OSX with Raycast Script

With the amount of applications that still don't allow copy/paste of an image, I find myself needed a faster way to quickly screengrab and download an image on OSX.


Table of contents

The problem

I've setup cmd+shift+4 to take a screenshot and copy it to clipboard. This is great for most cases, but sometimes I'm not able to copy/paste the image where I need to. For example, I'm trying to upload an image to a website that doesn't allow copy/paste of images. I need to save the image to my desktop, then upload it.

The solution

This really simple Python snippet as a Raycast extension simply saves any image in my clipboard to my downloads automagically, and I can then upload the file

#!/usr/bin/env python3
 
# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title Screenshot from clipboard
# @raycast.mode compact
 
# Optional parameters:
# @raycast.icon 🤖
# @raycast.packageName Adam Richardson
# @raycast.argument1 { "type": "text", "file-name": "" }
 
 
# Documentation:
# @raycast.description Save image to downloads from clipboard
# @raycast.author adamrichardson14
# @raycast.authorURL https://raycast.com/adamrichardson14
 
 
from datetime import datetime
from pathlib import Path
 
from PIL import ImageGrab
 
home = Path.home()
 
 
def save_image():
    im = ImageGrab.grabclipboard()
    if im != None:
        filepath = home / "Downloads/ScreenShot-{}.png".format(
            datetime.now().strftime("%Y%m%d-%H%M%S")
        )
        im.save(filepath.as_posix())
        print(f"OK Saved In: {filepath}")
    else:
        print("No Image in Clipboard")
 
 
if __name__ == "__main__":
    save_image()
 
Copyright Adam Richardson © 2024 - All rights reserved
𝕏