Automator Replace text inside a text file

I have absolutely no idea about coding, for a couple weeks I was playing around with automator, Mac OS... but I'm stuck when I attempt to create an automation to ask for a text and then based on the answer I could replace a couple of "variables" inside a text file and then just simply save this text file...

I've tried to play around with this code, but all the time an error in apple script:

set stringToFind to "replace that"
set stringToReplace to "with this"
set theFile to choose file
set theContent to read theFile as «class utf8»
set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, stringToFind}
set ti to every text item of theContent
set AppleScript's text item delimiters to stringToReplace
set newContent to ti as string
set AppleScript's text item delimiters to oldTID
try
    set fd to open for access theFile with write permission
    set eof of fd to 0
    write newContent to fd as «class utf8»
    close access fd
on error
    close access theFile
end try

Any ideas how to master it? Or where to look for the answer? I spend several hours trying to solve it but without any progress... Thank you in advance

Posted on May 26, 2021 09:18 AM

Reply
Question marked as Top-ranking reply

Posted on May 26, 2021 10:56 AM

Here is a complete Automator Application solution that prompts you for a text file, and then prompts for the Find String, and then the Replace string. These changes are applied to the text file and it is rewritten in place.


Replace the contents of a Run AppleScript action with the following code:


use framework "Foundation"
use AppleScript version "2.4"
use scripting additions

property NSString : a reference to current application's NSString

on run {input, parameter}
	set stringToFind to (item 1 of input) as text
	set stringToReplace to (item 2 of input) as text
	set theTextFile to (item 3 of input)
	
	set theContent to read (theTextFile as alias) as text
	set newContent to my replace_text(theContent, stringToFind, stringToReplace)
	
	try
		set fileRef to (open for access theTextFile with write permission)
		set eof of fileRef to 0 # truncate file
		write newContent to fileRef
		close access fileRef
	on error
		close access fileRef
	end try
	# display dialog (item 1 of input) & return & (item 2 of input) & return & (item 3 of input)
	return
end run

on replace_text(atext, xfind, xreplace)
	return ((NSString's stringWithString:atext)'s stringByReplacingOccurrencesOfString:xfind withString:xreplace) as text
end replace_text


The actual Automator application workflow will look like this:


6 replies
Question marked as Top-ranking reply

May 26, 2021 10:56 AM in response to VikingOSX

Here is a complete Automator Application solution that prompts you for a text file, and then prompts for the Find String, and then the Replace string. These changes are applied to the text file and it is rewritten in place.


Replace the contents of a Run AppleScript action with the following code:


use framework "Foundation"
use AppleScript version "2.4"
use scripting additions

property NSString : a reference to current application's NSString

on run {input, parameter}
	set stringToFind to (item 1 of input) as text
	set stringToReplace to (item 2 of input) as text
	set theTextFile to (item 3 of input)
	
	set theContent to read (theTextFile as alias) as text
	set newContent to my replace_text(theContent, stringToFind, stringToReplace)
	
	try
		set fileRef to (open for access theTextFile with write permission)
		set eof of fileRef to 0 # truncate file
		write newContent to fileRef
		close access fileRef
	on error
		close access fileRef
	end try
	# display dialog (item 1 of input) & return & (item 2 of input) & return & (item 3 of input)
	return
end run

on replace_text(atext, xfind, xreplace)
	return ((NSString's stringWithString:atext)'s stringByReplacingOccurrencesOfString:xfind withString:xreplace) as text
end replace_text


The actual Automator application workflow will look like this:


May 26, 2021 09:54 AM in response to lukasz122

I have simplified your code and tested the following script on macOS 11.4:


use framework "Foundation"
use scripting additions

property NSString : a reference to current application's NSString

set stringToFind to "replace that"
set stringToReplace to "with this"

set theTextFile to (choose file of type {"public.plain-text"} default location (path to desktop))

set theContent to read theTextFile as text
set newContent to my replace_text(theContent, stringToFind, stringToReplace)

try
	set fileRef to (open for access theTextFile with write permission)
	set eof of fileRef to 0 # truncate file
	write newContent to fileRef
	close access fileRef
on error
	close access fileRef
end try
return

on replace_text(atext, xfind, xreplace)
	return ((NSString's stringWithString:atext)'s stringByReplacingOccurrencesOfString:xfind withString:xreplace) as text
end replace_text


I will post the Automator solution shortly.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Automator Replace text inside a text file

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.