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.
httpsorhttp) - 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
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://", thenword 1will provide the scheme (without the"://"). Thustell urlString as URL to get word 1 & "://" & the host's DNS form & "/"gives everything before the path.tell current tabenvironment, 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."URL", which clashes with the AppleScript class also called"URL". Since you're inside a Chrometellblock, 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 Chrometellblock: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