Writing to a file

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Writing to a file

Post by mwieder » Tue Jun 21, 2016 8:02 pm

Ah. OK. In that case you have two choices:

1. Make newSFP a global variable
2. Pass newSFP as a parameter to the function that deletes the folder.

The first option is easier to implement, although a little quirky in xtalk, but is best avoided in general because of scope issues.
The second one requires a bit of refactoring of your code, so it's more involved, but is a better general-purpose solution.

one possible Solution 1:
put this line into the mouseUp handlers of both buttons:

Code: Select all

global newSFP
a better Solution 1:
in button "btnDelete"
add this line before the "delete Folder" line:

Code: Select all

put field "Field2" into newSFP
one possible Solution 2:
in the card script:

Code: Select all

local sNewSFP -- this is now a script-local variable, available to all card script handlers
command deleteThisFolder
  delete folder sNewSFP
end deleteThisFolder

command storeThisFolder pFolder
  put pFolder into sNewSFP
end storeThisFolder
in button "btnDelete":

Code: Select all

deleteThisFolder
in button "btnMakeDirectory":
put this line after "put newSFP into field Field2":

Code: Select all

storeThisFolder newSFP
Or experiment with other variations on the above possibilities.

RossG
Posts: 247
Joined: Thu Jan 08, 2015 7:38 am

Re: Writing to a file

Post by RossG » Tue Jun 28, 2016 7:32 am

Next step: to open the file just created with

put field tProgression into URL ("file:" & newSFP & "/" & fName)

Tried all the combinations I can think of but no luck so far.

Suggestions appreciated.

Win XP (for ever).
Is age an excuse? Eighty-four and counting.
Programming powered by coffee.

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Writing to a file

Post by [-hh] » Tue Jun 28, 2016 8:12 am

shiftLock happens

rumplestiltskin
Posts: 223
Joined: Wed Jun 21, 2006 7:33 pm
Contact:

Re: Writing to a file

Post by rumplestiltskin » Wed Jun 29, 2016 4:11 am

Maybe I'm missing something. Using the example in the first link I am trying this script in a button:

Code: Select all

   put field "field1" into URL "file:/MacHD/Users/blevine/Desktop/myfile.txt"
   answer the result
The result is "can't open file". I don't see anything in the lesson about having to open the file first before writing. There is already a file named "myfile.txt" I created in that location using TextEdit.

Thanks,
Barry

RossG
Posts: 247
Joined: Thu Jan 08, 2015 7:38 am

Re: Writing to a file

Post by RossG » Wed Jun 29, 2016 4:23 am

Should have made it clear that I want the file
to open in Notepad or whatever is set as the
default for .txt files.
Is age an excuse? Eighty-four and counting.
Programming powered by coffee.

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: Writing to a file

Post by AxWald » Wed Jun 29, 2016 9:44 am

Hi,
RossG wrote:Should have made it clear that I want the file to open in Notepad or whatever is set as the default for .txt files.

Code: Select all

get shell("MacHD/Users/blevine/Desktop/myfile.txt")
should do the trick ...

Have fun!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Writing to a file

Post by [-hh] » Wed Jun 29, 2016 10:09 am

Here works also, on Mac/Win/Linux

launch document <path>

where <path> is a directoryPath or filePath that will be
opened using the current system's launch services.

There is also "launch URL <theURL>" if you prefer URL-syntax.
If using Windows, you could also have a look at "relaunch" in the dictionary.

p.s On MacOS, I use to precede disk names with "/Volumes/".
For example
launch document "/Volumes/Macintosh HD"
opens "/" with application "Finder" (if 'Macintosh HD' is your only internal disk).
Works also if the directory path has a trailing slash.
shiftLock happens

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Writing to a file

Post by jacque » Wed Jun 29, 2016 5:56 pm

The best way to write to the desktop is to let the engine determine the file path for you. Use specialFolderPath to get the correct path on any OS.

Code: Select all

put field "field1" into URL (specialFolderPath("desktop") & "/myfile.txt") 
SpecialFolderPath provides locations for several other commonly used locations also.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Writing to a file

Post by Klaus » Wed Jun 29, 2016 8:31 pm

Erm, better add FILE:
jacque wrote:

Code: Select all

put field "field1" into URL ("FILE:" & specialFolderPath("desktop") & "/myfile.txt") 
8)

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Writing to a file

Post by mwieder » Wed Jun 29, 2016 9:00 pm

LOL.
Better than best, eh?

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Writing to a file

Post by Klaus » Wed Jun 29, 2016 10:46 pm

mwieder wrote:LOL.
Better than best, eh?
Hey, my name is Major, what do you exspect? :D

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Writing to a file

Post by jacque » Thu Jun 30, 2016 4:25 pm

Oops. I've been bested.

Was writing on my tablet and to save time I copied the original and edited it. Incorrectly.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Writing to a file

Post by mwieder » Thu Jun 30, 2016 9:07 pm

Well, Klaus has over 8500 posts to his credit.
He has to be right once in a while. :P

RossG
Posts: 247
Joined: Thu Jan 08, 2015 7:38 am

Re: Writing to a file

Post by RossG » Fri Jul 01, 2016 3:39 am

Progression Designer v1.3.zip
(4.32 KiB) Downloaded 248 times
Thanks for your efforts gentlemen but
I'm still struggling with this one.

So attached is the stack in question.

Put some numbers into the four fields.
I suggest 4,11,35 and 10.

Click the "Calculate Progression" button.
Click the "Save as..."
Label will change to "Open...."
I want this button to open the file just saved in Notepad
or the default prog. for .txt files.

The "HereYouAre" field is temporary for testing.

BTW I programmed this some years ago and now have no
idea how or why it works but it does...
Is age an excuse? Eighty-four and counting.
Programming powered by coffee.

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Writing to a file

Post by mwieder » Fri Jul 01, 2016 6:01 am

First of all, some code style tips:
Press the tab key while in the editor and it will automatically format the text nicely for you.

Next, you'll stay out of trouble a bit more if you put literal values in quotes.
Took me a bit to figure out that tArray and tProgression weren't local variables.

You have a line

Code: Select all

put 0 into cArray[x.1]
and I don't know what you intend by that. Do you mean x,1 instead of x.1?

(almost at the end) it's bad form to name a control as a keyword. Button "Save" is bound to cause trouble sometime.

Lastly, if you want to open the same file you save, you're missing a "/" delimiter between newSFP and fName in three lines.
Cheers

Post Reply