• 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
replace text in chunks
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

replace text in chunks


  • Subject: replace text in chunks
  • From: Laine Lee <email@hidden>
  • Date: Fri, 13 Dec 2002 11:09:37 -0600

This script works fine if search text and replace text are the same length.
What can I do to make it work if they have different lengths?
Or is there a better way to perform this on files larger than 32k in OS X
without using third party scripting additions?
Thanks.

property search_string : "12"
property replacement_string : "36"

set this_item to (choose file with prompt "Choose a text file:")

set open_target_file to (open for access this_item with write permission)
try
set startChunk to 1
set endChunk to 0
set myeof to get eof of open_target_file
repeat until endChunk = myeof
set endChunk to startChunk + 4024
if endChunk > myeof then set endChunk to myeof
set newText to read open_target_file from startChunk to endChunk
set newText to replace_chars(newText, search_string,
replacement_string)
write newText to open_target_file starting at startChunk
if startChunk > 1 then
set startChunk to endChunk - 10
else
set startChunk to endChunk
end if
end repeat
close access open_target_file
on error my_error
display dialog my_error
close access this_item
end try

on replace_chars(newText, search_string, replacement_string)
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to the (search_string as string)
set the item_list to every text item of newText
set AppleScript's text item delimiters to the (replacement_string as
string)
set newText to the item_list as string
return newText
set AppleScript's text item delimiters to oldDelims
end replace_chars
--
Laine Lee
email@hidden
http://www.txdirect.net/users/llee
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: replace text in chunks
      • From: Emmanuel <email@hidden>
  • Prev by Date: Re: How to check applications already opened
  • Next by Date: Keychain Scripting
  • Previous by thread: OSX pdf file creation
  • Next by thread: Re: replace text in chunks
  • Index(es):
    • Date
    • Thread