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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

9 Comments

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, 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.
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, 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.)
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.
|
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"

6 Comments

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"}}
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,
Your code works with Script Debugger, but not Script Editor, so this is looking like a Script Editor problem.
@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 - Testing with the built-in Script Editor is always a good idea, but Script Debugger has a free lite version that still trounces it.
|

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.