# Summ of Strings

Hi Folks,

I am writing to a file a Number (Sent Files/Received Files from Internet Connect) - this works pretty fine - reading is the same - works perfect
with

``````
set sent_data to read sent_file as string
display dialog sent_data

``````

the number in the file will be shown - everything fine… BUT

when I am trying to summ the sent_data with the received_data I get the following error:

``````
set gesamt to (received_data) + (sent_data)

``````

Can´t make “20” into type number. (-1700)

Can anybody please explain me how to summ two strings?

Stefan

A string is not a number. Imagine trying to do math on a letter. You may need to coerce to an integer or float.

``````set S to "20"
set R to "33"

set Both to 0 + S + R -- putting 0 first coerces the other two to numbers
--> 53
``````

Simarly:

``````set N to 22
set M to 33

set T to "" & N & ":" & M --> "22:33"; the leading "" coerces the numbers to text
``````

Stefan,

read chapter 9 in Hanaan Rosenthal’s book ;)

do you mean like this:

``````
set sent_data to read sent_file as string
set gesamt to 0 + received_data + sent_data

``````

Stefan

Exactly, assuming that the variables contain numbers as text and nothing else (except a decimal point or minus sign)

``````set A to "22.3"
set B to "-42"
set C to 0 + A - B --> 64.3
``````

Hi Stefan,

thanks for the hint…

regarding the book it should be possible to calculate integer…

``````
set sent_data to read sent_file as string
set sent_data to sent_data as integer
set gesamt to received_data + sent_data

``````

Error: Can´t make “20” into type integer. (-1700)

Stefan

Hi Folks,

I found the problem:

received_data is “20” with qoutes - so I have to delete the quotes…

Stefan

Sorry, this was not the problem…

Still searching…

Best Regards,

Stefan

Is your string perhaps Unicode text? If so, then read it that way and covert it afterward all in one step

``````set myNumber to read myFile as Unicode text as number

``````

thanks for your hints - this ist the Error Message:

Can’t make "ãˆ°

to get the input of the file I set first the variables:

``````
set sent_file to ((path to me as string) & "Contents:Resources:sent.txt") as file specification
set received_file to ((path to me as string) & "Contents:Resources:received.txt") as file specification

``````

To Read I use this commands:

``````
set sent_data to read sent_file as string
set sent_data to sent_data as number
display dialog sent_data

``````

The Output is received_data: 20, sent_data: 320

When I try to summ it I get an error that is not able to make “20” into type number…

Summ Command:

``````
set gesamt to 0 + (received_data) + (sent_data)

``````

Stefan

Hi,

Stefan could be reading utf8. When read as utf16, “20” is “ãˆ°”.

Edited: so you read as utf8:

``````
set f to choose file
set t to read f as «class utf8»
t as integer
``````

Edited: no, this isn’t it. You could still read utf8 and coerce to integer. Disregard.

gl,

Stefan,

what will be displayed without coercing to number?

```set received_data to read received_file as string -- or as Unicode text display dialog received_data```

Hi Folks,

you have to be very strong now - I found the solution:

It´s not the same wheter you write a number by hand in a file or by applescript! Entering
a number by hand into a file will result with an error - writing to a file with your script
will do exactly what you want…

That means, I first write (for testing purposes) a number into a file with applescript and
now the summ function is working as expected…

Thanks to all of you for your help…

Stefan

That depends on what application you’re writing (by hand) with. Rich Text Format, for example won’t work. The document has to be text. If you write it from AppleScript and retreive it from AppleScript it’ll work because the formats and text encoding of the two are the same, but if you write it in one encoding and try to read it in another, no go.

since I am writing the variable as string everthing is working fine - so if someone needs the code I will put it in here:

Setting the File (File is inside the Application (in Resources)

``````
set sent_file to ((path to me as Unicode text) & "Contents:Resources:sent.txt") as file specification
set received_file to ((path to me as Unicode text) & "Contents:Resources:received.txt") as file specification

``````

Writing to File:

``````
set file_sent to open for access sent_file with write permission
set eof of sent_file to 0
write (sent_final as string) to sent_file starting at eof
close access sent_file

``````

``````
set sent_data to read sent_file as string

``````

Thanks and Best Regards,

Stefan

Hi Folks,

expect the unexpected! In my application I try to write to file when the application is quitting or when the red button is pressed! The only thing which will be written (alltough I use the same code as before) is “true” to the file…

``````
on will close theObject

if name of the theObject is "main" then

if modem_display is "Web'n'walk Box compact" then
do shell script quoted form of POSIX path of ((path to me as Unicode text) & "Contents:Resources:Web'n'walk Manager") & " disconnect 0"

else if modem_display is "Web'n'walk Box" then
do shell script quoted form of POSIX path of ((path to me as Unicode text) & "Contents:Resources:Web'n'walk Manager") & " disconnect 1"

else if modem_display is "Web'n'walk Card express" then
do shell script quoted form of POSIX path of ((path to me as Unicode text) & "Contents:Resources:Web'n'walk Manager") & " disconnect 2"

end if

-- Hier werden die gesendeten/empfangen Daten geschrieben
if received_final does not contain "true" then
set eof of received_file to 0
end if

if sent_final does not contain "true" then
set file_sent to open for access sent_file with write permission
set eof of sent_file to 0
write (sent_final as string) to sent_file starting at eof
close access sent_file
end if

end if

quit

delay 1

end will close

``````

has anybody a clue how this can be solved?

Thanks,

Stefan