• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Creating/saving/appending to files the Cocoa way
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Creating/saving/appending to files the Cocoa way


  • Subject: Re: Creating/saving/appending to files the Cocoa way
  • From: Bill Hernandez <email@hidden>
  • Date: Sun, 17 Jan 2010 22:20:07 -0600

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.

Robert,

( 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

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
applescriptobjc-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Creating/saving/appending to files the Cocoa way
      • From: Robert DuToit <email@hidden>
References: 
 >Re: Creating/saving/appending to files the Cocoa way (From: Shane Stanley <email@hidden>)
 >Re: Creating/saving/appending to files the Cocoa way (From: Robert DuToit <email@hidden>)

  • Prev by Date: Re: NSSavePanel oddity
  • Next by Date: Re: NSSavePanel oddity
  • Previous by thread: Re: Creating/saving/appending to files the Cocoa way
  • Next by thread: Re: Creating/saving/appending to files the Cocoa way
  • Index(es):
    • Date
    • Thread