Reading number values from file

Hi Guys,
I’m pretty new to AppleScript and this is the problem I got:

I basically want to +1 the value of a Textfile. The Textfile only contains one integer, let’s say 3.

So what I tried was:

  • open file with write permission
  • set Variable to InputFile + 1
  • write Variable to InputFile

How do I read the content of a file as a numeric value in order to do calculations with it?

Thanks for your help!

Here’s a commented script on how to do it. The read/write commands can act up at times…

--First we set the path to the file
set thePath to (path to desktop as string) & "Yar.txt"
--to write something to a file, we need to open it with write permission
set theOpenFile to open for access thePath with write permission
--now we read the file to get the number stored in it
set theNum to read theOpenFile
--now we set it to a number
set theNum to theNum as number
--now we add 1 to that number
set theNum to theNum + 1
--now we erase the contents of the file
set eof theOpenFile to 0
--now we write the number to the file
write (theNum as string) to theOpenFile
--now we close access 
close access theOpenFile

Just replace “yar.txt” with whatever the name of your file is.

Of course, if your file is not at your desktop, then you could set thePath to a path (i.e. “Users:Joe:Desktop:”)

Welcome to MacScripter! :slight_smile:

Many thanks for the quick reply - worked like a charm :slight_smile: