Textexpander & YOURLs
A few months ago, I made the decision to switch from using Bitly for domain shortening to hosting my own URL shortener. The primary motivation behind this move was to save more than just a few dollars. After exploring various options available, I ultimately settled on YOURLs as my preferred choice.
The final step in this migration process was to integrate TextExpander with YOURLs, and in this blog post, I will guide you through the process.
This TextExpander snippet serves the purpose of shortening a URL that is currently on your clipboard. By typing
/bitly, TextExpander automatically takes the URL, initiates a call to the shortening service, and inserts the shortened URL for your convenience. This streamlined process saves time and simplifies the task of URL shortening.
Personally, I prefer to keep all my TextExpander scripts within the application itself. This ensures easy transfer across the multiple Macs that I frequently use. To get started, I created a new snippet of the type “Shell Script.”
I gave it a label of “YOURLs” and an abbreviation of “/bitly”. I used this abbreviation even though the powering service has changed as I already have muscle memory of using it and don’t want to force that change fo no real benefit.
Now for the fun part… Here is the script, and I’ll break down each piece. If you need more details on any of these, it should be easily Google-able. Or ask ChatGPT!
#!/bin/bash curl --silent -L -w '\n' "curl --silent -L -w '\n' "https://YOUR_URL_HERE/yourls-api.php?signature=YOUR_KEY_HERE&action=shorturl&format=simply&url=%clipboard"
#!/bin/bash- This line instructs the shell on how to execute the script. Simply include it as-is.
curl- The shell command to fetch something from the internet. man page
--silent- Runs the command without unnecessary output.
-L- Enables redirection; YOURLs redirects from an HTML page with a 301 status code to the raw, shortened URL.
-w '\\n'- By default, the zsh shell adds a ‘%’ sign and a new line at the end, which we want to remove.
https://- Standard HTTPS call. Will work over HTTP, but this is 2023. Secure your server.
YOUR_URL_HERE- Replace this with the domain where your YOURLs server is hosted. It corresponds to the “domain.tld” part of your YOURLs admin URL, ex: https://abc.com/admin.
/yourls-api.php- Calls the YOURLs API page
?signature=- Indicates that we will be passing the signature/API key to the API.
YOUR_KEY_HERE- Replace this with the API key from your YOURLs admin. For further details, refer to the documentation.
action=shorturl- Instructs the API to shorten the URL.
format=simply- Requests only the short URL as the response from the API.
url=%clipboard- Instructs TextExpander to insert the URL currently stored on your clipboard.
I hope this helps others. If you run into questions, let me know. Contact info on the left.