Pasting images copied from web browsers

I’ve seen several posts asking if there is a way to paste an actual image copied from a browser instead of a link to the image.

The following bit of AppleScript (which can be turned into an app, run from the command line, or embedded in a Keyboard Maestro macro, etc.) writes the clipboard into a temporary image file then reads the temporary file back into the clipboard. What was a clipboard with a bunch of extraneous information is now a clipboard that only contains the image. ⌘V will now put a pasted image into Obsidian.

# Takes a complicated clipboard containing an image and turns it
# into a simple clipboard containing only an image.

# Make a temporary file and get its path
set filePath to do shell script "echo `mktemp /tmp/clipboard.XXXXX`"

# Get the image out of the clipboard and write it to the tmp file
set theImage to the clipboard as {«class PNGf»}
set openedFile to open for access filePath with write permission
write theImage to openedFile
close access openedFile

# Reopen the temporary file and read the image into the clipboard
set openedFile to open for access filePath
set the clipboard to (read openedFile as {«class PNGf»})
close access openedFile

# Delete the temporary file
do shell script "rm " & filePath