heed help.. for find in TextEdit

hi there
I have a script to get the home folder name … I need to paste it in a text file … it is ok but I need to plase it in the meddle of the text … mainly I will replace it with some other …
I the the code to find a word inside the text file … only to find the word and hilite it… I have the result in the clipbord and it is ready to paste … but it comes in the stat of text file…
\can any one help me ?:rolleyes:

You don’t need to use TextEdit. For example, if I wanted to change “banana” to my home folder name:

do shell script "/usr/bin/sed -e 's/banana/'`/usr/bin/basename \"$HOME\"`'/g '/path/to/input_file.txt' > '/path/to/output_file.txt'"

That will replace all instances of “banana” in the input file with my home folder name and save as the output file.

If you’re unsure of the input file path, or how to translate it to the shell command, paste some of your code so we can show you how to get there.

The real question is: what instance(s) do you want to replace? All of them? One specifically?

Alternatively, you can probably just use the “whoami” command:

do shell script "/usr/bin/sed -e 's/banana/'`/usr/bin/whoami`'/g '/path/to/input_file.txt' > '/path/to/output_file.txt'"

Um, I’m no expert in applescript or anything but that looks like an OS X script to me…isn’t this the classic scripting area?

Yeah, I know, but he said “TextEdit” in the subject of the post. I assumed he’s using Mac OS X. He’s also trying to find the home folder name, which definitely implies Mac OS X.

Edit: Can a mod move this to the OS X area?

hi all
what I need is near what you said … but…
I tried the script ( I think I did a mistake) like this
do shell script “/usr/bin/sed -e ‘s/put your name here/’/usr/bin/basename \"$HOME\"'/g '‘startup disk/Users/defult/info.txt’> '‘startup disk/Users/defult/info.txt’”
but I got this error
sh: -c: line 1: unexpected EOF while looking for matching ’ "
sh: -c: line 2: syntax error: unexpected end of file

what I need to do is :
I have this text in TextEdit file

////////////////// please enter the following Information /////////////////////
var username = “put your name here”; // User name
var phoneext = “”; // Phone extension
var Computer No. = “”; // Computer Number

and many users on this computer… this file placed in the Pref folder of User Template folder in the system

the script I did is :

tell application “Finder”
get the name of home
set thehome to the name of home
set the clipboard to thehome
end tell

this will get the home name to the clipboard
what I need to put the home in the place of “put your name here” inside the file

open it or with I/O or with open and change then save it is not important but I neet it without any user action. ( I don’t want the user to put any thing by him self.

thank you all for your help

Yeah, you typoed the script command, it looks like. You should be able to use this code verbatim unless you need a different file path (see below):

do shell script "file=\"$HOME/Library/Preferences/info.txt\" ; /bin/mv \"$file\" \"$file.backup\" ; /usr/bin/sed -e 's/\"put your name here\"/\"'`whoami`'\"/g' \"$file.backup\" > \"$file\""

This will work if the file is in the user’s preferences folder. If it’s somewhere else, just alter the path. Note that “$HOME” will get the user’s home directory for you. For example, if the file were in a folder called “templates” on the desktop of each user, you would use this path:

That expands to something like this:

Make sense?

The script will get the get the user name for you with the “whoami” command, so you don’t need to put it on the clipboard first. Additionally, this script will make a backup of the original file, naming the backup “info.txt.backup”.

THANK YOUTHANK YOUTHANK YOU Mikey-San
a lot of thanks …
I did it
it is working fine
:smiley:

I’d like to improve it a little bit, and make it more robust:

do shell script "file=\"$HOME/Library/Preferences/info.txt\"

if [ `/usr/bin/grep '\"put your name here\"' \"$file\" &> /dev/null ; echo $?` == 0 ]
then
/bin/mv \"$file\" \"$file.backup\"
/usr/bin/sed -e 's/\"put your name here\"/\"'`whoami`'\"/g' \"$file.backup\" > \"$file\"
fi"

This way, if the file doesn’t exist or contain the string, nothing gets moved around.

Edit: Readability.