Changing one line of text in a large batch of files

Hi, I’m new to Applescript, so I thought I’d ask the pros…
I need to change one line of text in a group of files that I end up with every day (Adobe 3.0 PS files). The line states “ORIGINX 0” and I want it to say “ORIGINX 32”.
I’m not sure how to write an Applescript for this, but I have a lot of these files at the end of the day, and I don’t want to have to open each one in SimpleText and modify them.
Any help would be MOST appreciated!
Thx!

This uses code from Apple and it should be close to what you need if the files are plain text files. I would test it thoroughly on sample/duplicate files before putting the script into a production setting. Save the script as an application (it will become a droplet) and then drop the files (not folders) onto the its icon.

on open files_
	repeat with file_ in files_
		try
			set content_ to read file_
			set content_ to my replace_chars(content_, "ORIGINX 0", "ORIGINX 32")
			my write_to_file(content_, file_, false)
		on error error_message number error_number
			set this_error to "Error: " & error_number & ". " & ¬
				error_message & return
			set the log_file to ((path to desktop) as text) & "Script Error Log"
			my write_to_file(this_error, log_file, true)
			activate
			beep 3
			display dialog "An error report (Script Error Log) has been placed on the desktop." buttons ¬
				{"OK"} default button 1 with icon 0
		end try
	end repeat
	activate
	display dialog "The operation is complete." buttons ¬
		{"OK"} default button 1 with icon 1
end open

to replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

to write_to_file(this_data, target_file, append_data)
	try
		set the target_file to the target_file as text
		set the open_target_file to ¬
			open for access file target_file with write permission
		if append_data is false then ¬
			set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access file target_file
		end try
		return false
	end try
end write_to_file

– Rob (not a pro)

you’re not a pro? i beg to differ…that worked great!

the script worked and the text (ORIGINX) was changed (ORIGINX 32), but i was only sampling with a small file (64K). i then tried a larger file (2MB) and it said “out of memory”. i know that the files can become anywhere up to 30MB’s…is there anything i can do for this? i have 256 megs of ram on this computer (accelerated G4), so i didn’t think that this would be a problem.

Any ideas?

(thanks again, rob!)

just thought of something:
although these files could end up being thousands of lines long, the variable that i need to change (ORIGINX → ORIGINX 32) is within the first 50 or 60 lines of each file.
is there a way to limit the script to only search the first part of the file (since it runs out of memory with bigger files)?
just a thought…

I’m glad that the script works at least partially. :wink:

I suspect that the script ran out of memory and not the computer. If you are running the script in OS X’s classic environment, or on a pre-OS X operating system, you might overcome the problem by assigning more RAM to the droplet. To do this, select the script’s icon and then choose “Get Info” from the “File” menu. Look for a memory or RAM tab in the resulting window. My memory is fuzzy on the rest of the procedure so you might need to ask someone else how to proceed if you need further instructions.

– Rob

once again, it worked!

that’s incredible…i thought i’d never figure this out, and you found me the solution in a few hours!

thanks, buddy!

ryank
michelinesbury@hotmail.com

Ryan,

It looks like we were both responding at about the same time so I didn’t catch your latest post until I was finished with mine. If bumping the script’s RAM doesn’t help, I’ll see what I can do about reading and writing only the first part of the file. Unfortunately, I’ll be out until late this evening. If you are in a hurry, I hope that someone else steps in and offers a solution.

– Rob

We did it again! :stuck_out_tongue:

I’m glad that you were able to get it working.

– Rob

one last thing (if you’re still there)…

i’m distilling the PS files into PDF’s, and the end result is great, except now I have to crop a couple of inches off of the top and bottom of all the pages. is there a way to write a script to alter the PDF’s?

much appreciated again!

Working with PDFs is not one of my skills. If someone doesn’t offer help here, I suggest that you start another topic with a relevant subject.

Good luck!

– Rob

To crop the pages, you should script Distiller to use a different profile when distilling the files to PDF or, after the files have been distilled to PDF, use Acrobat to crop the files to the desired page size (this may require the use of another utility to control the GUI such as QuicKeys). Mucking about with the raw PostScript itself is usually not such a great idea.

Jon

thanks for all your help!

i’m going to post a new topic about applescript memory (since this one is getting pretty long)