Post images quickly with this Imgur Service

Inserting screenshots in your posts usually involves uploading the image to a host, copying the URL, and then enclosing the URL in tags. This service reduces the steps in the process to a control-click.

Create a new Service workflow in Automator that receives files or folders in Finder and paste the AppleScript below into a Run AppleScript action. The script will populate your clipboard with a URL ready for posting. Enjoy !

-- Register for a free API Developer Key at https://imgur.com/register/api_anon
property apiKey : "enter your api key"

-- Supported file types
property fileTypes : {"jpeg", "jpg", "gif", "png", "apng", "tiff", "bmp", "pdf", "xcf"}

-- If true, this will enclose the URL in a  tag
property img_tag : true

on run
	-- Verify the selected file is a supported file type
	tell application "Finder"
		set userSelect to (selection as alias list)
		if (count of userSelect) ≠ 1 then
			tell me to badChoice()
		else if name extension of first item of userSelect is not in fileTypes then
			tell me to badChoice()
		end if
	end tell
	
	-- Upload the file
	set theFile to quoted form of POSIX path of userSelect
	set imgurUpload to do shell script "curl -F image=@" & theFile & " -F 'key=" & apiKey & "' http://api.imgur.com/2/upload.xml "
	
	-- Copy the URL into your clipboard
	set {TID, text item delimiters} to {text item delimiters, {"<original>", "</original>"}}
	if img_tag then
		set the clipboard to "[img]" & text item 2 of imgurUpload & "[/img]"
	else
		set the clipboard to text item 2 of imgurUpload
	end if
	
	-- Reset delimiters
	set text item delimiters to TID
end run

on badChoice()
	using terms from application "Finder"
		tell me
			activate
			display alert "Please select ONE supported file and try again." message "Allowed file types: JPEG, JPG, GIF, PNG, APNG, TIFF, BMP, PDF, XCF (GIMP). " & return & return & "Please note that the TIFF, BMP, PDF and XCF formats will be converted to PNG on upload." buttons "OK" cancel button "OK"
		end tell
	end using terms from
end badChoice