Find & Replace script

Hi,
I’m trying to come up with a script that will find a string in everyfile in a specific folder and replace that string.

I have tried and tested the one below and this works but only if a file is dropped onto the application.

Ideally I’d like a simple command line… but I can’t find one that works… help…

he’s my attempt so far…

on open (aliasList)
set textToFind to “%%EndComments”
set textReplacement to "%
%HDJobstream V 2.7.3; JobID = 4
%
%%EndComments
%% OPC: HQS7CheckerNeg150
%
% PSBeforeOPC
%
currentfile 1 (%LHCOPCEnd) /SubFileDecode filter flushfile

%LHCOPCBegin: 1796
%!PS-Adobe-2.0

%%Copyright: (C) 1998 Heidelberger Druckmaschinen AG

%%BeginPrologLOC LinoOutputControl

statusdict/product known { statusdict/product get exec }{ ( ) }ifelse

(Linotype) eq{

{(Lino/LOC/defs) status } stopped not { {clear (Lino/LOC/defs) run}if } if

userdict /loc_dict known {

% define_token_if_unknown

/dtiu {

dup loc_dict exch known not { loc_dict exch { pop } put }{ pop } ifelse

} def

loc_dict begin

/loc_version dtiu

/loc_name dtiu

/loc_rip dtiu

/loc_recorder dtiu

/loc_negativemode dtiu

/loc_mirrormode dtiu

/loc_dotshape dtiu

/loc_screenfrequency dtiu

/loc_screening dtiu

/loc_interpreter_res dtiu

/loc_screenmode dtiu

/loc_substituteangles dtiu

/loc_slugline dtiu

/loc_slugline_usertext dtiu

/loc_alignmentmarks dtiu

/loc_app dtiu

/loc_resolution dtiu

/loc_punch dtiu

/loc_processcalib dtiu

/loc_cnf_punch dtiu

/loc_cnf_screen dtiu

/loc_activate dtiu

/loc_negativesuppl dtiu

/loc_delta_res dtiu

/loc_material dtiu

/loc_colorseparation dtiu

/loc_ds dtiu

/loc_gravure dtiu

/loc_printer dtiu

/loc_cropmarks dtiu

/loc_jobscale dtiu

/loc_matcut dtiu

/loc_colorangles dtiu

/loc_color_temp dtiu

/loc_source dtiu

%%BeginSetupLOC

(1.2) loc_version

(MAC-OPC) loc_source

(headers:PSHeaders:DavesTest) loc_name

(Herkules/DeltaRIP) loc_recorder

(DeltaRIP) loc_rip

(1) loc_ds

(1) loc_gravure

(0 ) loc_printer

(1693) loc_resolution

(on) loc_negativemode

(HQS7.5) loc_screening

(quality) loc_interpreter_res

(round) loc_dotshape

(133) loc_screenfrequency

(150) loc_colorangles

%

%%EndSetupLOC

%%BeginEpilogLOC

% now activate the above

% routine - dtiu - in header_prolog needs a dummy string

(dummy) loc_activate

%%EndEpilogLOC

end % loc_dict

} if % loc_dict known

} if % Linotype

%%EndPrologLOC

%LHCOPCEnd

%%BeginDefaults"

repeat with i from 1 to aliasList's length
	
	set aliasFile to aliasList's item i
	
	--   Filter out folders: 
	-- 
	if ((aliasFile as string)'s character -1 is not ":") then
		
		set fileRef to open for access aliasFile with write permission
		try
			
			set fileContents to read fileRef
			
			--   note: case-sensitive 
			-- 
			set fileContents to my ReplaceText(fileContents, textToFind, textReplacement)
			
			--   clear the file's contents 
			-- 
			set eof fileRef to 0
			
			write fileContents to fileRef
			
			close access fileRef
			
		on error e number n from f to t partial result p
			try
				close access fileRef
			end try
			error e number n from f to t partial result p
		end try
	end if
end repeat

end open

on ReplaceText(s, f, r)
set astids to AppleScript’s text item delimiters
try
set AppleScript’s text item delimiters to f
set s to s’s text items
set AppleScript’s text item delimiters to r
set s to s as string
set AppleScript’s text item delimiters to astids
return s
on error e number n from f to t partial result p
set AppleScript’s text item delimiters to astids
error e number n from f to t partial result p
end try
end ReplaceText

OK, so you’re saying that the script you posted works fine, but you want a different way to make the script do it’s thing other than drag and drop?

There is a command line tool for calling scripts, ‘osascript’, but one can’t pass arguments to the script.

So it depends on what you want to do. Instead of an open handler, you can place a ‘choose folder’ command into the run handler, and operate on files in the folder that user specifies. If you need to be able to call the script from the command line, you could do something tricky: like have a shell command write the file paths to be operated on into a file and then use osascript to launch the AppleScript, which can be designed to read the file of pathstrings, etc.

How do you want the script to work, ie: what “calls” the script, telling it what files to operate on?

Hi Arthur,

I really wanna be able to make the script choose the contents of a folder and process the files and I guess I need to trigger the script at timed intervals.

I thought of maybe using Cron to trigger the script.

Also Isnt there a way of doing this in a command line or shell script maybe using “find” or “sed” I’ve tried using sed but it doesnt seem to work as well as the applescript, because I’m repalcing quite a complex string??

I think I’m way over my head here…

any suggestions would be appreciated

:cry:

You can use your own handler adding this code. Just add this to your script, adjust the properties “folderToMonitorize” and “intervals”, save it as application “stay-open” (not “run-only”) and launch it (though it won’t work if your computer is asleep).

property folderToMonitorize : "path:to:dir:"
property intervals : 5 * 60 --> 5 minutes

on idle
	--> quick-find all files in folderToMonitorize (finds *any* nesteed file)
	set allFiles to paragraphs of (do shell script "find " & quoted form of POSIX path of folderToMonitorize & " -type f")

	--> convert posix paths to aliases
	repeat with i from 1 to count allFiles
		set allFiles's item i to allFiles's item i as POSIX file as alias
	end repeat
	
	--> call your handler
	open allFiles
	
	--> after processing, wait the specified time
	return intervals
end idle