saving a file based on a previous path

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
mattmaier
Posts: 109
Joined: Fri Apr 19, 2013 2:49 am

saving a file based on a previous path

Post by mattmaier » Mon May 06, 2013 5:01 pm

I'm trying to put together a simple text editor. There's a field, a "save as" button and a "save" button.

The "save as" button works:

Code: Select all

on mouseUp
   ask file "Please name the file:"
   put field "Text" into URL ("file:" & it & ".txt")
   set lastSavePath of button "Save As" to ("file:" & it & ".txt")
end mouseUp
The "save" button doesn't compile:

Code: Select all

on mouseUp
   put field "Text" into URL (lastSavePath of button "Save As")
end mouseUp
The error code is: button "save": compilation error at line 2 (expression: missing ')' before factor) near "of", char 31

I can find plenty of examples of how to put data into text files, but very few examples involve pulling the file path out of where it's been saved, so I assume the error is in my hacked boolean operation there.

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: saving a file based on a previous path

Post by Klaus » Mon May 06, 2013 5:47 pm

Hi Matt,

you are saving the CONTENT of that file into the custom property!

Code: Select all

on mouseUp
   ask file "Please name the file:"

   put it into tFile
   if tFile = empty then
       exit mouseup
   end if
   ### !!! ;-)

   put field "Text" into URL ("file:" & tFile & ".txt")
  
   ## Only store the PATH -> without "file:"!
   set lastSavePath of button "Save As" to (tFile & ".txt")
end mouseUp

on mouseUp
   put the lastSavePath of button "Save As" into tFile
   put field "Text" into URL("file:" & tFile)
end mouseUp
Best

Klaus

mattmaier
Posts: 109
Joined: Fri Apr 19, 2013 2:49 am

Re: saving a file based on a previous path

Post by mattmaier » Mon May 06, 2013 6:54 pm

Hey Klaus,

Thanks, that worked. The first time I tried to retype it all I got the same error, but I realized I was not putting "the" before "lastSavePath". Apparently that's important.

Why did you put "it" into "tFile" after the user closes the dialog?

The "save" button never compiled, so I never got to check, but why would my original code be putting the field's contents into the custom property if I used the URL command?

So...it's mostly working. As long as the "save as" button is used once, and then only the "save" button is used from then on, it works correctly. If "save as" is used again, and you save over the existing file, it starts adding an extra ".txt" and actually creates new files. The very first time the user selects the destination folder and types in a file name, the path is just the file path and name without any extension. The second time, because the file already exists, the path includes the file extension (.txt).

Is there a command to capture the path without its file extension? Or do I have to deal with either case?

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: saving a file based on a previous path

Post by Klaus » Mon May 06, 2013 7:06 pm

Hi Matt,
mattmaier wrote:Hey Klaus,

Thanks, that worked. The first time I tried to retype it all I got the same error, but I realized I was not putting "the" before "lastSavePath". Apparently that's important.
Yes, custom properties are addressed with a leading "THE XYZ of...", otherweise the engine thinks this is a variable!
mattmaier wrote:Why did you put "it" into "tFile" after the user closes the dialog?
Old habits! :D
I know today that IT changes when you least exspect it, so I NEVER use IT in more than one line! :D
mattmaier wrote:The "save" button never compiled, so I never got to check, but why would my original code be putting the field's contents into the custom property if I used the URL command?
Yep, with the keyword URL you are reading the content of files!
mattmaier wrote:So...it's mostly working. As long as the "save as" button is used once, and then only the "save" button is used from then on, it works correctly. If "save as" is used again, and you save over the existing file, it starts adding an extra ".txt" and actually creates new files. The very first time the user selects the destination folder and types in a file name, the path is just the file path and name without any extension. The second time, because the file already exists, the path includes the file extension (.txt).
Is there a command to capture the path without its file extension? Or do I have to deal with either case?
Well, check for the suffix before adding it!

Code: Select all

on mouseUp
   ask file "Please name the file:"

   put it into tFile
   if tFile = empty then
       exit mouseup
   end if
   ### !!! ;-)

  ## Only store the PATH -> without "file:"! BEFORE ADDING THE SUFFIX
   set lastSavePath of button "Save As" to tFile)

  ## Check if user added suffix:
  if NOT tFile ends with ".txt" then
       put ".txt" after tFile
  end if

   put field "Text" into URL ("file:" & tFile)   
end mouseUp

on mouseUp
   put the lastSavePath of button "Save As" into tFile

   if NOT tFile ends with ".txt" then
       put ".txt" after tFile
   end if
   put field "Text" into URL("file:" & tFile)
end mouseUp
Not sure if I understood your problem correctly however... 8)


Best

Klaus

mattmaier
Posts: 109
Joined: Fri Apr 19, 2013 2:49 am

Re: saving a file based on a previous path

Post by mattmaier » Mon May 06, 2013 7:32 pm

Klaus, thanks so much for pointing me in the right direction. You save me a lot of time sifting through documentation. Particularly the "not" and "ends with" syntax. I figured there had to be something simple like that but couldn't find it.

This combination is working correctly:
Save As button

Code: Select all

on mouseUp
   ask file "Please name the file:"
   
   --exit procedure if the user didn't select anything
   put it into tFile
   if tFile = empty then
      exit mouseUp
   end if
   
   --check to see if the file path/name has the .txt suffix
   if NOT (tFile ends with ".txt") then
      put ".txt" after tFile
      end if
   
   --save the contents of the field into the user-specified file
      put field "Text" into URL ("file:" & tFile)
      
      --store the path without "file:"
   set lastSavePath of button "Save As" to (tFile)
end mouseUp
and save button

Code: Select all

on mouseUp
   put the lastSavePath of button "Save As" into tFile
   put field "Text" into URL ("File:" & tFile)
end mouseUp

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: saving a file based on a previous path

Post by Klaus » Mon May 06, 2013 9:13 pm

Okie Dokie :D

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7228
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: saving a file based on a previous path

Post by jacque » Tue May 07, 2013 6:44 pm

mattmaier wrote: I realized I was not putting "the" before "lastSavePath". Apparently that's important.
Jacque climbs on soapbox:

This is one of my pet peeves. I see code examples here all the time where "the" is omitted. "The" should never be omitted, even though the engine will happily parse it for you -- sometimes -- if it is missing.

But the primary reason to include "the" -- at least, here on the forums where newcomers learn their habits -- is because it isn't intuitive when you can safely omit it and when you can't. "The" is required for custom properties, but isn't for built-in functions. (And custom functions can't use "the" at all, though I think they should.) By including it in all cases where it works, and excluding it only for custom functions, the above misunderstanding could be avoided.

</soapbox>
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”