• 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: Mike Hurley <email@hidden>
  • Date: Mon, 18 Jan 2010 11:00:23 -0500

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

Bill,

In ASOC echo -n no llonger works to suppress the final newline.
Apparently there was some change in  the way that echo is implemented in the shell used by
do shell script. There is a "\c" variant that works in its place, once you figure out the correct number of
times to escape the backslash. For example
set thescript to ("echo " & "ABC" & "\\\\c > afile ")
log thescript
               --> echo ABC\\c > afile
do shell script thescript
set thescript to ("echo " & "ABC" & "\\\\c >> afile ")
do shell script thescript
do shell script thescript
                --> ABCABCABC

Mike


Date: Sun, 17 Jan 2010 22:20:07 -0600
From: Bill Hernandez <email@hidden>
Subject: Re: Creating/saving/appending to files  the Cocoa way
To: ASOC List <email@hidden>
Message-ID: <email@hidden>
Content-Type: text/plain; charset="us-ascii"

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: behavior of "echo"
      • From: Christopher Nebel <email@hidden>
    • Re: Creating/saving/appending to files the Cocoa way
      • From: Bill Hernandez <email@hidden>
  • Prev by Date: Re: Creating/saving/appending to files the Cocoa way
  • Next by Date: Re: Creating/saving/appending to files the Cocoa way
  • 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