i would like to create a reusable script in scriptable that determines the hostname (document.location.hostname) of the currently opened URL for use in a shortcut.
Has anyone built such a script before? I do not know the syntax, so I hope that I will find a solution here in the forum.
Greetings from Germany, André
If I’m understanding this right, from a URL like
https://www.google.com/other/stuff you want to get
I wrote a shortcut for that.
The general idea is to use regular expressions. I’ve used a couple of features in there so I’ll try to break it down:
(http://|https://) – this matches the string “http://“ or “https://“ (that’s what the pipe character in the middle does, it’s like an either/or operator) and captures it into a “group” (that’s what the parentheses do).
([^/]+) – This looks for any character that’s not a forward slash (that’s what the stuff in the square brackets does) one or more times (that’s what the plus symbol is for) and saves it in another capturing group. This pattern is matching what we want to extract: the meaty content after the http stuff but before the first forward slash.
.* – this is a common pattern that matches zero or more of any character (i.e. the rest of the input)
Using all this, I’ve captured the hostname into capturing group 2 (the first one has the http prefix).
I just gave a high level overview, lemme know if y’all have questions.
document.location.hostname should get what you’re looking for.