如何使用Applescript在文件中编写[Backspace]?

时间:2012-09-10 18:17:09

标签: applescript backspace

我知道我可以write return to "path to file",但我想知道是否有办法将退格字符写入文件以便在不使用TextEdit的情况下删除字符,只需write命令。我已经尝试了backspacebckspc甚至delete,但似乎没有一个能够正常工作。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

这将删除位于桌面上的文件“test”的最后一个字符:

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

答案 1 :(得分:0)

即使文件没有以ASCII字符结尾,这也会起作用:

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

您也可以使用以下内容:

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