Page 1 of 1

Using the put URL command to save files

Posted: Thu Dec 29, 2011 4:21 pm
by Colorado
I am working on an iPad app, but also making a Mac version. I would like to save a text file representing data for a group in a separate (groupName) folder so that I can later recall that data by getting the text file contained in the groupName folder.

I am trying to use the "put URL" command to save the file. According to the LiveCode documentation, "put" creates a container (file) if one does not exist. Indeed, when I use this command:

put cd field "Temp" into url("file:" & specialfolderpath("documents") & "/someData.txt")

A file "someData.txt" is created and filled with the saved text in the Documents folder on my Mac. But this does not work:

put cd field "Temp" into url("file:" & specialfolderpath("documents") & "/AppNameFiles" & slash & quote & tGroupNameFolder & quote & "/someData.txt")

Is there a way for the "put" command to create a directory I can use late to retrieve text files for use in my app? If not, how is it possible to make one?

Is there a way for me to see the "Documents" directory structure in an iPad app?

When I use the "put URL" command, do I need to "set the defaultFolder to specialFolderPath…" whatever?

Thanks,

Craig H

Re: Using the put URL command to save files

Posted: Thu Dec 29, 2011 4:36 pm
by Klaus
Hi Craig,

Code: Select all

...
put cd field "Temp" into url("file:" & specialfolderpath("documents") & "/AppNameFiles" & slash & quote & tGroupNameFolder & quote & "/someData.txt")
...
I guess that "tGroupNameFolder" is a variable, right?
In that case there are NO quotes allowed!

Try this:

Code: Select all

...
put specialfolderpath("documents") & "/AppNameFiles/" & tGroupNameFolder & "/someData.txt" into tUrl
put field "Temp" into url("file:" & tUrl)
...
Best

Klaus

Re: Using the put URL command to save files

Posted: Fri Dec 30, 2011 4:25 am
by Colorado
Thanks for the reply, Klaus.

Unfortunately, it did not work. I got the error: "can't open file". Here is the entire block of code that does not work:

on mouseUp
local tSetNameFolder, tUrl
put cd field "Set Name" into tSetNameFolder
if tSetNameFolder is "" then
ask "You haven't put anything into the Set Name field. What do you wish to name this set?"
put it into cd field "Set Name"
put it into tSetNameFolder
end if
--set the defaultFolder to specialFolderPath ("documents") -- is this needed?
-- put cd field "Temp" into url("file:" & specialfolderpath("documents") & "/sibylData.txt") -- this works

put specialfolderpath("documents") & "/SibylFiles/" & tSetNameFolder & "/sibylData.txt" into tUrl -- from Klaus
put field "Temp" into url("file:" & tUrl) -- from Klaus
put the result -- to see the error, if any, in the message box
put tSetNameFolder & return after cd field "Saved Sets"
end mouseUp

Note that the line with the comment "-- this works" does work. It appears the "put URL" structure is incapable of more than one layer in the directory hierarchy. Would open file, write to file, close file work? In iOS on an iPad? Any other suggestions?

Thanks,

Craig H

Re: Using the put URL command to save files

Posted: Fri Dec 30, 2011 1:09 pm
by Klaus
Hi Craig,

please doublecheck that THERE IS A FOLDER first -> specialfolderpath("documents") & "/SibylFiles/" & tSetNameFolder
Put URL does create the FILE if not present, but does not create non existent folders!

If there is NOT that folder, you need to create it first and THEN put somehting into it!

HINT:
The iOS filesystem is CASE sensitive!
So please do also check and take this into account!

Code: Select all

on mouseUp
  local tSetNameFolder, tUrl
  put cd field "Set Name" into tSetNameFolder
  if tSetNameFolder is "" then
    ask "You haven't put anything into the Set Name field. What do you wish to name this set?"

   ## Doublecheck user input!!! ALWAYS!!! :-)
   if it = empty then
        BEEP
        exit mosueup
    end if

    put it into cd field "Set Name"
    put it into tSetNameFolder
  end if
 
## set the defaultFolder to specialFolderPath ("documents") -- is this needed?
## No you can accesss the folder directly!
  
  put specialfolderpath("documents") & "/SibylFiles/" & tSetNameFolder & "/" into tUserFolder
  if there is not a folder tUserFolder then
    create folder tUserFolder
  end if
  
  put tUserFolder & "/sibylData.txt" into tUrl -- from Klaus

  put field "Temp" into url("file:" & tUrl) -- from Klaus
  put the result -- to see the error, if any, in the message box
  put tSetNameFolder & return after cd field "Saved Sets"
end mouseUp

Best

Klaus