applescriptcoerce

How to convert an AppleScript URL object to text


I have a URL object in AppleScript, and I'm trying to convert it to a text object.

set theURL to "http://apple.com/myfile" as URL

--set t to theURL as text --Fails, can't coerce to text

set a to scheme of theURL --works
set b to host of theURL --works
set c to path of theURL --fails

Can’t get path of {class:URL, scheme:http URL, path:"http://apple.com/myfile", host:{class:Internet address, DNS form:"apple.com", port:80, dotted decimal form:"17.253.144.10"}}.

Clearly, from the error message, the URL object has a 'path' property. My theory is that maybe the word 'path' is reserved? Do I need to escape it somehow?


Solution

  • The example AppleScript code, shown below, was tested in Script Editor under macOS Catalina and macOS Big Sur with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.

    To resolve the issue, in Script Editor, with just using .e.g.,:

    set theURL to "http://apple.com/myfile" as URL
    
    --set t to theURL as text --Fails, can't coerce to text
    
    set a to scheme of theURL --works
    set b to host of theURL --works
    set c to path of theURL --fails
    

    Is to add at least use scripting additions to the top of the script. You might also what to add use AppleScript version "2.4" -- Yosemite (10.10) or later.

    So, in Script Editor, e.g.,:

    use AppleScript version "2.4" -- Yosemite (10.10) or later
    use scripting additions
    
    set theURL to "http://apple.com/myfile" as URL
    
    set c to path of theURL
    

    Notes:

    The is no conflict with path being a keyword (or reserved) as it it just not a keyword! See: AppleScript Keywords

    Also see the screenshot below of the Library in Script Editor for Standard Additions.

    This issue is solely due to the fact that use scripting additions must be used in the context of your example AppleScript code.

    Assuming you are running Yosemite (10.10) or later, generally speaking, it typically is safe to add the aforementioned lines to your scripts. Personally I only add them as needed.



    enter image description here



    From the Library in Script Editor (⇧⌘L) for Standard Additions:

    enter image description here

    As you can see path is a text read-only property of URL and is easily retrievable when using use scripting additions as show above.




    Update:

    To refute the false allegations made by Robert Kniazidis of me in comments under my answer, ...

    enter image description here

    ... which reference his answer at the time of my comments, here are three screen shots taken after his latest rewrite of his previously deleted answer and since the time of my comments, that confirm what I said.

    In the first screen shot I again copy and paste the first block of code into Script Editor, and as one can see it's in purple, meaning it hasn't yet been compiled.

    enter image description here

    The second screen shot shows having hit the Run button, which compiles the code and executes it. As one can clearly and plainly see, it did exactly what I previously stated, it automatically changed set c to «property FTPc» of theURL to set c to path of theURL and resulted in the error I mentioned, which is also mentioned in the OP and is the issue of the question asked!

    enter image description here

    In the third screenshot I added use scripting additions and ran the script again, resulting in the expected output and no errors as in the OP or my statements of fact that I'm being accused of lying about.

    enter image description here

    I was able to replicate these results in both macOS Catalina and macOS Big Sur on two different machines using Script Editor, the Apple default application for AppleScript, and Script Debugger, a third-party application, that is designed to be superior to Script Editor.