Hi, i am new to applescript and was wondering if anyone could help me write a script that does the following:
I would like to be able to take a TextEdit file containing a list of song names and put each line of the text document onto a seperate file of a folder (line 1 to the first file, line 2 to the second…). I have tried using apples “add to file names” script and editing it but i do not know how to call upon the seperate lines of the document.
Here’s a start, but it would really be helpful if we had a sampling of the text file you are wanting to work with. For instance, does the file have each song listed on a new line, is there text afer the songs title i.e “Lucy in the Sky with diamonds”, by William Shatner?
[This script was automatically tagged for color coded syntax by Script to Markup Code]
This is easy to do, but what do you want the files called?
I’ll assume for the moment you want to save each line in a text file that has the same name as the line itself, i.e. if the file contains:
A
B
C
you end up with three files called “A”, “B”, and “C”, and each file contains the text “A”, “B”, and “C” respectively. If that’s not right, and you can’t work it out, let me know how it should be.
set filecontents to read file "path:to:file" using delimiter {return} -- read the source file
repeat with eachLine in filecontents -- loop through the lines
try
set outputFile to open for access ("path:to:" & eachLine as text) with write permission -- create/open the output file
set eof outputFile to 0 -- clear out the existing contents
write eachLine to outputFile -- write the data
close access outputFile -- close the file when done
end try
end repeat
Good one Andrew, does using “using delimiter {return}” avoid having to set and reset TID’s? I’ve never seen anyone use that technique for Text Item Delimiters (TID’s) before. Interesting indeed!
Using the ‘delimiters’ parameter to ‘read file’ doesn’t affect the TIIDs at all (at least, not for the rest of the script is concerned), so you don’t need to store/restore TIDs.
This technique actually processes the file as it’s read in, which also saves memory. Normally, you’d read the file, then convert it to a list using TIDs, so you have two copies of the file contents in memory. Specifying the field delimiter during read saves you one of those copies (as well as a significant amount of time).
The problem with the method of using return as a delimiter when reading text files (as discussed in other threads) is that you don’t always know the encoding of a file and this may not work. You can’t reliably use ASCII Character 10 or ASCII Character 13 either. I find a slightly modified version of what Greg posted above to be preferable because the paragraph property of text doesn’t care what the line ending delimiter is. Also, again, you don’t need to wrap file I/O commands in a tell block for any particular app, Standard Additions can handle it all on its own:
Almost. You’re right that it take a list but I believe the list is limited to just two delimiters (am I wrong?) and, if so, this can’t account for all the line endings available (Mac, DOS, Unix), and so the paragraphs property seems to work best for me with the least amount of fuss.
You’re right, jonn. read file only accepts two delimiters (I’d forgotton that one).
However, I still think it has its place. It is still easier to do this than it is to save/set/restore TIDs.
Also, the fact that it accepts two delimiters makes it a better choice than TIDs which, by its nature, only supports one.
When using a single delimiter, ‘using delimiters’ is faster than ‘paragraphs of’ (although ‘using delimiter {return, ascii character 10}’ is slower than ‘paragraphs of’).
One other drawback of ‘using delimiters’ is that it only accepts two single-character delimiters. You cannot read file using delimiters of “hello”, for example - the file will be delimited on every occurrence of the letter ‘h’, regardless whether it’s a ‘hello’ or a ‘href’. TIDs, on the other hand, can deal with strings rather than characters.
So I guess which one to use depends on your situation.
When dealing with text files of unknown origin, ‘paragraphs of’ has the ability to automatically distinguish between different line endings.
If using two delimiters, or some character other than return or linefeed then ‘using delimiters’ seems to have the edge.
If using strings rather than individual characters, TIDs is your best bet.
Nice summation. I never understood, however, why TIDs’ input is actually a list (set AppleScript’s text item delimiters to {“,”}) but it accepts a string (set AppleScript’s text item delimiters to “,”) and multiple items of a list are ignored–only the first is used (set AppleScript’s text item delimiters to {“,”, tab} uses the comma only). Anyone know why this is so?