AS variable is garbled when outputed to shell script

I’m a complately new to AS, so bare with me please…

What I’d like to know is; why does a variable from AS become garbled when I output it to shell script?
I presume that AS and shell script (bash) has different ways of reading characters, but how do I convert from one form to the other?

In my AppleScript, I’m reading a single number from a file (that is the only character this file contains) because I’m using this file as a “countdown”-file to se how many times a script has been run.
For each run, this number is X - 1, and my plan is to write this back into the same file.

Now, If I use AS to do the write operation, shell script (bash) won’t see this as anything other than garble. For instance like this:

write CountfileData - 1 to file "path:countfile"

I’ve also tried to pass the varible to the shell by this command

do shell script ("echo" & CountfileData & " > /path/countfile")

I’m sure someones must have come across this problem before.
The answer might be right in front of my eyes without me being able to see it…

One thing though…
The contents of my “countfile” must be readable to the shell, since I’m combining operations from a shell script with applescript for GUI purposes.



You are writing a “number”, so it is stored as applescript number -a long integer- (four bytes). If you write a number 5, it will be ascii 0 + ascii 0 + ascii 0 + ascii 5.
Simply use “as text” and it will be stored as text (eg, readable from a shell script):

write ((CountfileData - 1) as text) to file "path:countfile"

OK, thanks!!
It worked great except for one small issue.

My AppleScript starts with the following code:

(set CountfileData to (read file ("path:countfile"))

if CountfileData > 0 then
(* a lot of things, amongst them, write the variable to the "count-file"*)
(*....script quits*)

When it is read as text, the IF suddenly reads as TRUE, even if the file contains ‘0’, making the script loop virtually for ever…

Do you know what can be done to prevent this?


Hmmm… This works here:

"0" > 0 --> false

Anyway, you can coerce to number:

if (CountfileData as number) > 0 then

More info about “coercions”, AppleScript Language Guide, pag. 363 (you can find a link at the main page: )

Thanks again!
If it’s of any interesst I also found that setting

if CountfileData > 1 then

also will work.