Page 1 of 1

read/write files in a standalone app

Posted: Sun Jan 13, 2013 6:18 pm
by tyarmsteadBUSuSfT
I have a file that I want the user to be able to save info to and read it in field. My problem is that I'm using the specialfilefolder, and every thing works in the simulator, but when I build the standalone and make ad save entries the list of saved entries does not display.
Thank you

Re: read/write files in a standalone app

Posted: Sun Jan 13, 2013 6:22 pm
by Klaus
Hi tyarmsteadBUSuSfT,

the actual name is "specialfolderpath()" and it is a function 8)
What syntax are you using? Please post your code.


Best

Klaus

Re: read/write files in a standalone app

Posted: Sun Jan 13, 2013 8:57 pm
by tyarmsteadBUSuSfT
I apologize, missed type in my post.

put url ("file:" & specialFolderPath("documents") & "/colorlist.txt") into tSavedFile
the colorlist.txt is the file that is created, read and written to.

Thank you

Re: read/write files in a standalone app

Posted: Sun Jan 13, 2013 9:49 pm
by Klaus
Hi,

OK, the script is correct, how do you write the data in your standalone?
Please post that code, too :D

But wait, maybe this will only fail the first time when there is NOT YET a file with this name?
So do some checking in your scripts:

Code: Select all

...
put specialFolderPath("documents") & "/colorlist.txt") into tFile
if there is a file tFile then
  put url ("file:" &  tFile) into tSavedData
else
  put EMPTY into tSavedData
end if
...
Best

Klaus

Re: read/write files in a standalone app

Posted: Sun Jan 13, 2013 11:43 pm
by tyarmsteadBUSuSfT
on mouseUp
ask info "Save As Name:" titled "Save Entry"
if it is empty then exit mouseup
put it into tSavedname
if tSavedname is "" then answer error "Must Enter a file Name"
if tSavedname is empty then exit mouseUp
set the itemDel to tab
open file url ("file:" & specialFolderPath("documents") & "/colorlist.txt") for write
put url ("file:" & specialFolderPath("documents") & "/colorlist.txt") into tSavedFile
put the number of lines in tSavedFile + 1 into tNextFileNumber
put tSavedName into item 1 of line tNextFileNumber of tSavedFile
put item 1 of field "Color2" into item 2 of line tNextFileNumber of tSavedFile
put item 2 of field "Color2" into item 3 of line tNextFileNumber of tSavedFile
put item 1 of field "Color3" into item 4 of line tNextFileNumber of tSavedFile
put item 2 of field "Color3" into item 5 of line tNextFileNumber of tSavedFile
put item 1 of field "Color4" into item 6 of line tNextFileNumber of tSavedFile
put item 2 of field "Color4" into item 7 of line tNextFileNumber of tSavedFile
put item 1 of line 1 of field "Color" into item 8 of line tNextFileNumber of tSavedFile
put item 1 of line 2 of field "Color" into item 9 of line tNextFileNumber of tSavedFile
-- put return
put tSavedFile into URL ("file:" & specialFolderPath("documents") & "/colorList.txt")
close file url ("file:" & specialFolderPath("documents") & "/colorlist.txt")
--put url ("file:" & "/colormatchlist.txt") into tfile
answer info "Entry Saved"


end mouseUp

I just recently added the open and close file options
Thank you for your help
Ty

Re: read/write files in a standalone app

Posted: Mon Jan 14, 2013 7:59 am
by Traxgeek
Hi tyarmsteadBUSuSfT,

I'm a complete Newbie here but do you 'NEED' the 'file:' before the 'specialfolderpath.....' in your code ?

So, in theory,
open file url ("file:" & specialFolderPath("documents") & "/colorlist.txt") for write
would be come (in my rather simple world !)
open file url (specialFolderPath("documents") & "/colorlist.txt") for write

Hope I'm not being stupid here ! If I am - just ignore me ! :?

Rgds Traxgeek

Re: read/write files in a standalone app

Posted: Mon Jan 14, 2013 12:59 pm
by Klaus
Hi Ty,

Traxgeek is correct, wrong syntax!
You did not add the "if there is a file XXX" check as suggested?!

And your are mixing the syntax for "open file XXX for read" with the shorter "url("file:...") syntax!
Decide to use ONE of them!

Try this:

Code: Select all

on mouseUp
  set itemdel to TAB
  
  ask info "Save As Name:" titled "Save Entry"
  put it into tSavedname
  if tSavedname = empty then
    answer error "Must Enter a file Name"
    exit mouseUp
  end if
  
  ## Put complete path into a variable, will save a lot of typing later:
  put specialFolderPath("documents") & "/colorlist.txt"  into tFile
  
  ## IMPORTANT!!!!
  ## If there is not a file then the script will HALT at this point and might leave you clueless!
  if there is a file tFile then
    
    ## Put content of file "en bloc" into variable!
    put url ("file:" & specialFolderPath("documents") & "/colorlist.txt")  into tSavedFile
    
    ## Plan B:
  ELSE
    put EMPTY into tSavedFile
  end if
  
  ## Fill new content of variable
  put the number of lines in tSavedFile + 1 into tNextFileNumber
  put tSavedName into item 1 of line tNextFileNumber of tSavedFile
  put item 1 of field "Color2" into item 2 of line tNextFileNumber of tSavedFile
  put item 2 of field "Color2" into item 3 of line tNextFileNumber of tSavedFile
  put item 1 of field "Color3" into item 4 of line tNextFileNumber of tSavedFile
  put item 2 of field "Color3" into item 5 of line tNextFileNumber of tSavedFile
  put item 1 of field "Color4" into item 6 of line tNextFileNumber of tSavedFile
  put item 2 of field "Color4" into item 7 of line tNextFileNumber of tSavedFile
  put item 1 of line 1 of field "Color" into item 8 of line tNextFileNumber of tSavedFile
  put item 1 of line 2 of field "Color" into item 9 of line tNextFileNumber of tSavedFile
  
  ## Write new content back to file:
  put tSavedFile into URL ("file:" & specialFolderPath("documents") & "/colorList.txt")
  
  answer info "Entry Saved"
end mouseUp
Do you see the last dialog "Entry saved"?

Best

Klaus

Re: read/write files in a standalone app

Posted: Tue Jan 15, 2013 12:36 am
by tyarmsteadBUSuSfT
Thank you, but it is still not working in the standalone version. I copied your code into my app. When I go to the card with the list of saved files I use this:
on opencard
put url ("file:" & specialFolderPath("documents") & "/colorlist.txt") into tSavedFile
repeat with x =1 to the number of lines in tSavedFile
put item 1 of line x of tSavedFile into item 1 of line x of field "ColorList"
end repeat
end opencard

It works fine on the desk top and simulator.
Thank you for your help and patience.
Ty

Re: read/write files in a standalone app

Posted: Tue Jan 15, 2013 12:36 pm
by Klaus
Hi Ty,

add this line after writing of the file, maybe "the result" will give us a hint:
...
## Write new content back to file:
put tSavedFile into URL ("file:" & specialFolderPath("documents") & "/colorList.txt")
ANSWER the result
...

Best

Klaus

Re: read/write files in a standalone app

Posted: Tue Jan 15, 2013 6:14 pm
by bangkok
One question : what do you call "simulator" ? And furthermore, which OS do you use ?

Then, for debugging purpose you should add a few "answer" (along with the result) in order to see how your script is working, and which data the script is handling.

For instance, are you sure that you actually write data to your file ?

Code: Select all

answer tSavedFile 
put tSavedFile into URL ("file:" & specialFolderPath("documents") & "/colorList.txt")
answer the result
For reading :

Code: Select all

on opencard
put url ("file:" & specialFolderPath("documents") & "/colorlist.txt") into tSavedFile
answer tSavedFile
Other issue, in your write script you use "set itemdelimiter to tab"

And then in your read script, you seem to have forgotten it...

Re: read/write files in a standalone app

Posted: Tue Jan 15, 2013 8:02 pm
by tyarmsteadBUSuSfT
Thank you for the great feedback. I used both answer options and I got "OK" then trying again I used tSavedFile and got the variable displayed as it should.
Ty