Generate Alt Text with TextExpander

tdrayson

I came across AltText.AI on Appsumo and it looked like a nice tool to help with generating Alt text for images. They have integrations with a bunch of different solutions including WordPress. However I didn’t want to install a plugin just for generating alt text.

Instead, I created a TextExpander snippet that allows me to make use of their API and generate alt text on the fly only when I needed it.

Note: You shouldn't rely soley on the AI generator for your Alt text. Only as a starting point.

This solution uses an AppleScript, this means the solution only works for people on a Mac.

Demo

1. Get your API key

Make sure you are signed in to your AltText.ai account. Once logged in, you can get your API key from here.

A screenshot of the altText.ai account page

2. Installing dependancies

The TextExpander script relies on two dependancies – Homebrew and jq.

Homebrew

In your terminal, run this command to install:

JSON

        ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null
      
Copy

jq

Once homebrew is installed. You can install jq with the command:

JSON

        brew install jq
      
Copy

3. The snippet

Add a new TextExpander snippet and set the content type to applescript.

Set your apiKey and replace {{image url}} and {{keywords}} with TextExpander fill ins.

JSON

        set apiKey to "XXXXXXXXXXXXX"
set imageUrl to "{{image url}}"
set keywordsString to "{{keywords}}"


set AppleScript's text item delimiters to ","
set tagsList to text items of keywordsString
set AppleScript's text item delimiters to {""}


set tagsJSON to "\"tags\": ["
repeat with i from 1 to count of tagsList
	set thisTag to item i of tagsList
	set tagsJSON to tagsJSON & " \"" & thisTag & "\""
	if i is not equal to (count of tagsList) then
		set tagsJSON to tagsJSON & ","
	end if
end repeat
set tagsJSON to tagsJSON & " ], "


set overwriteJSON to "\"overwrite\": true"
set jsonData to "{ \"image\": { \"url\": \"" & imageUrl & "\", " & tagsJSON & overwriteJSON & " } }"
set curlCommand to "curl -X POST 'https://alttext.ai/api/v1/images' -H 'X-API-Key: " & apiKey & "' -H 'Content-Type: application/json' -d '" & jsonData & "'"


try
	set response to do shell script curlCommand
	set altText to do shell script "echo " & quoted form of response & " | /opt/homebrew/bin/jq -r '.alt_text'"
	return altText
on error errText
	return "Error: " & errText
end try
      
Copy

👋🏻 Weekly Tutorial Digest

I send out a weekly newsletter with links to new tutorials written in the last week, you can subscribe below.

Newsletter

🔒I won't send you spam, I promise