When when I import the text file, the folder path does not include : or / in the paths?
I have a text file that has the following information.
Now, I import it with the following.
set filePath to (path to current user folder) & "Library:Preferences:com.Backup.folders" as string
set theFile to open for access filePath
set theData to read theFile as string
close access theFile
set theContext to paragraphs of theData
repeat with theLine in theContext
--Parse the text
set theName to first word of theLine as string
set thePath to words 2 thru -1 of theLine as string
display alert thePath
end repeat
I would think the easiest thing to do would be to use the equivalent of an String-to-Array function like split(ECMA Script) or explode (PHP)…but does AppleScript have such a thing? If it does, then you could split each line into a list at the comma, and the first item in the list would be the “key” and the second item in the list would be the value.
But my relative ignorance of AppleScript prevents me from know if there such a command, and what it’s called if it’s there. I looked through the docs a little, but no luck. So, sorry! I’d love to know if AS has that command or not, so if someone happens to know, please post!
But wait a minute…someone with more experience check me on this, but if you’ve got “words,” then you have a list of words, right? Of course, you’re casting it as a string right away, but you can set AppleScript’s text item delimiters to a custom value, like “:”, and then cast the list as text, and it’ll have the “:” in between each word.
on listAsString(theList, theDelimiter)
set textItemDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {theDelimiter}
set returnString to theList as text
set AppleScript's text item delimiters to textItemDelimiters
return returnString
end listAsString
That might fall apart if you have a two-word folder, though…
Nope. Unfortunately Applescript’s string commands are the only thing I don’t like about it. I work with about 5 other scripting languages and PHP is my favorite by far.
I made a recursive string replacement. On the table view I show a forward slash but in the text file is a hyphen.
drukepple, did you learn how to use the table view yet. It is actually very simple but I forgot to make the connection to the data source.
As long as you only have the one comma, something like this should work:
get "Documents,Maggie:Users:PaulAvery:Documents
Movies,Maggie:Users:PaulAvery:Movies
Music,Maggie:Users:PaulAvery:Music
Pictures,Maggie:Users:PaulAvery:Pictures"
explode(ASCII character 10, result)
repeat with thisItem in result
set {thisName, thisPath} to {first item, second item} of explode(",", thisItem)
-- log result
-- whatever else
end repeat
on explode(someSeperator, someInput)
if someSeperator is "" then return false
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {someSeperator}
set someInput to text items of someInput
set AppleScript's text item delimiters to ASTID
return someInput
end explode
Bruce: awesome, that’ll save me some agony when I need an explode function!
Peejavery: Tables…kinda sorta. It’s slow going, because I just don’t have a lot of time to sit down and work on this stuff…believe me, I’m at a point where I’d certainly rather learn AS Studio (and eventually Cocoa proper) than work my day job, but reality is I can only work in small doses. i do know about datasources, but I’m wondering if my ineptitude in general with AS Studio has mucked something up in that regard…
Bruce, why will that only work if you only have one comma? I must be not understanding the way AppleScript is treating the “set someInput to text items of someInput” line.
set test to "Junk,Drive:Users:PaulAvery:Junk, Blah, Etc:"
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {","}
set test to text items of test
set AppleScript's text item delimiters to ASTID
return test
--> {"Junk", "Drive:Users:PaulAvery:Junk", " Blah", " Etc:"}
That affects the path too, which is probably not what you wanted.
Thanks for the explanation Bruce…but that’s exactly what I’d expect to happen in an “explode” function. I wouldn’t expect the function to be smart enough to know that if should only affect commas that aren’t part of the path or some such exception. I would expect the function to find every comma (or whatever you feed it) and use that as a delimter when turning bits of a string into elements of a list. I would also expect me as the developer to find a delimiter that can work reliably. That is, if I expect a comma to be part of the data I was to store, then I shouldn’t use it as a deliminter when I store the text file.