• 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: Opening and reading a text file
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Opening and reading a text file


  • Subject: Re: Opening and reading a text file
  • From: Christopher Stone <email@hidden>
  • Date: Mon, 28 Jan 2013 18:47:51 -0600

On Jan 28, 2013, at 16:41, Ed Rush <email@hidden> wrote:
I'm trying to open an existing text file, add some HTML code, and write the result to replace a pre-existing second file, but I keep getting an end-of-file error right off the bat:

--read the books list 
set booksList to read sourceFile as Unicode text using delimiter return
close access sourceFile
______________________________________________________________________

Hey Ed,

Close access not necessary, because you've never opened a file for access to begin with.

read alias ((path to documents folder as text) & "BUUF:BUUF Library:BUUF library in OO.txt") as «class utf8»

This will produce an -39 error on my machine if the file has NO content.

'read' is a Standard Additions command that has nothing to do with TextWrangler, and canonical usage these days is to place osax calls outside of application tell-blocks (as annoying as that is).

Something like this might work for you:

-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------

on writeFile(srcData, targetFile, startPosition, dataType)
try
set fileRef to open for access targetFile with write permission
if startPosition = 0 then
set eof of fileRef to 0
end if
write srcData to fileRef as dataType starting at startPosition
close access fileRef
on error eMsg number eNum
try
close access fileRef
end try
error eMsg & return & "Error Number: " & eNum
end try
end writeFile

-------------------------------------------------------------------------------------------
--» MAIN
-------------------------------------------------------------------------------------------

try


set srcFile to choose file
set srcData to read srcFile as «class utf8»
set hdrData to "<HEADER CODE>" & return
set destFile to choose file
set destData to hdrData & srcData
writeFile(destData, destFile, 0, «class utf8»)


on error eMsg number eNum
set e to eMsg & return & return & "Num: " & eNum
tell me to set dDlg to display dialog e with title "ERROR!" buttons {"Cancel", "Copy", "OK"} default button "OK"
if button returned of dDlg = "Copy" then set the clipboard to e
end try

-------------------------------------------------------------------------------------------

You don't need to read with a delimiter unless it is neither CR or LF.

Using choose file to do this piecemeal seems mighty inefficient.  Are you doing this for testing, or is this genuinely a necessary part of your workflow?

If you give us a little more information we might be able to help streamline things a bit.

--
Best Regards,
Chris

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Opening and reading a text file
      • From: Emmanuel LEVY <email@hidden>
    • Re: Opening and reading a text file
      • From: Ed Rush <email@hidden>
References: 
 >Opening and reading a text file (From: Ed Rush <email@hidden>)

  • Prev by Date: Opening and reading a text file
  • Next by Date: Re: Opening and reading a text file
  • Previous by thread: Opening and reading a text file
  • Next by thread: Re: Opening and reading a text file
  • Index(es):
    • Date
    • Thread