Applescript to convert image to its base64 data-uri equivalent

I recently had a need to convert some image files to its base64 equivalent as I was creating some email signatures and needed to embed some images.

Here is the script that takes a selection of images and converts them to their base64 encoded data-uri results. The results will appear in the same location as the original image file.

Pre-requisites: OpenSSL*see thread below

Feel free to enhance and tweak.

Latest version on Github and here:

-- Converts an image file to its base64 data-uri equivalent.
-- Created by Ian Shen / 2B3 PRODUCTIONS LLC on 2021/12/31
-- Latest at
-- Copyright (cc) Attribution-NonCommercial-ShareAlike 4.0 International

tell application id "DNtp"
		set this_selection to the selection
		set the_group to the current group
		if this_selection is {""} then error "Please select some image files."
		repeat with this_item in this_selection
			set the_rectype to the type of this_item as string
			set the_imagetype to the MIME type of this_item
			if the_rectype = "picture" or the_imagetype = "image/svg+xml" then
					set this_path to the path of this_item
					set the_tags to the tags of this_item
					set the_record to (create record with {type:"text", name:(name without extension of this_item) & "-b64.txt"} in the_group)
					set the_b64_path to the path of the_record
					with timeout of 45 seconds
						set the_results to (do shell script "/usr/bin/openssl base64 -in " & this_path)
					end timeout
					set the plain text of the_record to ("data:" & the_imagetype & ";base64," & the_results) as string
					set the tags of the_record to the_tags
				end try
				display alert "This file is not an image file."
			end if
		end repeat
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
	end try
end tell

Btw: I wrote a similar script in JavaScript that does not need OpenSSL:

It relies on ObjC so could be re-written in AppleScript easily, I suppose.

Welcome @2b3pro
Thanks for sharing your idea with everyone. It’s nice to have you here :slight_smile:

PS: I am a little curious why you opted for using openssl instead of just the base64 command?


To be honest, I guess I could have! I didn’t know. Still learning!

And thank you for the welcome! You’re a wonderful resource in these forums. Thank you, Jim!


set the_results to (do shell script "base64 " & this_path)
1 Like

You’re very welcome and thank you for the kind words of encouragement. They’re very appreciated!

Keep on learning! :smiley: