hi,
jon was so kind to help me out with this script:
property search_strings : {“ORIGINX 0”} --add to these lists if there are more things to search & replace
property replace_strings : {“ORIGINX 32”}
property chunk_size : 512 – this will read the first 512 characters of the file, if you need more, adjust as necessary.
on run
open {choose file of type {“TEXT”}}
end run
on open the_files
repeat with this_file in the_files
try
set this_file to this_file as string
set {item_name, container_name} to (my get_name_and_container(this_file))
set {item_name, item_extension} to (my get_name_and_extension(item_name))
if item_extension is not “” then set item_extension to “.” & item_extension
tell application "Finder"
set creator_type to creator type of file this_file
set file_type to file type of file this_file
set name of file this_file to (item_name & "_o" & item_extension)
end tell
set original_file to (container_name & item_name & "_o" & item_extension)
set new_file to (container_name & item_name & item_extension)
set start_position to 1
repeat while start_position is not -1
set the_chunk to ""
try
set the_chunk to read file original_file from start_position to (start_position + chunk_size)
set start_position to (start_position + chunk_size + 1)
on error
try
set the_chunk to read file original_file from start_position to -1
end try
set start_position to -1
end try
if the_chunk is not "" then
repeat with i from 1 to (count of search_strings)
set the_chunk to my snr(the_chunk, (item i of search_strings), (item i of replace_strings))
end repeat
my write_to_file(the_chunk, new_file, true, creator_type, file_type)
end if
end repeat
on error error_message number error_number
set error_string to "Error: " & error_number & ". " & error_message
activate
beep 3
set the_button to button returned of (display dialog error_string buttons {"Copy to Clipboard", "OK"} default button 1 with icon 0)
if the_button = "Copy to Clipboard" then set the clipboard to error_string
end try
end repeat
activate
beep
display dialog "The operation is complete." buttons {"OK"} default button 1 with icon 1
end open
to write_to_file(this_data, target_file, with_appending, creator_type, file_type)
try
set target_file to target_file as text
set open_target_file to open for access file target_file with write permission
if with_appending = false then set eof of open_target_file to 0
write this_data to open_target_file starting at eof
close access open_target_file
try
tell application “Finder”
if creator_type is not “” then set creator type of file target_file to creator_type
if file_type is not “” then set file type of file target_file to file_type
end tell
end try
return true
on error
try
close access file target_file
end try
return false
end try
end write_to_file
on snr(the_string, search_string, replace_string)
tell (a reference to AppleScript’s text item delimiters)
set {old_atid, contents} to {contents, search_string}
set {the_string, contents} to {the_string’s text items, replace_string}
set {the_string, contents} to {“” & the_string, old_atid}
end tell
return the_string
end snr
on get_name_and_container(item_path)
set item_path to item_path as string
tell (a reference to AppleScript’s text item delimiters)
set {old_atid, contents} to {contents, “:”}
set {item_name, container_name} to {(text item -1 of item_path), “” & (text items 1 thru -2 of item_path)}
set contents to old_atid
end tell
if character -1 of container_name is not “:” then set container_name to container_name & “:”
return {item_name, container_name}
end get_name_and_container
on get_name_and_extension(the_item)
if the_item does not contain “.” then return {the_item, “”}
tell (a reference to AppleScript’s text item delimiters)
set {old_atid, contents} to {contents, “.”}
set {item_name, item_extension} to {“” & (text items 1 thru -2 of the_item), (text item -1 of the_item)}
set contents to old_atid
end tell
return {item_name, item_extension}
end get_name_and_extension
it works great! the only problem is that it takes forever with a large group of files that could be up to 50 megs each…
all i need this script to do is examine the first little chunk of the file (since the line that needs replacing is always at the beginning of the file) and then write the rest of the file back normally without scanning it all, which takes forever.
i didn’t want to keep bugging you guys for the easy answer, so i tried everything i could, but nothing worked…
much appreciated!