• 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: Read for 1000 until nearest space? - Almost
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Read for 1000 until nearest space? - Almost


  • Subject: Re: Read for 1000 until nearest space? - Almost
  • From: Philip Aker <email@hidden>
  • Date: Tue, 14 Oct 2008 13:21:14 -0700

On Oct 14, 2008, at 12:13 PM, Joe wrote:

I have a problem though with writing the last piece to a file.

I think that before the 'read until space' you'll probably need a test for 'get eof' and then a compare for the accumulated length or use an embedded try block.

Another "sketch" is below (not stress tested, will need checking). All in all, a better approach might be by reading lines and then testing to see if the accumulated length is less than 1000. 

set p2d to (path to desktop folder as text)
set fpath to p2d & "aaa.txt"
set read_size to 1000
set accumulated to 0

try
set fd to open for access fpath
set read_limit to (get eof of fd)
repeat
set d2 to ""
set d1 to read fd for read_size
set accumulated to accumulated + (length of d1)
if (accumulated is less than read_limit) then
set d2 to read fd until space
set accumulated to accumulated + (length of d2)
end if
set res to d1 & d2
-- do something with ->
--{res, length of res}
if (accumulated is equal to read_limit) then
exit repeat
end if
end repeat
close access fd
on error
try
close access fd
end try
end try






This script does read the last piece of a file (as evidenced
by turning on logging) but it doesn't write the last chunk to
a file as it does previous chunks. It seems the error I'm
trapping gets thrown before the last write is done, but I
can't find a way to fix it.

--My script
tell application "Finder"
set myfile to the selection as alias
set flname to name of myfile
end tell
set filcnt to 0
set fr to open for access myfile
try
repeat
 set filcnt to filcnt + 1
 set rd1 to read fr for 2000
 set rd2 to read fr until space
 set chunx to (rd1 & rd2) as string
 set nfr to open for access file ((path to desktop folder as string) & filcnt & " " & flname) with write permission
 write chunx to nfr
 close access nfr
end repeat
on error
try
 close access fr
end try
end try

Philip Aker
echo email@hidden@nl | tr a-z@. p-za-o.@

Democracy: Two wolves and a sheep voting on lunch.

 _______________________________________________
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 for 1000 until nearest space? - Almost
      • From: Joe <email@hidden>
References: 
 >Re: Read for 1000 until nearest space? - Almost (From: Joe <email@hidden>)

  • Prev by Date: Re: Read for 1000 until nearest space? - Almost
  • Next by Date: Re: Read for 1000 until nearest space? - Almost
  • Previous by thread: Re: Read for 1000 until nearest space? - Almost
  • Next by thread: Re: Read for 1000 until nearest space? - Almost
  • Index(es):
    • Date
    • Thread