Page 1 of 1

Scrolling list for downloadable files

Posted: Mon Sep 01, 2014 7:25 am
by Appy777
Hi there,

I am very new to LiveCode and all development. I have read through as much info as I can for now (my head is swimming!) I have decided to jump in and get started with my first androidapp, and learn as I work starting today. Right now, I have far more questions than answers :-)

I would like to create a scrolling list field which can allow the user to select from a list of files and launch each file from within the app. I need the be able to close the file after they have viewed it (and print but that can come later):

I have set up the scrolling list with the correct dimensions and have populated it with names corresponding to 3 PDF files which are contained in a file named DocumentsLTR, which is contained in the same file as the main stack is saved. The files are named:
LTRTestFile1
LTRTestFile2
LTRTestFile3

I am currently implementing a scrolling list menu for videos (work in progress!), and wondered if I could use the same tutorial and swap the videos for files? The problem is that I am finding it a bit confusing, as it makes reference to Videos and Video Files etc and I am finding it difficult to differentiate what is a variable and what is not. Here is a link to the tutorial: http://lessons.runrev.com/m/754/l/6316- ... files-menu

And here is my card script "converted" for opening files rather than watching videos.
I have not tested this yet (having trouble with both my emulator and getting my Galaxy S4 to behave for testing on the device (that's another story!). I wonder if someone with a keen eye and a lot more knowledge than me could take a look and tell me if it will be possible to convert this script to open files instead of videos, and also if I am on the right track / have made any ridiculous errors! Any help would be much appreciated! Once I have the menus functioning I will feel like I am starting to win the battle!

Thanks in advance,

Prue. (card script below, and the button script is below that)



on preOpenCard
_## Load the file lister
_uiPopulateFileLister
_
end preOpenCard

command uiPopulateFileLister
_## Get list of Document files in DocumentsLTR folder
_put GetDocumentsLTRFiles() into theFiles
_
_## Assign the list of files to the list field (our menu)
_set the text of field "File Lister" to theFiles
_
end uiPopulateFileLister

function GetDocumentTRFFiles
_## Get the path to the "DocumentsLTR" folder on disk
_put GetPathToFolder("DocumentsLTR") into theFolder
_
_## Get list of files in the folder
_put GetFilesInFolder(theFolder) into theFiles
_
_## Return list of files
_return theFiles
end GetDocumentsLTRFiles

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 uiLoadSelectedDocument
_## Get the name of the document selected in the document menu
_put the selectedtext of field "File Lister" into theDocumentName
_put "Documents /" & theDocumentName into theDocumentFile
_
_## Set 'the filename' property the player object to the relative document path
_## Revolution will locate the video as long as the "Documents" folder is
_## alongside the stack file or the application executable.
_set the filename of File "My Document" to theDocumentFile
_
end uiLoadSelectedDocument




And for the button script:
on selectionChanged
uiLoadSelectedDocument
end selectionChanged

Re: Scrolling list for downloadable files

Posted: Mon Sep 01, 2014 12:56 pm
by Klaus
Hi Appy777,

just change the last line to:

Code: Select all

...
  ## set the filename of File "My Document" to theDocumentFile
  launch document theDocumentFile
end uiLoadSelectedDocument
That will open the file with the associated application.

Best

Klaus

Re: Scrolling list for downloadable files

Posted: Mon Sep 01, 2014 10:10 pm
by Appy777
Thank you Klaus! I can't wait to get the emulator working again to test! I appreciate your help. :D
Prue.

Re: Scrolling list for downloadable files

Posted: Sat Sep 06, 2014 1:30 am
by Appy777
Ok, so I now have my test device working, and I can test. I have input this script on the card, and the button script on the button. I have put a folder of files with the corresepoding folder and file names in the same folder in which the app file itself is saved. I am getting the following error on the following line, which is line 9:

put GetDocumentsLTRFiles() into theFiles

The error is:
card "Page 1 Intro": compilation error at line 9 (Commands: missing ',') near "into", Char 27

Does anyone know what is wrong?

When testing on the device, the menu is there, but the files are not loading when I click on the file name. Do I need to add more to the script to open the file?

Re: Scrolling list for downloadable files

Posted: Sat Sep 06, 2014 2:47 pm
by Klaus
Did you check if the path to the folder is definitively correct on MOBILE?
Add an "answer xxx" to verify!

Re: Scrolling list for downloadable files

Posted: Mon Sep 08, 2014 3:25 am
by Appy777
Hi Klaus,

I'm so sorry, this is all so very new to me. I am reading as much as possible to try to wrap my head around this, but I'm really finding it difficult, having no programming background at all.

I've looked around online to find tutorials about checking the path to the folder using the answer commend, to better understand what you mentioned here. The more I look, the more confused I become!

I think it is important to mention that I have included everything in my script literally... ie everything in the script I provided is exactly as I have included in my Livecode card and button scripts in the app I am building.

After digging around online I found this:
on mouseUp
put the effective filename of this stack into tPath
set the itemDelimiter to slash
delete last item of tPath

answer tPath
end mouseUp

Which I put into the stack script. When I click anywhere on the card, the respose I get is the file location on my PC's D drive.

So I'm pretty sure this is not going to work on mobile. So now I am trying to figure out what I need to include in my script to put the files in the correct place for Android? On hunting around, I also found this on another thread... is this of relevance to my question?:

Quote:
In order to make the specialFolderPaths() work right on mobile, the engine creates "virtual" folders that actually point to other places. On Android that's at:

/data/data/com.yourcompany.yourapp/<foldername>/

So, if you've saved a file in specialfolderpath("documents"), the real folder path will be:

/data/data/com.yourcompany.yourapp/files/

On an unrooted device, you can't look at /data/data/, it's locked, but the files are there.
end quote

I recognise this from the "identifier" field in the stand alone settings for android.
I had previously left this field unchanged. Does my error lie here?
What script do I need to include to put my files in the zip file with the app for use on Android (and am I on the right thought path here???)

Thanks for your kind help. Hopefully I can get a grasp on this, I'm really struggling at the moment!

Re: Scrolling list for downloadable files

Posted: Mon Sep 08, 2014 3:46 am
by keram
Hi Appy777,

Try this on the stack script:

Code: Select all

global gSFPath
on preopenStack

      switch platform()
         case "android"
            ## Android
            put specialFolderPath("documents") into gSFPath
            break
         case "Win32"
            ## Everything >= Win2000
            if the systemversion contains "NT" then put specialFolderPath(26) into gSFPath
            break
      end switch
            put gSFPath &"/myFiles" into gSFPath
      if there is not a folder gSFPath then new folder gSFPath  --new folder
	  
end preopenStack
This works for me on both Windows (files will be in C:\Users\<user name>\AppData\Roaming\myFiles folder) and Android.

keram

Re: Scrolling list for downloadable files

Posted: Mon Sep 08, 2014 5:31 am
by Appy777
Thanks Keram,

When testing this on my device using "Test Target", what should I expect the difference to be with / without this script?

Nothing seems to be happening still when I touch the file names in the scrolling field. I have made modifications to the folder name - ie. I changed the folder name to documents rather than LTRDocuments in the previous script as I thought that this perhaps was causing a problem. The modified card script is below:

on preOpenCard
_## Load the file lister
_uiPopulateFileLister
_
end preOpenCard


command uiPopulateFileLister
_## Get list of Document files in documents folder
_put GetDocumentsFiles("documents") into theFiles
_
_## Assign the list of files to the list field (our menu)
_set the text of field "File Lister" to theFiles
_
end uiPopulateFileLister

function GetDocumentTRFFiles
_## Get the path to the "documents" folder on disk
_put GetPathToFolder("documents") into theFolder
_
_## Get list of files in the folder
_put GetFilesInFolder(theFolder) into theFiles
_
_## Return list of files
_return theFiles
end GetdocumentsFiles

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 uiLoadSelectedDocument
_## Get the name of the document selected in the document menu
_put the selectedtext of field "File Lister" into theDocumentName
_put "documents /" & theDocumentName into theDocumentFile
_
_## Set 'the filename' property the player object to the relative document path
_## Revolution will locate the video as long as the "Documents" folder is
_## alongside the stack file or the application executable.
## set the filename of File "My Document" to theDocumentFile
launch document theDocumentFile
end uiLoadSelectedDocument

I have in the object script:
on selectionChanged
uiLoadSelectedDocument
end selectionChanged

Re: Scrolling list for downloadable files

Posted: Mon Sep 08, 2014 5:36 am
by Simon
Hi Appy,
You should look up "specialFolderPath" in the dictionary.
The lesson you linked to isn't made for mobile so all the path information must be created specifically for mobile.
I also found this on another thread... is this of relevance to my question?:
Don't worry about that, just use the path above as keram used it.

Simon

Re: Scrolling list for downloadable files

Posted: Tue Sep 09, 2014 5:39 am
by Appy777
Ok. I have used the script Keram provided in the stack script. I have looked for the path Keram mentioned: "files will be in C:\Users\<user name>\AppData\Roaming\myFiles folder)"

I have \Application Data rather than AppData, but there is no \Roaming\myFiles folder

I have input the stack script, saved the stack, checked the Standalone Application settings for android, saved as a Standalone Application, and launched in Test Target. Have I missed anything? Am I meant to place the files somewhere (I am assuming in the myFiles folder in Roaming, but I can't see such folder in that application!)

Thanks for your kind help, and patience.

Prue.