Automatic URL Shortening with YOURLS and TextExpander

Hi there!

I have been using the bitly and now have switched to yours. I use TextExpander to shorten the links, for bitly was using this apple script which was doing the job.


set my_API_Key to "%snippet:my_bitly_API_key%"

if (my_Login is equal to "%%snippet:my_bitly_login%%" or my_API_Key is equal to "%%snippet:my_bitly_API_key%%") then
return "bit.ly now requires you to have an account and to specify your login and API key. Please create snippets with abbreviations of my_bitly_login and my_bitly_API_key and the corresponding values."
else
set my_Login to first paragraph of my_Login
set my_API_Key to first paragraph of my_API_Key
end

set the ClipURL to (the clipboard as string)

ignoring case
	if ((characters 1 through 4 of ClipURL as string) is not "http") then
		return "Nah! No URL Copied."
	else
		set the EncodedClipURL to urlencode(ClipURL) of me
		set curlCMD to ¬
			"curl --stderr /dev/null \"http://api.bit.ly/v3/shorten?format=txt&login=" & my_Login & "&apiKey=" & my_API_Key & "&uri=" & EncodedClipURL & "\""
		
		-- Run the script and get the result:
		set tinyURL to (do shell script curlCMD)
		
		return tinyURL
	end if
end ignoringa


on urlencode(theText)
	set theTextEnc to ""
	repeat with eachChar in characters of theText
		set useChar to eachChar
		set eachCharNum to ASCII number of eachChar
		if eachCharNum = 32 then
			set useChar to "+"
		else if (eachCharNum ≠ 42) and (eachCharNum ≠ 95) and (eachCharNum < 45 or eachCharNum > 46) and (eachCharNum < 48 or eachCharNum > 57) and (eachCharNum < 65 or eachCharNum > 90) and (eachCharNum < 97 or eachCharNum > 122) then
			set firstDig to round (eachCharNum / 16) rounding down
			set secondDig to eachCharNum mod 16
			if firstDig > 9 then
				set aNum to firstDig + 55
				set firstDig to ASCII character aNum
			end if
			if secondDig > 9 then
				set aNum to secondDig + 55
				set secondDig to ASCII character aNum
			end if
			set numHex to ("%" & (firstDig as string) & (secondDig as string)) as string
			set useChar to numHex
		end if
		set theTextEnc to theTextEnc & useChar as string
	end repeat
	return theTextEnc
end urlencode```

I'm wondering if this can be modified or somehow used for the yourls too? 

Here's [the post](https://go.abdurrehman.net/auto-shorten) for how to set up in Textepxander.

Thoughts? 

Thanks, in advance

Don’t know textexpander and don’t use a Mac, but I know that there are 2 links about YOURLS and textexpander, including something that looks like code to do what you want, on the first page of googling “YOURLS textexpander”

I did Google so much before opening the ticket and hadn’t found anything, there were some post but they aren’t working. I thought you could be aware of this.

http://chrismarquardt.com/articles/textexpanderyourls.txt ?

I had tried this as well and this gives me the result (AppleScript did not compile)

Here’s how I have set it up. https://puu.sh/FSdbX/3a9815fe47.png

Here’s when I use that shortcut to run the script.

I believe that have followed it but doesn’t work. The script works fine with the bitly.

I don’t know if it will help at all, but following is the script I use with Keyboard Maestro using AppleScript and curl (all cobbled together because I am a total hack!):

set the ClipURL to (the clipboard as string)

tell application "Keyboard Maestro Engine"
	set LongURL to process tokens ("%Variable%LL_LongURL%")
	set ShortURLKeyword to process tokens ("%Variable%LL_ShortURLKeyword%")
end tell

ignoring case
	if ((characters 1 through 4 of ClipURL as string) is not "http") then
		return "Malformed URL."
	else
		set the EncodedClipURL to urlencode(ClipURL) of me
		set curlCMD to "curl --stderr /dev/null \"https://ktxc.to/yourls-api.php?signature=YOURTOKEN&action=shorturl&format=simple&keyword=" & ShortURLKeyword & "&url=" & EncodedClipURL & "\""
		-- return curlCMD
		set ShortURL to (do shell script curlCMD)
		return ShortURL
	end if
end ignoring

on urlencode(theText)
	set theTextEnc to ""
	repeat with eachChar in characters of theText
		set useChar to eachChar
		set eachCharNum to ASCII number of eachChar
		if eachCharNum = 32 then
			set useChar to "+"
		else if (eachCharNum ≠ 42) and (eachCharNum ≠ 95) and (eachCharNum < 45 or eachCharNum > 46) and (eachCharNum < 48 or eachCharNum > 57) and (eachCharNum < 65 or eachCharNum > 90) and (eachCharNum < 97 or eachCharNum > 122) then
			set firstDig to round (eachCharNum / 16) rounding down
			set secondDig to eachCharNum mod 16
			if firstDig > 9 then
				set aNum to firstDig + 55
				set firstDig to ASCII character aNum
			end if
			if secondDig > 9 then
				set aNum to secondDig + 55
				set secondDig to ASCII character aNum
			end if
			set numHex to ("%" & (firstDig as string) & (secondDig as string)) as string
			set useChar to numHex
		end if
		set theTextEnc to theTextEnc & useChar as string
	end repeat
	return theTextEnc
end urlencode