Sunday, November 27, 2022

#1 2022-05-06 08:03:09 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1511

Make array of URLs from an array of POSIX paths

Is there a key for use with valueForKey which will convert an array of POSIX paths to an array of URL's? For example,

Applescript:

set theFiles to {"/Users/Robert/Working/New Text File 1.txt", "/Users/Robert/Working/New Text File 2.txt", "/Users/Robert/Working/New Text File 3.txt"}
set theFiles to current application's NSArray's arrayWithArray:theFiles
set theURLs to theFiles's valueForKey:"fileURL"


2018 Mac mini - macOS Monterey - Script Debugger 8

Online

 

#2 2022-05-06 09:46:14 pm

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

Re: Make array of URLs from an array of POSIX paths

Alas, no.

My BridgePlus script library has a command for it, though:

Applescript:

use scripting additions
use framework "Foundation"
use script "BridgePlus"
load framework

set theResult to current application's SMSForder's URLsFrom:{path to desktop, "/Applications", "~/Documents"}


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

Offline

 

#3 2022-05-07 07:44:00 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1511

Re: Make array of URLs from an array of POSIX paths

Thanks Shane.


2018 Mac mini - macOS Monterey - Script Debugger 8

Online

 

#4 2022-05-07 10:11:06 am

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

Re: Make array of URLs from an array of POSIX paths

After reading the wikipedia of URL's https://en.wikipedia.org/wiki/File_URI_scheme
Maybe this is what you want...

Applescript:

use scripting additions
use framework "Foundation"

set theFiles to {"/Users/Robert/Working/New Text File 1.txt", "/Users/Robert/Working/New Text File 2.txt", "/Users/Robert/Working/New Text File 3.txt"}
set theFiles to current application's NSArray's arrayWithArray:theFiles
set theURLs to theFiles's valueForKey:"fileURL"

set urlList to {}
repeat with anURL in theURLs
   set the end of urlList to anURL's |description|() as text
end repeat
return urlList


Node-RED makes it easy to automate IoT

Offline

 

#5 2022-05-07 10:32:56 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1511

Re: Make array of URLs from an array of POSIX paths

Fredrik71. Thanks for looking at my script and for the suggestion.

I was hoping to find a solution which did not use a repeat loop, and I thought valueForKey might work, which wasn't the case. FWIW, a solution that uses a repeat loop might be:

Applescript:

use framework "Foundation"
use scripting additions

set theFiles to {"/Users/Robert/Working/New Text File 1.txt", "/Users/Robert/Working/New Text File 2.txt", "/Users/Robert/Working/New Text File 3.txt"}
set theFiles to current application's NSArray's arrayWithArray:theFiles
set theURLs to current application's NSMutableArray's new()
repeat with aFile in theFiles
   set theURL to (current application's |NSURL|'s fileURLWithPath:aFile)
   (theURLs's addObject:theURL)
end repeat

theURLs

BTW, the simple solution in most cases is to write the script so that URLs rather than paths are employed. I was just curious if valueForKey might work. Thanks again.

Last edited by peavine (2022-05-07 10:38:55 am)


2018 Mac mini - macOS Monterey - Script Debugger 8

Online

 

#6 2022-05-07 11:17:47 am

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

Re: Make array of URLs from an array of POSIX paths

If you look at my code you also see I use |description| its from Objective-C to return
a string. I have use it many times to debug url's or dictionary. In other words your array
of POSIX's items is all file URLs after valueForKey but when convert it to AS list it become
something else.

https://developer.apple.com/documentati … guage=objc

My code show that, Its my understanding its AS that convert it to wrong type or something else.

So I did some more research... without repeat loop.
https://developer.apple.com/documentati … guage=objc

Applescript:

use scripting additions
use framework "Foundation"

set theFiles to {"/Users/Robert/Working/New Text File 1.txt", "/Users/Robert/Working/New Text File 2.txt", "/Users/Robert/Working/New Text File 3.txt"}
set theFiles to current application's NSArray's arrayWithArray:theFiles
set theURLs to theFiles's valueForKey:"fileURL"
set thePList to (theURLs's |description|())'s valueForKey:"propertyList"
return thePList as list

LIKE TO POINT OUT: After a reboot the code above didn't work (Mojave)
PS. So I guess my point is useless

Last edited by Fredrik71 (2022-05-08 03:43:11 am)


Node-RED makes it easy to automate IoT

Offline

 

#7 2022-05-07 03:05:26 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1511

Re: Make array of URLs from an array of POSIX paths

Fredrik71. I didn't understand the purpose of your script--thanks for the explanation.


2018 Mac mini - macOS Monterey - Script Debugger 8

Online

 

#8 2022-05-08 12:22:59 am

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

Re: Make array of URLs from an array of POSIX paths

peavine wrote:

...I didn't understand the purpose of your script


I thought your question was: Is there any key value coding for file URL to convert an array of POSIX paths

1. I show you it was, but its ->AS that change the property to something else.

2. The instance property description show something else ex. file:///

3. To store the values as property list its possible for AS to convert the right key value coding to the right property

Ask yourself why do the instance property description show something else compare to NSArray -> ASList.

I did what you asked and also did it without repeat loop, do you not agree.

Applescript:

use scripting additions
use framework "Foundation"

set theURL to current application's |NSURL|'s fileURLWithPath:"/Users/Robert/Working/New Text File 1.txt"
log (theURL's valueForKey:"fileURL") as boolean
log theURL's |description|() as text

Last edited by Fredrik71 (2022-05-08 12:49:31 am)


Node-RED makes it easy to automate IoT

Offline

 

#9 2022-05-08 08:07:22 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1511

Re: Make array of URLs from an array of POSIX paths

Fredrik71. My original question was:

Is there a key for use with valueForKey which will convert an array of POSIX paths to an array of URL's?



I tested all of your suggestions multiple times, and none of them return an array of URLs. More specifically,

* Your script in post 4 returned the error: "[<__NSCFString 0x600002ce0480> valueForUndefinedKey:]: this class is not key value coding-compliant for the key fileURL."

* Your script in post 6 returned the error: "[<__NSCFString 0x600002ca27c0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key fileURL."

* Your script in post 8 returned true and a single URL.

So, as far as I can tell, none of your suggestions use the valueForKey method to convert an array of POSIX paths to an array of URLs.


2018 Mac mini - macOS Monterey - Script Debugger 8

Online

 

#10 2022-05-08 11:25:58 pm

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

Re: Make array of URLs from an array of POSIX paths

Let me be clear I use Mojave and I did test the code many times and it worked. After a reboot it no longer worked. I later return to the code and it worked again.

I couldn’t explain it more and share it

It looks like consistency is not the greatest part in that code.

Also I used Script Editor


Node-RED makes it easy to automate IoT

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)