loading and saving a text file and loading a graphic

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

Post Reply
admin12
Posts: 412
Joined: Wed May 11, 2011 9:47 am

loading and saving a text file and loading a graphic

Post by admin12 » Sat Jul 02, 2011 6:37 pm

Couple of things.

I need to write a text file (say, back.txt) with the file location of a graphic in the image control, imgBack, to the c: drive. The graphic is the background on all cards. I need my program to automatically load that background graphic when it first opens - and it will use the text file (back.txt) for the file location. Finally, it will take that file and insert it into the image placeholder called imgBack. Every card will have a image called imgBack. When someone wants to change the background, they click a button (in admin section) and choose another graphic, which is automatically inserted into the imgBack image control on all cards and the file and path info of the graphic file is updated in the text file (back.txt).

First, is there an easier way to do this than I mention above. If not, how would I go about doing the above?

Thanks for your help.

Mike

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: loading and saving a text file and loading a graphic

Post by Mark » Mon Jul 04, 2011 12:57 pm

Mike,

I have read your message several times and thought about it for a few days. I can't figure out why you have a problem. Don't you know how to save data to a file? Can't you figure out what the location of your image file is? Why is this a problem at all?

Having no clue of what you are trying to do, I'd suggest you simply save the id number or the name of the image in a preferences file in the preferences folder on Mac OS X, the application data folder on Windows or the Home folder on Linux (e.g. ~/home/.yourAppPrefs).

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

kray
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 56
Joined: Sat Apr 08, 2006 5:28 pm
Location: Eau Claire, WI
Contact:

Re: loading and saving a text file and loading a graphic

Post by kray » Mon Jul 04, 2011 3:11 pm

admin12 wrote:Couple of things.

I need to write a text file (say, back.txt) with the file location of a graphic in the image control, imgBack, to the c: drive. The graphic is the background on all cards. I need my program to automatically load that background graphic when it first opens - and it will use the text file (back.txt) for the file location. Finally, it will take that file and insert it into the image placeholder called imgBack. Every card will have a image called imgBack. When someone wants to change the background, they click a button (in admin section) and choose another graphic, which is automatically inserted into the imgBack image control on all cards and the file and path info of the graphic file is updated in the text file (back.txt).

First, is there an easier way to do this than I mention above. If not, how would I go about doing the above?

Thanks for your help.

Mike
Hey Mike... You are going in basically the right direction; the only question I have is what the purpose of the text file is. The reason I ask is that there are two methods of getting external image data into LC - the first is an import, which embeds the image into your stack. The second is a *reference*, which leaves the image file on disk and does not embed the image data into the stack. Based on your description, it sounds like you'd be best served with the second method, which just requires setting the "filename" of an image object to a path. Since you can also "get the filename" of the same image object to get the path that it came from, you don't need an external text file.

Here's how that would work:

1) Create an empty image object and set it to be the size of the card.

If the images to be imported are the exact same size as the card, then you don't need to do anything. But if you are going to allow for importing different sized images, you'll need to decide if you want the incoming image stretched to fill the card (in which case you need to set the 'lockLocation' property of the image to 'true'), or if you want the image to adjust its size based on the size of the incoming image (which might not fill the whole card), in which case you can leave the 'lockLocation' property set to 'false'.

2) Group it and set the margins of the group to 0, the layer of the group to 1 (so it's behind everything) and the backgroundBehavior of the group to "true".

If you only have a single card to start with, you can go to #3, otherwise you'll need to make sure the group is shared among all the current cards by going to each one and "placing" it (Menu: Object -> Place Group) and setting the place group's layer to 1 to move it to the back (if you need to).

3) In your Admin, after the user clicks the button and selects the image, assign the "filename" property of the image object to the path:

Code: Select all

on mouseUp
  answer file "Select an image:"
  if it <> "" then
    set the fileName of image 1 of group "whateverYouNamedYourGroup" to it
  end if
end mouseUp
If you ever need to find out the path to the image, just "get" the fileName of the same image.

Hope this helps,
Ken Ray
Sons of Thunder Software
Email: kray@sonsothunder.com
Web site: http://www.sonsothunder.com

admin12
Posts: 412
Joined: Wed May 11, 2011 9:47 am

Re: loading and saving a text file and loading a graphic

Post by admin12 » Mon Jul 04, 2011 6:26 pm

kray wrote:
admin12 wrote:Couple of things.

I need to write a text file (say, back.txt) with the file location of a graphic in the image control, imgBack, to the c: drive. The graphic is the background on all cards. I need my program to automatically load that background graphic when it first opens - and it will use the text file (back.txt) for the file location. Finally, it will take that file and insert it into the image placeholder called imgBack. Every card will have a image called imgBack. When someone wants to change the background, they click a button (in admin section) and choose another graphic, which is automatically inserted into the imgBack image control on all cards and the file and path info of the graphic file is updated in the text file (back.txt).

First, is there an easier way to do this than I mention above. If not, how would I go about doing the above?

Thanks for your help.

Mike
Hey Mike... You are going in basically the right direction; the only question I have is what the purpose of the text file is. The reason I ask is that there are two methods of getting external image data into LC - the first is an import, which embeds the image into your stack. The second is a *reference*, which leaves the image file on disk and does not embed the image data into the stack. Based on your description, it sounds like you'd be best served with the second method, which just requires setting the "filename" of an image object to a path. Since you can also "get the filename" of the same image object to get the path that it came from, you don't need an external text file.

Here's how that would work:

1) Create an empty image object and set it to be the size of the card.

If the images to be imported are the exact same size as the card, then you don't need to do anything. But if you are going to allow for importing different sized images, you'll need to decide if you want the incoming image stretched to fill the card (in which case you need to set the 'lockLocation' property of the image to 'true'), or if you want the image to adjust its size based on the size of the incoming image (which might not fill the whole card), in which case you can leave the 'lockLocation' property set to 'false'.

2) Group it and set the margins of the group to 0, the layer of the group to 1 (so it's behind everything) and the backgroundBehavior of the group to "true".

If you only have a single card to start with, you can go to #3, otherwise you'll need to make sure the group is shared among all the current cards by going to each one and "placing" it (Menu: Object -> Place Group) and setting the place group's layer to 1 to move it to the back (if you need to).

3) In your Admin, after the user clicks the button and selects the image, assign the "filename" property of the image object to the path:

Code: Select all

on mouseUp
  answer file "Select an image:"
  if it <> "" then
    set the fileName of image 1 of group "whateverYouNamedYourGroup" to it
  end if
end mouseUp
If you ever need to find out the path to the image, just "get" the fileName of the same image.

Hope this helps,
Thank you so much for your help. Although I have programmed in other languages, I have not programmed in Live Code before, so the transition is a bit confusing for me.

Now, this is going to deploy to web, so I am going to have to save a relative path, right? What is the best way to go from their computer to save it on the local server, or is there a better way? The recruiters want to be able to essentially 'skin' the background image - across all cards (and there will be quite a few). Even worse, there will probably be various front ends (using Live Code) to the database (mysql), so I will need to save that path and file info across different programs (all Live Code-based). That is why I was thinking of writing text files - like cookies.

Mike

Mike

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”