Populating the Video Files Menu
Now that you have created a menu (the list field) to display your videos we will look at how to populate the menu so that it displays a list of available videos.
This lesson will introduce revTalk for the first time in the application. revTalk is the language you use in Revolution to control what the application does and how it responds to user actions.
Edit The Card Script
So far we have discussed the Stack window, the Player and the List Field. Now we will introduce the Card object. A Stack window is made up of one or more Cards. A Card can contain multiple objects such as a Player or Field.
We are going to add the revTalk necessary to populate the List Field to the Card Script. The Card Script stores revTalk that can affect any object on the card.
To edit the Card Script choose Object > Card Script.
The Script Editor Opens
When you edit the script of an object you use the Script Editor. The Script Editor enables you to write revTalk that affects the behavior of your application. Notice that a tab is open in the Script Editor for the Stack window's card (1). We will begin typing revTalk into the script field (2).
The Card Script
Below you will find all of the revTalk that goes in the Card script. If you would just like to copy and paste all of the code into the Card script without worrying about what it does then you can do so. If you would like to walk through writing the revTalk that goes in the Card script then skip the next step go to the next lesson which describes the Card script in detail.
Copy and paste the following revTalk into the Card script. After pasting the revTalk into the Card script the dot in the Card's tab will turn yellow (1). Click the Compile button (2) so that Revolution applies the changes you've made to the Card's script.
Copy & Paste Into Card Script
on preOpenCard
## Load the video list
uiPopulateVideoList
## If there is at least 1 video in the list then load the video
if the text of field "Video Menu" is not empty then
set the hilitedline of field "Video Menu" to 1
uiLoadSelectedVideo
end if
end preOpenCard
command uiPopulateVideoList
## Get list of video files in Video folder
put GetVideoFiles() into theFiles
## Assign the list of files to the list field (our menu)
set the text of field "Video Menu" to theFiles
end uiPopulateVideoList
function GetVideoFiles
## Get the path to the "Video" folder on disk
put GetPathToFolder("Videos") into theFolder
## Get list of files in the folder
put GetFilesInFolder(theFolder) into theFiles
## Return list of files
return theFiles
end GetVideoFiles
function GetPathToFolder pFolderName
## Retrieving paths to folders that are relative to the stack can be tricky.
## Determine the location of this stack on disk
put the filename of this stack into theFolder
## Folder paths use "/" to separate each folder in the path
## By setting the itemDelimiter to slash we can refer to
## individual sections of the path by the 'item' token in revTalk.
set the itemDelimiter to slash
## When you build a standalone version of this stack on OS X the stack
## file will be located in side of an application bundle. These next few
## lines strip the application bundle portion of the path off.
if the platform is "MacOS" then
if theFolder contains ".app/Contents/MacOS" then
## Delete the last three items of the path that are specific
## to the application bundle
delete item -3 to -1 of theFolder
end if
end if
## Replace the last item in theFolder with the 'pFolderName' parameter
put pFolderName into the last item of theFolder
## Return the complete path
return theFolder
end GetPathToFolder
function GetFilesInFolder pFolder
## This function uses the default folder to get a list of files
## Store the original default folder so we can return to it later
put the defaultfolder into theOrigDefaultFolder
## Change the default folder to the folder passed into the function (pFolder)
set the defaultfolder to pFolder
## 'the files' always returns the files in the 'default folder'
put the files into theFiles
## Restore the original 'default folder' setting
set the defaultfolder to theOrigDefaultFolder
## Filter out invisible files (files that start with a "." in their name) from 'theFiles' variable
filter theFiles without ".*"
## Return the list of files to the caller of the function
return theFiles
end GetFilesInFolder
command uiLoadSelectedVideo
## Get the name of the video selected in the video menu
put the selectedtext of field "Video Menu" into theVideoName
put "Videos/" & theVideoName into theVideoFile
## Set 'the filename' property the player object to the relative video path
## Revolution will locate the video as long as the "Videos" folder is
## alongside the stack file or the application executable.
set the filename of player "My Video" to theVideoFile
## Reset the time of the Player object to 0
set the currenttime of player "My Video" to 0
end uiLoadSelectedVideo
Rory
How do I get the code above to copy correctly? At the moment it copies all on one line and has '?' at the end of every line of code?
Hanson Schmidt-Cornelius
Hi Rory,
you can copy the code by first placing it in a text editor and the copying it from there into the LiveCode script editor. This should remove the '?' for you.
Kind Regards,
Hanson
Daniel
The list of videos in my video file doesn't appear I just get:
about.text
license agreement.txt
livecode.exe
etc....
How do I fix this?
Elanor Buchanan
Hi Daniel
It sounds like the line of code where you set the defaultFolder property is failing, these are the files in the LiveCode distribution which is what the defaultFolder is set to when LiveCode starts up so it sounds like it is not changing successfully.
Double check that line of code, and the folder path you are using there and you should be able to get it working.
Kind regards
Elanor
Rachel
Would someone please show what to do in code. I don't understand Elanor's solution. I have followed the tutorial and everything works fine until I install it on my Android Phone. Then, I get the same list as Daniel shows above.
It would be nice to have more explanation the folder and file paths. Something that shows a non defaultFolder or, at least, demonstrates what a defaultFolder property is.
Regards,
-Rachel
Elanor Buchanan
Hi Rachel
Getting the files will always return the files that are in the default folder, this is the folder the app is currently 'looking in', if that makes sense. If you want to app to get the files in a particular folder you have to set the defaultFolder property to the folder you want to look in.
You might find these lessons useful, they go into the files and folders options in a bit more detail
http://lessons.livecode.com/m/4071/l/6929-how-to-list-the-files-in-a-folder
http://lessons.livecode.com/m/4071/l/16635-file-and-folders-part-1
http://lessons.livecode.com/m/4071/l/16635-file-and-folders-part-2
There is now an optional targetFolder parameter for the files function
put files()
You can read a bit more about this in the Dictionary entry for the files function.
Please note that this lesson is aimed at Desktop platforms, if you want to run it on Android you'll need to ensure you set the folder to something relevant, you might want to use the specialFolderPath function to find the path to the folder you want.
I hope that is some help.
Kind regards
Elanor