You are not logged in.
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
Offline
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
Thanks Shane.
2018 Mac mini - macOS Monterey - Script Debugger 8
Offline
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
if you are the expert, who will you call if its not your imagination.
Offline
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
Offline
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)
if you are the expert, who will you call if its not your imagination.
Offline
Fredrik71. I didn't understand the purpose of your script--thanks for the explanation.
2018 Mac mini - macOS Monterey - Script Debugger 8
Offline
...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)
if you are the expert, who will you call if its not your imagination.
Offline
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
Offline
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
if you are the expert, who will you call if its not your imagination.
Offline