Sunday, September 27, 2020

#1 2020-05-19 03:01:50 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 393

path, absoluteString, URLWithString, NSURL

Doing some example from Objective-C to ASObjC


Would it be correct to assume the pointer to file will use |path| as return
and when use remote URL we use absoluteString as return...

Here is the code:

Applescript:

use framework "Foundation"
use scripting additions

set theFile to "file:///Users/username/Desktop/DummyFile.txt"
set theURL to current application's NSURL's URLWithString:theFile
log theURL's className() as text
log theURL's absoluteString as text
log theURL's |path| as text

set theHTML to "http://www.apple.com"
set theURL to current application's NSURL's URLWithString:theHTML
log theURL's className() as text
log theURL's absoluteString as text
log theURL's |path| as text

Regards.


I could teach you to cook but I couldn't do anything if you do not have desire or commitment for it.

Offline

 

#2 2020-05-19 06:04:56 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6460

Re: path, absoluteString, URLWithString, NSURL

Path is only meaningful for file UTLs. If you look up the fileURL method, you'll see something like, "If this property’s value is YES, then the receiver’s path property contains a suitable value for input into NSFileManager or NSPathUtilities."


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2020-05-20 02:15:30 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 393

Re: path, absoluteString, URLWithString, NSURL

Thanks Shane...

Did some more examples... after I read the Apple references.

Applescript:

use framework "Foundation"
use scripting additions

set theFile to "file:///Users/username/Desktop/DummyFile.txt"
set theURL to current application's NSURL's URLWithString:theFile
-- log theURL's className() as text --> NSURL
-- log theURL's absoluteString as text --> file:///Users/username/Desktop/DummyFile.txt
log "URLWithString: " & theURL's |path| as text

set theHTML to "http://www.apple.com"
set theString to current application's NSURL's alloc's initWithString:theHTML
-- log theString's className() as text --> NSURL
log "initWithString: " & theString's absoluteString as text --> http://www.apple.com

set theFile to "~/Desktop/DummyFile.txt"
set theURL to current application's NSString's stringWithString:theFile
set theURL to theURL's stringByExpandingTildeInPath()
-- log theURL's className() as text --> NSPathStore2
log "stringByExpandingTildeInPath: " & theURL as text

set theURL to current application's NSString's stringWithString:theFile
set theURL to theURL's stringByResolvingSymlinksInPath()
-- log theURL's className() as text --> NSPathStore2
log "stringByResolvingSymlinksInPath: " & theURL as text --> /Users/f.gustafsson.user/Desktop/dummyFile.txt

set theFile to "/Users/username/Desktop/DummyFile.txt"
set theURL to current application's NSURL's fileURLWithPath:theFile
set theURL to current application's NSURL's URLByResolvingAliasFileAtURL:theURL options:0 |error|:(missing value)
-- log theURL's className() as text --> NSURL
-- log theURL's absoluteString as text --> file:///Users/username/Desktop/DummyFile.txt
log "URLByResolvingAliasFileAtURL: " & theURL's |path| as text

set theFile to "/Users/username/Desktop/DummyFile.txt"
set theURL to current application's NSURL's fileURLWithPath:theFile
-- log theURL's className() as text --> NSURL
-- log theURL's absoluteString as text --> file:///Users/username/Desktop/DummyFile.txt
log "fileURLWithPath: " & theURL's |path| as text

set theHTML to "http://www.apple.com"
set theURL to current application's NSURL's URLWithString:theHTML
-- log theURL's className() as text --> NSURL
log "URLWithString: " & theURL's absoluteString as text
-- log theURL's |path| as text --> missing value

set theFiles to {"/Users/username", "Desktop", "DummyFile.txt"}
set theURL to current application's NSURL's fileURLWithPathComponents:theFiles
-- log theURL's className() as text --> NSURL
-- log theURL's absoluteString as text --> file:///Users/username/Desktop/DummyFile.txt
log "fileURLWithPathComponents: " & theURL's |path| as text --> /Users/username/Desktop/DummyFile.txt


I could teach you to cook but I couldn't do anything if you do not have desire or commitment for it.

Offline

 

#4 2020-05-20 11:04:56 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11697
Website

Re: path, absoluteString, URLWithString, NSURL

An URL is an object which contains several components.

For example https://api.openweathermap.org/data/2.5 … t?q=London has the following components

- scheme: https://
- host: api.openweathermap.org
- path: /data/2.5/forecast
- query: q=London

And there are others like user, password, port and fragment.

There are two major initializers: URLWithString and fileURLWithPath, the former is for string representations of an URL including the scheme, the latter for file system URLs represented only by the path. You can even create file system URLs with URLWithString if the string contains the scheme file:// like the URL property in Finder and System Events. File system URLs don't contain host and query but can contain user and password

The corresponding APIs to get the string representation from an URL are absoluteString and path. The former returns the entire (percent encoded) string including the scheme, the latter only the path component.

Last edited by StefanK (2020-05-20 11:05:42 pm)


regards

Stefan

Offline

 

#5 2020-05-21 01:34:08 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 393

Re: path, absoluteString, URLWithString, NSURL

Thanks Stefan for your words.

It may be true that the only great book about ASObjC is Shanes book...
So I thought, if I could understand Objective-C and able to translate examples to ASObjC.

The learning could be better I think.

Shane gives clues on this in this book.

Regards.


I could teach you to cook but I couldn't do anything if you do not have desire or commitment for it.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)