• 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
Read a text file
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Read a text file


  • Subject: Read a text file
  • From: Gil Dawson <email@hidden>
  • Date: Tue, 29 Jul 2014 12:25:48 -0700

Hi--

I have a sort of generic way of processing a plain text file line-by-line...

set srtFile to (choose file with prompt "Select a file:")

log "srtFile=" & name of (info for srtFile)

set srtFile to open for access srtFile  -- I suppose one should use a different variable here

repeat -- once for each line
try
set srtLine to read srtFile until return

-- do something with this line

on error errm number errn
if errn = -39 then exit repeat -- end of file
error errm number errn
end try
end repeat
close access srtFile

However, I'm running into problems:

1. Sometimes, the EOL delimiter is not return, but LF or CR/LF.
2. Sometimes, the encoding can be UTF-8, sometimes UTF-8 with BOM, sometimes UTF-16.

Is there a way for me to tell a priori what are the EOL delimiter and encoding for the file?

I can open the file in Text Wrangler and then test its text document's line breaks and encoding properties.  But I thought perhaps there's something in the native Mac/AppleScript world that could do this.  

Any suggestions?

--Gil
 _______________________________________________
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: Read a text file
      • From: Gil Dawson <email@hidden>
    • Re: Read a text file
      • From: Emmanuel LEVY <email@hidden>
    • Re: Read a text file
      • From: Christopher Stone <email@hidden>
    • Re: Read a text file
      • From: Stan Cleveland <email@hidden>
References: 
 >Encrypt/Decrypt a Text File (From: Eric Geoffroy <email@hidden>)

  • Prev by Date: Encrypt/Decrypt a Text File
  • Next by Date: Re: Read a text file
  • Previous by thread: Encrypt/Decrypt a Text File
  • Next by thread: Re: Read a text file
  • Index(es):
    • Date
    • Thread