I know I can write return to "path to file"
, but I would like to know if there is a way to write the backspace character to a file so as to delete a character without using TextEdit, just the write
command. I've tried backspace
, bckspc
and even delete
, but none of them seem to be working. Any help would be appreciated.
Asked
Active
Viewed 601 times
0

Gepapado
- 333
- 2
- 5
- 11
-
If you check the ASCII tables, there is indeed a [DEL] character. However, I have never seen an implementation of it working they way you want. I'd read in the file, drop the last character, and rewrite it back to file to simulate that functionality – im so confused Sep 10 '12 at 18:21
-
I see. Since I'm new to Applescript, how can I "drop the last character?" – Gepapado Sep 10 '12 at 19:16
-
Oh, I think I got it. I just read your comment more carefully. I'll just delete the old file and create a new one while having saved the contents of the previous file and changed the part I want. Thanks for the info. – Gepapado Sep 10 '12 at 19:25
-
Yep, exactly. However, I'm not sure that's the *fastest/most efficient* way, given that I know nothing about AppleScript, hence why I didn't post it as an answer. (also you usually don't have to delete the old file in most OSes I've encountered - just make sure you're not in append mode for writing and it will automatically clear the previous contents) – im so confused Sep 10 '12 at 19:31
2 Answers
1
This will delete the last character of the file "test" located on the desktop:
set myFile to (path to desktop as text) & "test"
set fRef to (open for access file myFile with write permission)
try
set eof of fRef to (get eof of fRef) - 1
end try
close access fRef

adayzdone
- 11,120
- 2
- 20
- 37
-
`(get eof of fRef) - 1` doesn't work with multi-byte characters. If `test` contained `aä` as UTF-8, TextEdit couldn't open it after running the script. – Lri Sep 10 '12 at 23:00
0
This would work even if the file didn't end with an ASCII character:
set f to POSIX file ((system attribute "HOME") & "/Desktop/test.txt")
set input to read f as «class utf8»
if input is not "" then set input to text 1 thru -2 of input
set b to open for access f with write permission
set eof b to 0
write input to b as «class utf8»
close access b
You could also use something like this:
shopt -u xpg_echo
x=aä
echo -n "$x" > test.txt
x=$(cat test.txt)
x=${x%?}
echo -n "$x" > test.txt
cat test.txt
rm test.txt

Lri
- 26,768
- 8
- 84
- 82