Hi!
And old question, I know. But the answers here is too complicated for me
Is there a simple way of change the word(s) “null” in a text file to “” (nothing)?
/Jonas
Hi!
And old question, I know. But the answers here is too complicated for me
Is there a simple way of change the word(s) “null” in a text file to “” (nothing)?
/Jonas
There’s many ways to do this.
choose file with prompt "Replace text in this file:" without invisibles
set theFile to result
display dialog "Find this:" default answer ""
set find to text returned of result
display dialog "Replace with:" default answer ""
set replace to text returned of result
try
set fileRef to open for access theFile with write permission
set fileText to read result
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to find
set fileText to every text item of fileText
set AppleScript's text item delimiters to replace
set fileText to (fileText as text)
set AppleScript's text item delimiters to ASTID
write fileText to fileRef starting at 0
close access fileRef
display dialog "Script finished." buttons "OK" default button 1
on error errorMsg number errorNum
try
close access fileRef
end try
display dialog "Error: (" & errorNum & "):" & return & return & errorMsg buttons "Cancel" default button 1
end try
Nice!
But if I don’t want the dialogs, just the replace function…?
You didn’t need to start another thread.
Is this a standalone script, or do you want something that you can include in another script?
If you’re just trying to get rid of the dialogs, try change the top part of the second script.
choose file with prompt "Replace text in this file:" without invisibles
set theFile to result
display dialog "Find this:" default answer ""
set find to text returned of result
display dialog "Replace with:" default answer ""
set replace to text returned of result
.to something like this:
property theFile : "" -- put a valid file inside the quotes
property find : "null"
property replace : ""
If what you really wanted was a function, then you should have said so. Then I would’ve pointed you to this thread.
Edit: You should’ve found the above thread if you used the forum’s search function. Of course, there are other ways to find it. (I just found that yesterday, so I had to try it out. :P)
I tried to search but there was no hits what so ever that helped me
But you have come to my rescue so everything is all right now :lol:
Anyway…
I’m almost there but there is one step left.
i cant write the results of the replace to the file.
Here is the script:
(The word “WHAT?” is a substitute for the thing i’m looking for)
set the_file to "Macintosh HD:Users:abisilla:Desktop:images:TheTextFile.txt"
findAndReplace("null", "hello", the_file)
on findAndReplace(tofind, toreplace, TheString)
set ditd to text item delimiters
set res to missing value
set text item delimiters to tofind
repeat with tis in text items of TheString
if res is missing value then
set res to tis
else
set res to res & toreplace & tis
end if
end repeat
set text item delimiters to ditd
return res
end findAndReplace
try
(open for access the_file with write permission) as text
set eof of the_file to 0
write "WHAT?" to the_file starting at eof as list
close access the_file
end try
…hoping