1

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?

Jesse Barnum
  • 6,507
  • 6
  • 40
  • 69

2 Answers2

3

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.


  • 1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed 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.

user3439894
  • 7,266
  • 3
  • 17
  • 28
  • That is odd, since scripting additions are supposed to be available by default _unless_ there are `use` commands. My Script Debugger template uses that since it is set up for AppleScriptObjC, so it working appears to have been an accident. Looks like those `use` statements might be something to add for a default Script Editor document. Nice detective work. – red_menace Aug 20 '21 at 23:43
  • @red_menace, RE: "That is odd, since scripting additions are supposed to be available by default unless there are use commands." -- I agree, but the proof, in the express use case of the _code_ presented in the OP, is in the adding of `use scripting additions`, which definitely resolved the issue. – user3439894 Aug 20 '21 at 23:51
  • I agree with your solution, it is just that when there are no `use` commands, coercing to `URL` (which is also a part of the StandardAdditions dictionary) works, which would indicate that scripting additions are being loaded, so it is a bit odd that explicitly declaring the use of scripting additions would include something more (or different) than the default. I think I will be doing that in the future, though. – red_menace Aug 21 '21 at 00:18
  • @red_menace, Maybe it's a _fluke_ or a _bug_ in this particular use case. The fact that **Script Debugger** also, in this use case, requires `use scripting additions` for the _example_ **AppleScript** _code_ in the OP to work confirms it's what it takes to make it work if you don't use the `run script` _command_ as in the other answer. (Which IMO in this use case is totally unnecessary.) – user3439894 Aug 21 '21 at 00:35
  • Looks like an AppleScript bug. Take it easy, user@3439894. When I see that there is a better solution than mine, I delete my solution and use the better one. I am not mentally ill. – Robert Kniazidis Aug 21 '21 at 01:03
  • I will even give you an upvote for your working solution, and I will not lie like you that someone's working solution does not work. – Robert Kniazidis Aug 21 '21 at 01:13
  • No, I changed my mind to delete my answer. In order not to provide a service to dishonest people, and so that users can see what is happening here. – Robert Kniazidis Aug 21 '21 at 01:53
  • This question shouldn't require much more than a line of code and maybe a sentence or two describing the issue. Is everything here necessary to answer the OP? – Mockman Aug 22 '21 at 12:33
  • 1
    Thank you for your thorough answer, I added the 'use scripting additions' to the top of my example script and it immediately fixed the issue. I am on Big Sur 11.3.1. – Jesse Barnum Aug 22 '21 at 15:24
0

Yes, it conflicts with some other path property («property ppth»). Use instead following. After compiling the code it will reference to proper path property:

set theURL to "http://apple.com/myfile" as URL
set c to «property FTPc» of theURL

Or, in the following form (this form allows you to deliver already compiled code to other users, as is):

set URLpath to run script "set theURL to \"http://apple.com/myfile\" as URL
set d to «property FTPc» of theURL"
Robert Kniazidis
  • 1,760
  • 1
  • 7
  • 8
  • If one copies and pastes the first _block_ of _code_ in your answer to **Script Editor** and runs it, it automatically compiles `set d to «property FTPc» of theURL` to `set d to path of theURL` and fails with: `error "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\"}}." number -1728 from 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"}}` – user3439894 Aug 20 '21 at 22:03
  • RE: "Yes, it conflicts with some other path keyword." -- I do not believe that is why it's failing because e.g. `return path in {path:"http://apple.com/myfile"}` returns `"http://apple.com/myfile"` I think issue is with the escaped _double-quotes_ in `path:\"http://apple.com/myfile\"` of what's returned by the first _block_ of _code_, – user3439894 Aug 20 '21 at 22:08
  • Your code works with Script Debugger, but not Script Editor, so this is looking like a Script Editor problem. – red_menace Aug 20 '21 at 22:18
  • @red_menace RE: "Your code works with Script Debugger, but not Script Editor, so this is looking like a Script Editor problem" -- Well that's good to know, however, **Script Debugger** is a paid app that cost $99.99 USD and as **Script Editor** is the **macOS** default, IMO answers posted should work with the **Apple** default products or it should be so noted in the answer that the _code_ will not compile or run properly in **Script Editor**. – user3439894 Aug 20 '21 at 22:24
  • @user3439894 - Testing with the built-in Script Editor is always a good idea, but Script Debugger has a free lite version that still trounces it. – red_menace Aug 20 '21 at 22:31
  • @red_menace, I figured out what was wrong with how the OP was using the _code_ in **Script Editor**. See my answer, thanks. BTW Even in **Script Debugger** it changes `set d to «property FTPc» of theURL` to `set d to path of theURL` and without `use scripting additions` it too fails the same way as with **Script Editor** without `use scripting additions`. – user3439894 Aug 20 '21 at 23:10