gingerbeardman Posted April 25, 2023 Posted April 25, 2023 I often have to generate data-uri for some bitmap data. Previously I had to save the data to a file and process it that way. I needed to support - data currently in clipboard I can get old data from clipboard history and make it current clipboard My workaround is using `pngpaste` terminal command and a workflow that does: #!/usr/bin/env bash echo "data:image/png;base64,$(pngpaste -b)" | pbcopy https://www.gingerbeardman.com/alfred/image to data-uri.alfredworkflow That works well. I'm wondering if there is a more efficient way to do this in Alfred that relies only in built-in tools? matt
vitor Posted April 26, 2023 Posted April 26, 2023 Here’s how you can save image data from the clipboard via AppleScript, with quite a bit of error handling. This particular code saves it to the Desktop, named as a screenshot. WebScreenshot can do it too. try set imageData to the clipboard as {«class PNGf»} on error tell me to error "Clipboard data does not seem to be an image" end try set filePath to (POSIX path of (path to desktop folder)) & (do shell script "date +'Screenshot %Y-%m-%d at %H.%M.%S.png'") tell application "System Events" to if filePath exists then tell me to error "Path exists: " & filePath set imageFile to open for access filePath with write permission write imageData to imageFile close access imageFile Alternatively, look inside ~/Library/Application Support/Alfred/Databases/clipboard.alfdb.data which has the images stored as tiffs. You can convert them to PNG with the Convert Image Format Automation Task.
gingerbeardman Posted April 26, 2023 Author Posted April 26, 2023 Thanks, not quite what I was looking for. I need the data-uri (base64) of the image, and the process needs to be transient - I don't want files to be created on disk. This is what my workflow does (produces base64 of the current clipboard image) but it relies on the third party tool pngpaste
vitor Posted April 26, 2023 Posted April 26, 2023 Right. I may still be able to help but to give the best answer I need to understand what’s the scope of the request. You want to get the base64 of the image without any dependencies on software which does not ship by default with macOS, correct? That is simpler if you create a temporary image then run the base64 command on it. I’ll simplify the code above and show what I mean. Note how it creates the image in the tmp directory. set imageData to the clipboard as {«class PNGf»} set filePath to (POSIX file "/tmp/base64image.png") set imageFile to open for access filePath with write permission write imageData to imageFile close access imageFile Then you connect that to a Zsh Run Script: echo "data:image/png;base64,$(base64 --input=/tmp/base64image.png)" | pbcopy rm '/tmp/base64image.png' You don’t even need to rm the image if you don’t want, as /tmp is cleaned up on reboots and the AppleScript overwrites the path. gingerbeardman 1
gingerbeardman Posted April 26, 2023 Author Posted April 26, 2023 Thanks! That seems to be it, I will give it a try. I like to keep my brew/dependencies to a minimum, as that thing takes so long to do updates if left unchecked.
vitor Posted April 26, 2023 Posted April 26, 2023 (edited) Note that since February Homebrew gets the list of formulae and casks from a JSON, it no longer git pulls the whole repo. Updates are much faster. But yes, I also prefer to do things with as few dependencies as possible. Edited April 26, 2023 by vitor gingerbeardman 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now