Isaiah Turner
Isaiah Turner

Reputation: 2662

How do I remove the automatic new line when exporting from applescript to text file?

If I was to the run command

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

It would generate a text file on my Desktop with the name file.txt and the contents

hello

(Imagine a blank line here)

How can I keep it from creating this line, I am unable to find any resources online. Or is my online option to find some way to remove it after it is created?

Upvotes: 0

Views: 364

Answers (3)

Lri
Lri

Reputation: 27633

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

printf aa%sbb would print aabb. This method doesn't work if the input is longer than getconf ARG_MAX bytes (currently about 300 kB).

This also works for longer strings:

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

Without as «class utf8» the file would be saved as MacRoman. as Unicode text is UTF-16.

do shell script uses /bin/sh, so do shell script "echo -n hello" prints -n hello unless you add shopt -u xpg_echo.

Upvotes: 1

markhunte
markhunte

Reputation: 6932

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.

Upvotes: 1

user1804762
user1804762

Reputation:

This does it I believe:

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

Upvotes: 1

Related Questions