0

如果我要运行命令

do shell script "echo hello > ~/Desktop/file.txt"

它会在我的桌面上生成一个名为 file.txt 和内容的文本文件

你好

(想象这里有一个空行)

我怎样才能阻止它创建这条线,我无法在网上找到任何资源。还是我的在线选项可以在创建后找到某种方法将其删除?

4

3 回答 3

1
do shell script "printf %s " & quoted form of "hel'\\%slo" & " > ~/Desktop/file.txt"

printf aa%sbb会打印aabb. getconf ARG_MAX如果输入长于字节(目前约为 300 kB),则此方法不起作用。

这也适用于更长的字符串:

set f to POSIX file ((system attribute "HOME") & "/Desktop/file.txt") as text
set b to open for access f with write permission
set eof b to 0
write "α" to b as «class utf8»
close access b

没有as «class utf8»该文件将被保存为 MacRoman。as Unicode text是 UTF-16。

do shell script使用 /bin/sh,所以do shell script "echo -n hello"除非-n hello你添加shopt -u xpg_echo.

于 2013-03-03T16:36:32.207 回答
1

我相信这样做:

do shell script "printf hello > ~/Desktop/file.txt"
于 2013-03-03T07:11:51.733 回答
1

DigiMonk's code works.

But a note to future readers:

The original posters code :

do shell script "echo hello" > "~/Desktop/file.txt"

Is incorrectly posted and will never work as is. Applescript will compile it. But in effect it is say 'is hello greater than ~/Desktop/file.txt

Correct forms:

Quote and escape the quotes for the text string. ( the better option if the text string is long and contains spaces.)

do shell script "echo \"hello world\" > ~/Desktop/file.txt"

or use no quotes.

do shell script "echo hello > ~/Desktop/file.txt"

Also note you cannot quote around the file path when using the tilde ~ symbol. The tilde symbol is used to point to the users home directory. Quoting it will stop it working.

于 2013-03-03T13:39:07.600 回答