On Jan 17, 2010, at 7:47 PM, Robert DuToit wrote:
set somestring to "stat info"
set filePath to (POSIX path of (path to desktop)) & "newfile.txt"
set oldtext to do shell script "cat " & quoted form of filePath
set newtext to oldtext & tab & somestring
do shell script "echo " & newtext & " >" & quoted form of filePath
Look at the five examples below
Using Bash all you need to replace the five lines is :
$ echo -n $'\t'hello >> ~/Desktop/hello.txt
Craig, I used just the '>>' in my case but since he wanted a tab delineated string I appended the new string to the old one first so the new text is not on a new line. Though there is a simple way to avoid the new line with echo, I think.
( 1 ) echo -n whatever >> ~/Desktop/hello.txt
the -n prevents a newline from being added to the end of the echo argument
$ echo -n hello >> ~/Desktop/hello.txt
$ echo -n hello >> ~/Desktop/hello.txt
$ echo -n hello >> ~/Desktop/hello.txt
---> hellohellohello
( 2 ) we can easily add a space by using quotes around the text
$ echo -n "hello " >> ~/Desktop/hello.txt
$ echo -n "hello " >> ~/Desktop/hello.txt
$ echo -n "hello " >> ~/Desktop/hello.txt
---> hello hello hello
( 3 ) adding \t converts the output to t which is not what you want.
$ echo -n \thello >> ~/Desktop/hello.txt
$ echo -n \thello >> ~/Desktop/hello.txt
$ echo -n \thello >> ~/Desktop/hello.txt
---> thellothellothello
( 4 ) we CAN add a tab character by using $'\t'
$ echo -n $'\t'hello >> ~/Desktop/hello.txt
$ echo -n $'\t'hello >> ~/Desktop/hello.txt
$ echo -n $'\t'hello >> ~/Desktop/hello.txt
---> hello hello hello
( 5 ) we can add THREE tab characters by using $'\t\t\t'
echo -n $'\t\t\t'hello >> ~/Desktop/hello.txt
echo -n $'\t\t\t'hello >> ~/Desktop/hello.txt
echo -n $'\t\t\t'hello >> ~/Desktop/hello.txt
---> hello hello hello
( 6 ) However, AppleScript does not like the -n switch
set somestring to "stat info"
set filePath to do shell script "echo ~/Desktop/hello.txt"
do shell script "echo -n $'\\t' " & somestring & " >> " & quoted form of filePath
do shell script "echo -n $'\\t' " & somestring & " >> " & quoted form of filePath
---> -n stat info
---> -n stat info
Bill Hernandez
Plano, Texas