0

Given a url (https://www.stackoverflow.com/path-to-question), I am wondering if there is a way to parse the url to get its components in AppleScript/AppleScriptObjC directly (without using e.g. Javascript) and set them as variables; those components being:

  • protocol (i.e. https or http)
  • host (e.g. www.stackoverflow.com)
  • pathname (e.g. path-to-question)

I know one can do this via Javascript using

l = window.location
protocol = l.protocol
host = l.host
path = l.pathname

What I am trying to do: What I am trying to do is, given a current tab with URL (https://www.stackoverflow.com/path-to-question), change its URL to something like (https://(MY_TEXT1)-www.stackoverflow.com_(MY_TEXT2)/path-to-question_(MY_TEXT3))

Any help with how to do this via AppleScript directly would be much appreciated

5
  • Is AppleScriptObjC allowed? Commented Aug 15, 2022 at 10:49
  • @vadian Yes, indeed it is Commented Aug 15, 2022 at 11:24
  • If you coerce a URL string to class URL, this provides some easily accessible and useful properties, e.g. the host's DNS form. It can identify most common schemes (protocols), but enumerates these using AppleScript constants, which can be annoying. But provided the URL string starts with "http://" or "https://", then word 1 will provide the scheme (without the "://"). Thus tell urlString as URL to get word 1 & "://" & the host's DNS form & "/" gives everything before the path. Commented Aug 16, 2022 at 23:05
  • @CJK Thank you for your comment. Yes that would be nice to do. I am currently trying to extract the URL within a tell current tab environment, and within it, for some reason I am not able to perform the following: set myURL to URL as URL. I am new to AppleScript and therefore am likely missing something here. Commented Aug 17, 2022 at 13:15
  • That's because Chrome (I presume it's Chrome you're using) has a property named "URL", which clashes with the AppleScript class also called "URL". Since you're inside a Chrome tell block, the compiler assumes by default that all terminology pertains to Chrome, and thus can't make sense of the instruction to coerce a value to a class that's actually a named property. You simply need to do the two steps separately: 1) A Chrome tell block: tell app "Google Chrome" to tell the active tab of the front window to set myURL to the URL; 2) Outside the block: set myURL to myURL as URL Commented Aug 18, 2022 at 3:25

1 Answer 1

2

With the help to the Foundation framework it's pretty easy

set urlString to "https://www.stackoverflow.com/path-to-question"
set urlComponents to my (NSURLComponents's componentsWithString: urlString)
tell urlComponents
    set urlScheme to |scheme|() as text
    set urlHost to |host|() as text
    set urlPath to |path|() as text
end tell

If you need to omit the leading slash in the path just write

set urlPath to text 2 thru -1 of (|path|() as text)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.