Pages

Thursday, December 7

Grabbing a domain name out of a URL using Shortcuts

Recently I needed the ability to grab a domain name (specifically the hostname) out of a URL using Shortcuts.  

I wanted to integrate this functionality into a much larger shortcut, but this blog post will highlight just the domain parsing bit.


This will get you the whole domain name from any given URL from the share sheet.  That being said, I wanted to take it a step further and get just the hostname out of the domain name:


Assuming that there is more than two parts to the domain name, this works.  But if you have and "A" type of url "blah.com" instead of a "CNAME" type like: "www.blah.com", you're going to get "com" as a result.  So your milage may vary.

I couldn't find a good way to do this on the Internet, despite several attempts for people to try and figure it out using regex.  You can then go on to store the result as a variable and then use the variable anywhere else in the rest of the Shortcut.  

Anyway, here's a way to do it.  Hopefully this blog post helps someone trying to figure out the same issue.  It's a little hacky, but knock yourself out.


Please leave comments below.

No comments: