Greetings. I am trying to run a shell script (using curl) to get a file list from an ftp server. I can easily get the file list, but once I have it it is quite difficult to manipulate the data in the file list. It’s formatted text, and I can not find a way to get the formatting OUT, so I can coerce it to a string, and then a simple list. I can read it as text, and use text references like ‘paragraph’, ‘word’, ’ character’, etc, but there should be a better way to evaluate each record, as using text references is incredibly inefficient and error-prone.
Here is a sample of what the text (var ‘curlResults’) looks like when pasted straight into a text field…
For example, if I use…
word -1 of paragraph 1 of curlResults
… on the text above, I get “administration”…which is good and what I want. But if I use the same reference on paragraph 3, I get “html”… which is BAD. I want to get “index.html” out of it, so I can make a nice list of all the files and directories. Likewise, I want to use the permissions information, the date, etc… all of the basic “important” file data as it might pertain to an ftp application, so I really want to use the entire file record list rather than just the file names list.
I would prefer to do something like…
set ftpList to (curlResults as list)
--> Some statements (like a repeat loop for each record) left out
set ftpPerm to item 1 of ftpList
set ftpDate to item -2 of ftpList
set ftpName to item -3 of ftpList
I have fiddled with many iterations of coersions to strings, text, list…have used all sorts of attempts at setting the delimiters and parsing the “list”…and have run out of things to try to find an answer. Just when I think I’ve successfully got the contents as a list, it spit’s out an error “NSNSUnknownKeyScriptError (7)”, which apple identifies as “an unidentified error occurred; indicates an error in the scripting support of a scriptable application, or of AppleScript Studio (or Cocoa) itself”.
Below is a bit of code reflecting what I think SHOULD work, but as I said it’s throwing errors and I’m getting frustrated…
(* This works fine, and spits out the var "curlResults" identified above *)
set theCurlString to contents of text field "curlCommand" of window "main"
set theUserPass to ("-u " & curlUser & ":" & curlPass & " ") as string
set theCommand to ("curl " & theUserPass & (quoted form of theCurlString))
try
set curlResults to (do shell script theCommand)
on error errorMessage number errorNumber
set contents of text field "curlResponse" of window "main" to (errorMessage & "(" & errorNumber & ")")
set curlResults to ""
end try
(* Something like this works, but doesn't always yield the correct results *)
set curlResultsList to {}
set theCount to count paragraphs of curlResults
repeat with theIndex from 1 to theCount
set curlResultsList to (curlResultsList & (word -1 of paragraph theIndex of curlResults))
end repeat
(* This does not work. I want a comma-separated list of file records *)
set oldDel to AppleScript's text item delimiters
set AppleScript's text item delimiters to (ASCII character 13) --> or "(ASCII character 10)"
set curlResultsList to (curlResults as string) as list
set AppleScript's text item delimiters to oldDel
--> Then I want to further process each record/line as a list here
Note: some of this code I rewrote by hand because my test code was a huge mess. If you cut and paste code I claim “works” and it does not, it’s probably my fault for not typing it in correctly…sorry. As I said, I can not successfully make each record (or the entire “list” of records) into a true applescript list. Any help or solutions are greatly appreciated. Thanks,
j