Cant make type 1 into number

Discussion in 'Mac Programming' started by DoubleJGames, Oct 8, 2013.

  1. DoubleJGames macrumors newbie

    Joined:
    Sep 30, 2013
    #1
    Hello,

    I am making an applescript that involved a text document with just a number in it.

    This is the code I am having trouble with.
    Code:
    set posFileLoc to ((path to home folder as text) & "TestFolder:Position.txt")
    set theName to (do shell script "cat " & quoted form of (POSIX path of posFileLoc))
    set theName to theName + 1
    
    The Position.txt has just a single number in it but when i run it i get this error.
    Cant make type 1 into number. (the error bit is the 1 after set theName to theName)

    I have tried putting integer and number infront of both theName and the actual 1 but i still get the same error.

    DoubleJGames
     
  2. robvas macrumors 68020

    Joined:
    Mar 29, 2009
    Location:
    USA
  3. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #3
    It works for me, using the script exactly as posted.

    I created the folder and the file, pasted the number 10 into the file as text, and ran the script from AppleScript Editor.app. It worked. Then I added a newline, and it still worked. Changed the number to 42: it still worked.

    Maybe your Position.txt file doesn't really contain what you think it does. Copy and paste this into a Terminal window, then copy and paste the complete output into a reply posted here:
    Code:
    hexdump -C ~/TestFolder/Position.txt
    

    Try this:
    • Open a new AppleScript Editor window.
    • Copy and paste your exact script fragment into it.
    • Compile it (command-K).
    • Run it (command-R).
    • Post the result.
     
  4. DoubleJGames thread starter macrumors newbie

    Joined:
    Sep 30, 2013
    #4
    Well it works fine by itself but when i add in the part that writes theName +1 back into the .txt file thats when i get the problem

    This is my full code for the read/write to .txt file
    Code:
    set posFileLoc to ((path to home folder as text) & "TestFolder:Position.txt")
    set theName to (do shell script "cat " & quoted form of (POSIX path of posFileLoc))
    set (theName to theName + 1
    --this is the writing to file.
    set posBody to theName
    set posFile to ((path to home folder as text) & "TestFolder:" & "Position.txt")
    try
    	set posReference to open for access file posFile with write permission
    	write posBody to posReference
    	close access posReference
    on error
    	try
    		close access file posFile
    		
    	end try
    end try
    
    
    
    The error i get is cant make "10" (Whatever number i have in the .txt file) into type number.

    P.S sorry for taking ages to reply been quite busy lately

    DoubleJGames
     
  5. chown33, Oct 14, 2013
    Last edited: Oct 15, 2013

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    The posted code doesn't compile. There's a spurious open-paren here:
    Code:
    set (theName to theName + 1
    

    You should probably study exactly how AppleScript's file access commands work.

    When you write posBody to a file, it's written as a 32-bit big-endian integer. That is, as four binary bytes. The 'cat' command will simply output those four binary bytes, but the rest of your program is assuming that cat's output will be text, i.e. a numeric string. This fails because you get a binary representation (4 binary bytes) instead of a text representation (2 ASCII characters.)

    If you want to write a numeric string (again, study the file access commands), then you must explicitly say that in the script:
    Code:
    	write posBody to posReference as string
    
    [See EDIT below.]


    You could also sidestep much of AppleScript's file access by using shell commands. Example:
    Code:
    do shell script "echo " & theName & " >" & quoted form of (POSIX path of posFileLoc)
    
    So the entire AppleScript to read the value, increment it, and write it back to the same file would be:
    Code:
    set posFileLoc to ((path to home folder as text) & "TestFolder:Position.txt")
    set theName to (do shell script "cat " & quoted form of (POSIX path of posFileLoc))
    set theName to theName + 1
    do shell script "echo " & theName & " >" & quoted form of (POSIX path of posFileLoc)
    
    In general, it's a bad idea to mix shell commands with AppleScript file access, unless you're very careful about exactly what data representation is being read and written. If you'd opened and read the file using AppleScript commands, it might have worked. But then the first read would fail, because the data in the file would be "10" (ASCII chars, numeric text) instead of 4 hex bytes: 00 00 00 0A.


    EDIT
    Before writing the string data, you should first truncate the file to zero length. This ensures that only the most recently written string is the entire file's contents.
    Code:
    	set eof of posReference to 0
    	write posBody to posReference as string
    
    The 'echo' shell script already truncates, so it's fine as-is.
     

Share This Page