I tried to create an AppleScript that reads a text file and puts the contents into a list. The file is a simple text file where every line looks like this: example-"example"
The first is a filename and the other is a folder name.
Here is my code now:
set listOfShows to {}
set theFile to readFile("/Users/anders/Desktop/test.txt")
set Shows to read theFile using delimiter return
repeat with nextLine in Shows
if length of nextLine is greater than 0 then
copy nextLine to the end of listOfShows
end if
end repeat
choose from list listOfShows
on readFile(unixPath)
set foo to (open for access (POSIX file unixPath))
set txt to (read foo for (get eof foo))
close access foo
return txt
end readFile
When I run that the output I get this:
error "Can not change \"Game.of.Thrones-\\\"Game Of \" to type file." number -1700 from "Game.of.Thrones-\"Game Of " to file"
My list looks like this: Game.of.Thrones-"Game Of Thrones" and two more lines like that.
The error is that you are trying to read the contents of a file (the first file you read) as a file. Getting the paragraphs of text will break it apart at return/linefeed boundaries, which usually works better than trying to guess what end of line character(s) were used in the file.
You also don't need the whole open for access thing when just reading files, so your script can be reduced to just
set listOfShows to {}
set Shows to paragraphs of (read POSIX file "/Users/anders/Desktop/test.txt")
repeat with nextLine in Shows
if length of nextLine is greater than 0 then
copy nextLine to the end of listOfShows
end if
end repeat
choose from list listOfShows
read uses MacRoman by default, so it jumbles up non-ASCII characters in UTF-8 files unless you add as «class utf8». (as Unicode text is UTF-16.)
paragraphs of (read POSIX file "/tmp/test.txt") as «class utf8»
paragraphs of also works with CRLF and CR line endings. This doesn't, but it ignores the last line if it's empty:
read POSIX file "/tmp/test.txt" as «class utf8» using delimiter linefeed
set milefile to ((path to desktop as text) & "Alert.txt")
set theFileContents to (read file milefile)
display dialog theFileContents
AppleScript’s Language Reference states on page 120:
A series of characters beginning immediately after either the first character after the end of the preceding paragraph or the beginning of the text and ending with either a carriage return character (\r), a linefeed character (\n), a return/linefeed pair (\r\n), or the end of the text. The Unicode "paragraph separator" character (U+2029) is not supported.
So, U+8232 is ignored and AppleScript returns the whole text from the file…
U+8232 is used in TextEdit as the CR character…
Related
I am wondering from where newline (4th in example code) is written out from following very simple tcl code. Handling from puts -nonewline is cumbersome. Is there any other tcl command influence this behavior?
set fid [open testout.txt w]
puts $fid 1
puts $fid 2
puts $fid 3
close $fid
Output:
#1:1
#2:2
#3:3
#4:
The puts command always appends a newline to the end of what you ask it to write, unless you pass the -nonewline option. It is a feature of that command, and is most of the time what you tend to want. (The puts command is the only standard Tcl command that writes data to a channel; chan puts is just a different name for the same thing.)
In your case, maybe you don't want the newline at the end of the final line (and should use the option). Or maybe you want to trim the newline from the end before splitting the text into lines when reading it back in. Whether you can tolerate that newline character at the end of the text data in the file depends on what you're doing with it.
Extra space character appears using pipe (|) and stdout(>) in the same line in a batch file (in Windows)
echo sampletext>outfile|echo text
- outfile consists of an extra space (#20) character in the first line
echo sampletext>outfile2
echo text
- works correct, but I need to make two operations per line.
Is there any ideas how to cut this space character?
I am using AutoIt to write a text file. The only problem I am having is that it adds an extra line at the end (there is absolutely nothing on it). I would like to remove the line.. Either by never adding it or deleting it after writing everything that I need.
Example:
_FileCreate($file)
FileOpen($file, 0)
FileWriteLine($file,$line)
FileClose($file)
Just use FileWrite($file, $line) instead of FileWriteLine($file, $line) as the latter will always make sure, that the line ends with a carriage return (#CR) or a line feed (#LF), else a DOS linefeed (#CRLF) will be added as you can read in its documentation.
If you want to write multiple lines into the file with only carriage returns in between the lines, you'd either have to manually always check whether a further line will follow and only then write an extra carriage return to the file. Or you could write each line to a single array entry and in the end join them all together with _ArrayToString($lines, #CR). You'd have to #include <Array.au3> before to be able to use this function...
hi all
Suppose we have a text file (file1.txt)
file1.txt contains many words and spaces and enter characters (cR+LF).
I wanna to replace a specific word that follows with an enter character and replace it with only that word. I mean eliminating cr+lf character.
How ?
Thank you
i assume you're asking about how to do it programmatically.
LF and CR are characters and as such they have an ascii code assigned (10,13). you'll need to load the text file, copy it to a new buffer word by word and whenever you encounter the word you want to replace - check whether it is followed by 10,13 and just don't copy those characters if so.
then write the new buffer back to the file.
Use of regular expressions should make short work of this:
replace word\r\n with word
How this is exactly done depends on your environment / editor / tools. You mentioned cf + lf, which hints that you're using Windows.
If you use Notepad++ for example, it has builtin regex support and you can use these facilities to obtain your goal.
Update: I have tried this variant it works:
Download Vim for Windows.
Open your file in Vim.
In it, issue the following command:
%s/\v([[:digit:]]+NPN[[:alpha:]]+)\n/\1 /g
Explanation:
%s - work for all lines
\v - easier regex manipulation regarding backslashes
([[:digit:]]+NPN[[:alpha:]]+) - match some digits, then NPN, then letters and capture this
\n - match end of line
\1 - replace everything with first group and two spaces
g - do this many times for each line (this is basically optional)
If you want to convert CRLF to LF:
sed 's/.$//' # assumes that all lines end with CR/LF
If you want to remove CRLF altogether
cat file1.txt | tr '\n' ' ' # join the lines with a space
cat file1.txt | tr -d '\n' # join the lines without a space
You might have to convert the line endings to unix (CRLF to LF) first and then do the translation.
i m reading from file line by line but when i read some garbage character like space /r is being added i m nt getting why it is being added although there is no such character in file from where i m reading ..i have used fread and fgets both from both i m getting the same problem please reply if u have solution for this problem
The file was probably edited/created on Windows. Windows uses \r\n as a line delimiter. When you read the file, you must strip the \r manually. Since most editors treat \r\n as a single character (line end), you can't "see" it but it's still in the file. Use a hex editor if you want to see it or a tool like od.
Open the file in text mode.
/* ... */
fopen(filename, "r"); /* notice no 'b' in mode */
/* ... */
Supposing you're on Windows ... on reading operations, the library is responsible for translating the literal "\r\n" present on disk to "\n"; and on writing operation, the library translates "\n" to "\r\n".