One field, two environments?

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
dalkin
Posts: 176
Joined: Wed Jul 04, 2007 2:32 am
Location: Blackheath, Australia
Contact:

One field, two environments?

Post by dalkin » Thu Sep 16, 2021 10:17 am

I have a button scripted that loads a folder of audio files into a field, then when the selected line is clicked, it plays the audio file thru a player. The button script is

Code: Select all

on mouseUp
   answer folder "Choose the folder containing the sound files?"
   if the result is not cancel then
      set the cSoundFilesFolder of this card to it
      set the defaultFolder to it
      put the files into tFiles
      put return before line 1 of field "playlist"
      put the files into line 1 of field "playlist"
      filter lines of field "playlist" with regex pattern "(?i)\.(wav|aif|wmv|mp3|wma)$"
      put the longFilePath of tFiles into fld "URL"
   end if
end mouseUp
and the field script is:

Code: Select all

on mouseUp
   put the selectedText of me into tFile
   set the filename of player "P2" to tFile
   set the filename of player "P2" to (the cSoundFilesFolder of this card & "/"& tFile)
   put the cUserDefinedName of me into tUserDefindName
   start player P2
end mouseUp
I want to add another button to either select a single file OR drag and drop a single (maybe multiple) files into the same field but the cSound and cUser code is messing with things. I can't seem to find a way clear. Any advice much appreciated.
If we're treading on thin ice, well you might as well dance.

stam
Posts: 2638
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: One field, two environments?

Post by stam » Thu Sep 16, 2021 10:39 am

Hi dalkin,

if i've understood correctly (and that may not be the case!) your current system relies on storing a folder reference for the folder with the sound files, but you also want the ability to store other sounds files?

If my understanding is correct, then would it not make more sense to store the file references in an array (possibly as a custom property or in memory) that takes the full file references from your folder selector but to which you can then append further files?

Stam

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

Re: One field, two environments?

Post by Klaus » Thu Sep 16, 2021 10:43 am

Hi dalkin,

please revisit your "images in standalone" thread! 8)

1. put the longFilePath of tFiles into fld "URL"
This function -> longFilePath will only work for ONE file, so only the first file in tFiles will have an absolute pathname.

2.

Code: Select all

...
## This will not work and may even throw an error, so just leave this line out!
## See above for -> longFilePath
## set the filename of player "P2" to tFile
set the filename of player "P2" to (the cSoundFilesFolder of this card & "/"& tFile)
...
3. We don't need to mess around with the DEFAULTFOLDER anymore, we have a new funtion -> files(the_folder_with_the_files)

Code: Select all

on mouseUp
   answer folder "Choose the folder containing the sound files?"
   if the result <> "Cancel" then
      set the cSoundFilesFolder of this card to it
      put the files(IT) into tFiles
      
      ## For speed use VARIABLES instead of accessing fields!
      filter lines of tFiles with regex pattern "(?i)\.(wav|aif|wmv|mp3|wma)$"
      put tFiles & CR before line 1 of field "playlist"
      
      ## See above
      ## put the longFilePath of tFiles into fld "URL"
   end if
end mouseUp
4.
I want to add another button to either select a single file OR drag and drop a single (maybe multiple) files into the same field but the cSound and cUser code is messing with things.
cSound and cUser code?
Sorry, no idea what you are talking about!? :-)


Best

Klaus

dalkin
Posts: 176
Joined: Wed Jul 04, 2007 2:32 am
Location: Blackheath, Australia
Contact:

Re: One field, two environments?

Post by dalkin » Fri Sep 17, 2021 4:50 am

Many thanks Klaus. Your code works for a folder of sound files but what changes would I make to be able to select a single sound file from a folder of sound files? And any advice on how to enable drag and drop of a single file?

The cSound and cUser were meant to reference the longer cSoundFilesFolder and cUserDefinedName.
If we're treading on thin ice, well you might as well dance.

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

Re: One field, two environments?

Post by Klaus » Fri Sep 17, 2021 9:23 am

Hi dalkin,

AHA! :-)

Hm, you cannot select a file OR folder in the same script, except you add a modifier key, like ALT-Click for single files.

Code: Select all

on mouseup
  if the ALTKEY = "down" then
   answer file...
  ...
  else
    answer folder...
...
end mouseup
Drag and drop is easy.
Add this to your card (or stack) script:

Code: Select all

on dragenter
  set the dragaction to "copy"
end dragenter

on dragdrop
  ## Only ONE file at a time:
  put LINE 1 OF the dragdata("files") into tFiles

  ## Or this if you want the user to drop several files:
  ## put the dragdata("files") into tFiles
  ## Then you need a repeat loop over these list like below
  set itemdel to "."
  if item -1 of tFiles is in "wav.aif.wmv.mp3.wma" then
    ## do something with tFiles
  end if
end dragdrop
But yes, that will collide with your cSound etc. code.

I ususally store the filename AND the full path to the file in a list field.
I set the first TAB of that field to a high number like 1000 so the user
will only see the actual filename. Know what I mean?
sound.mp3 TAB full/path/to/sound.mp3
...

Then you can do this in your listfield:

Code: Select all

on mouseUp
   set itemdel to TAB
   put the selectedText of me into tFile
   set the filename of player "P2" to item 2 of tFile
   put the cUserDefinedName of me into tUserDefindName
   start player P2
end mouseUp
Use a repeat loop to modify your list of files after selecting a folder.
Drop a line if you need help for this.


Best

Klaus

dalkin
Posts: 176
Joined: Wed Jul 04, 2007 2:32 am
Location: Blackheath, Australia
Contact:

Re: One field, two environments?

Post by dalkin » Fri Sep 17, 2021 9:32 am

Many thanks Klaus. But now the big question. Would you prefer to be remembered for your coding skills or your guitar playing. Take your time.
If we're treading on thin ice, well you might as well dance.

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

Re: One field, two environments?

Post by Klaus » Fri Sep 17, 2021 9:33 am

Coding skills and BASS guitar playing, please! 8)

dalkin
Posts: 176
Joined: Wed Jul 04, 2007 2:32 am
Location: Blackheath, Australia
Contact:

Re: One field, two environments?

Post by dalkin » Sat Sep 18, 2021 10:22 am

I can't get this to work unfortunately. I've attached a snapshot showing the result.
error.jpg
I should also let you know that this morning I took out the LC email support package so hopefully, requests on your time will fade but thank you so much for your help.

You too stam and others.
If we're treading on thin ice, well you might as well dance.

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

Re: One field, two environments?

Post by Klaus » Sat Sep 18, 2021 12:45 pm

Please read #1 again here:
viewtopic.php?f=7&p=208929#p208863
8)

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

Re: One field, two environments?

Post by Klaus » Sat Sep 18, 2021 12:49 pm

Oh and change this:

Code: Select all

...
else
  answer folder "Choose folder containing sound files!"
  if the result <> "Cancel" then
    set the cSoundfolder of this cd to IT
    ## put the files into tFiles
    put FILES(IT) into tFiles
  ...

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”