renaming a file and using fields?

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
peter.s
Posts: 99
Joined: Thu Jul 10, 2008 11:47 am

renaming a file and using fields?

Post by peter.s » Thu Jul 10, 2008 2:19 pm

Hi There,

I'm a novice here. I installed the trial version of Revolution Studio about four days ago and it has totally consumed me!

I have had moderate success with my test project so far, but I seem to have hit a brick wall with something seemingly simple. Actually three things:

Q1. How do I get the contents of a field to become the new name of a file?

I can get my test program to create a file with a temporary name. I can rename that file with a predetermined new name eg: rename file "/Folder/FileName.mov" to "/Folder/NewFileName.mov"... But I want the User to put their own NewName into a field and then the contents of that field to become the new name of the file. I do not want to use the default Save dialogue - so I am trying to make my own and this is the last piece of the puzzle. I've looked all through the Help resources and can't seem to get it.

Q2. How do I get the Cursor to automatically appear in a Field when a new window (Stack) is opened (and contains that Field)?

Q3. How do I get a List Field to display the contents of a Folder? Clicking the Folder icon in the Field's Property Inspector suggests it will let me select a folder, but it doesn't seem to work that way... or am I missing something?


Many thanks in advance to anyone who is willing to help.

P

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

Post by Klaus » Thu Jul 10, 2008 3:00 pm

Hi Peter,

welcome to the show :-)

Here we go:

1. You can put the field into a variable then use the "rename" command, see the docs...

Code: Select all

...
put fld "user input" into tNewFileName
rename file "path/to file/here.xyz" to tNewFileName
...
Of course you should check the field for "illegal" characters like return (CR), TAB,/,: etc...

2. You can "set" the cursor maybe "on opencard" via script in your card script:

Code: Select all

on opencard
  select text of fld "your field here"
  ...
end opencard
3. You have to script this.
The button in the inspector will only let you select a text file that will be put into that field!

You should write you a little function that returns the content of a folder:

Code: Select all

function folder_content theFolder
  ## theFolder is a valid foldername...

  put the directory into olddir
  ## Store the current directory and restore it after we are done!

  set the directory to theFolder

  ## Now get the files
  put the files into tFiles

  filter tFiles without ".*"
  ## optionally filter out all "invisible" files
  ## or only one special filetype or whatever...
 
  set the directory to olddir
  ## restore folder...

  return tFiles
  ## finally return the folders content
end folder_content

Then you can later use this like:

Code: Select all

on mouseup
  answer folder "Please select a folder to show its contents"
  if it = empty then
  ## User clicked CANCEL
    exit mouseup
  end if

  put folder_content(it) into fld "your field here"
end mouseup
Tested and works :-)

Hope that helps!


Best from germany

Klaus

peter.s
Posts: 99
Joined: Thu Jul 10, 2008 11:47 am

Post by peter.s » Thu Jul 10, 2008 3:38 pm

Hi Klaus

Thank you so much for your fast and detailed reply.
I will test this out now and let you know how I go...

Best from Cambodia.

Peter

peter.s
Posts: 99
Joined: Thu Jul 10, 2008 11:47 am

Post by peter.s » Thu Jul 10, 2008 4:00 pm

Hi again Klaus,

I have tried numbers 1 & 2 and failed. Neither of them work... I feel like I am close with number 1, but still missing (probably something obvious to an old hand but not obvious to me).

Before I try number 3 I just want to clarify a couple of things:

Where do I put the Function code? The Field in question is on my Main stack - and I want it to list the contents of a folder and be updates as soon as someone makes a new file in that folder - So, do I put this function statement in my Main stack? As in: on open stack function folder_content... etc?? Or does it go elsewhere?

Are you using the # (hash) symbols like the -- (double dash) symbols in the example scripts? To give comment but not script?

Thanks again.

Peter

peter.s
Posts: 99
Joined: Thu Jul 10, 2008 11:47 am

Post by peter.s » Thu Jul 10, 2008 4:37 pm

O.K. I'm getting somewhere with the renaming of files.

The code Klaus suggested does work but it renames the file and puts it in the Revolution default folder. So now I want to set my own default folder.
I tried this as part of the preopen of my main stack: set defaultfolder to "/Folder Name/" - it worked the first time around - But now, when I go through the process, my project crashes!

What am I doing wrong? Have I put the "set defaultfolder..." in the wrong place?

Peter

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

Post by Klaus » Thu Jul 10, 2008 5:52 pm

Hi Peter,

OK, looks like my hint with renaming needs some refinement,
since the user obviously only enters the filename and not the
complete path :-)

1.
E.g.
File to rename:
path/to/file.suffix

User enters this into the field:
my new file

Then you "build" the necessary complete path for renaming the file like:
...
put "path/to/file.suffix" into tFile2Rename_old
put tFile2Rename_old into tFile2Rename_new
# We change tFile2Rename_new later...

put fld "user input" into tNewFile
set itemdelimiter to "/"
put tNewFile into item -1 of tFile2Rename_new
# Yes this is the comment sign I use, one char less to type :-)
# Now tFile2Rename_new will hold: path/to/my new file
# ... item -1 ... is the last item!

# And now you can use this to rename the file correctamente!
rename file tFile2Rename_old to tFile2Rename_new
...

2.
You can also check "focus" in the docs to "activate" a field on opencard...

3.
Put the finction into the stack script of your mainstack.
This way all objects in this mainstack and its substacks can use it anywhere, anytime.

Hope this helps.


Best

Klaus

peter.s
Posts: 99
Joined: Thu Jul 10, 2008 11:47 am

Post by peter.s » Fri Jul 11, 2008 4:34 am

Hi Klaus,

Thank you so much for these instructions.

The file rename works great - what you describe makes perfect sense. I had been reading about scripts to change words in text, I just didn't make the connection.

Back to the List Field - Can you please explain the first few lines in the script?

function folder_content theFolder
  ## theFolder is a valid foldername...

Do you mean I should write: ... folder_content "/My Folder/" ?

I get an error message if I write this - But there is no other reference in the script to which folder I want to list. How else will the program know I want to list this folder?

Also - I'm not sure what the on Mouseup code is suggesting.
Can't I just put: put folder_content(it) into fld "your field here"
at the end of the function above?

Cheers,

Peter

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

Post by Klaus » Fri Jul 11, 2008 12:16 pm

Hi Peter,

good to hear the rename thing works :-)

"folder_content" is a function and this means it requires one (or more) parameter(s) in parenthesis!

See the mouseup script:

I first let the user select a folder with "answer folder ..."

Code: Select all

on mouseup 
  answer folder "Please select a folder to show its contents" 
  if it = empty then 
  ## User clicked CANCEL 
    exit mouseup 
  end if 

  put folder_content(it) into fld "your field here" 
end mouseup
When the user selects a folder, the variable IT will contain the
absolute path to the selected folder like:
/Users/klaus/Desktop
## on a mac

C:/Folder1/Folder2
## on a win pc

When the user clicks "Cancel", the variable IT will be empty, that's why
I "exit" the script in the above example!

Now I pass the selected folder to the function, which now actually reads:
folder_content("/Users/klaus/Desktop")
## This is a string and MUST be a valid folder as an absolute or relative path

See "ask/anser file/folder" in the docs.
Also - I'm not sure what the on Mouseup code is suggesting.
Can't I just put: put folder_content(it) into fld "your field here"
at the end of the function above?
If you are using this thing only once, you can omit the function and put
everything into the mouseup script like:

Code: Select all

on mouseUp
  answer folder "Please select a folder to show its contents"
  if it = empty then
    ## User clicked CANCEL
    exit mouseup
  end if
  
  put the directory into olddir
  ## Store the current directory and restore it after we are done!
  
  set the directory to it
  
  ## Now get the files
  put the files into tFiles
  
  filter tFiles without ".*"
  ## optionally filter out all "invisible" files
  ## or only one special filetype or whatever...
  
  put tFiles into fld "your field here" 
end mouseUp
BUT if you need this functionality (a folders content) more than once in your project(s),
a function is the way to go. Functions are more versatile and can be enhanced later
if needed

Example:
Later in your project you decide that you also need a listing of FOLDERS inside a
give folder instead fo files. Then you could write another handler/function that does
what you need or you simply add a nother parameter to your existing function like:

Code: Select all

function folder_content tFolder,tKind_of_Content
 put the directory into olddir 
  ## Store the current directory and restore it after we are done! 

  set the directory to theFolder 

  ## Now get the desired content  
  if tKind_of_Content = "Folders" then
    put the folders into tRetValue
  else
    put the files into tRetValue
  end if

  filter tRetValue without ".*"
  return tRetValue
end folder_content
Now you have ONE function for two or even more purposes :-)
Use it like this for files:
folder_content("path/to/folder")
## Just leave the second parameter = default, see scipt above
OR
folder_content("path/to/folder","Files")

Or for folders:
folder_content("path/to/folder"."Folders")

You get the picture :-)


Best

Klaus

peter.s
Posts: 99
Joined: Thu Jul 10, 2008 11:47 am

Post by peter.s » Sat Jul 12, 2008 2:40 am

Hi Klaus,

Yes I do get the picture.

I'm going to run some experiments over the weekend and see where I get to. I feel like a child playing with building blocks while not understanding the force of gravity. It'll come to me though.

Thanks again Klaus, you've been a great help!

Peter

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

Post by Klaus » Sat Jul 12, 2008 11:11 am

At your services, Sire! :-)

Post Reply