LiveCode LessonsHow To - Step-By-Step Guides To Tasks In LiveCode LiveCode tipsHow can I get the path to the folder a stack file resides in?

How can I get the path to the folder a stack file resides in?

Use "the filename" and "the effective filename" to find the location of a stack file on the hard drive.

Create a new stack

Create a new stack

Select "New Mainstack" from the File menu and drag out a button from the Tools palette.

Do NOT save this stack yet.

Use the Inspector to change the name of the button to "File Path".

Edit the script of the button as follows:

on mouseUp

answer the filename of this stack

end mouseUp

Change back to the browse tool and click the button. You will see an empty dialog box as shown above.

This is because this file has never been saved, so it has no filename yet.

Save the stack

Save the stack

Now choose "Save As..." from the File menu and save the stack anywhere you like - mine is called "StackPath.rev" and is in my Documents folder.

Once the stack has been saved, click the "File Path" button again.

This time, you should see the file path to your stack in the dialog. Your path will be different to mine, but it will be in the same sort of format.

If you are running under Windows, your path will be something like:

C:/Documents and Settings/Administrator/My Documents/StackPath.rev

So the filename of the stack shows the path to the saved stack file. It is blank until the stack is saved.

Windows users should note that Revolution's standard file path delimiter is slash, not back-slash.

Add a substack

Add a substack

Create a substack of this stack by selecting "New Substack of ...." from the File menu.

Move the new stack's window so you can see the main stack underneath.

Change to the pointer tool and select the "File Path" button. Copy it, click in the substack's window and paste the button there.

Now each stack window should show a "File Path" button.

Save the stack again and click on the "File Path" button in the substack.

It looks like we have lost the file path again! How come it works in the main stack and not in the substack?

This is because the filename is a property of the main stack only. Substacks do not have a filename property, which is why our dialog is empty again.

The effective filename

The effective filename

So what do we do if we need to get the filename of a stack file from inside a substack?

The key is to use "the effective filename" which travels up the hierarchy and finds the path to the stack file containing the current stack, whether it is a main stack or a substack.

Change the "File Path" button script in the substack to this:

on mouseUp

answer the effective filename of this stack

end mouseUp

Now when you click the button, you will get the file path to the stack as before.

Using "the effective filename" will work in a main stack as well as in substacks, so it is a good idea to use it all the time. That way you will always get a valid answer without having to worry about whether the stack is a main stack or a sub stack.

Getting the path to the folder

Getting the path to the folder

Now we know how to get the full path to the stack file, but it is often more useful to get the path to the enclosing folder. This can be used to locate media files, other stack files, externals and so on.

If you look at the file path returning by using "the effective filename" you can see that it is divided into sections using a slash (/) as the delimiter. If we remove the last section, then we are left with the path to the folder.

Edit the script of either of the "File Path" buttons as follows:

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

Switch back to the browse tool and click the edited button, to see the path to the enclosing folder.

This script puts the effective filename of the stack into a variable. Then the itemDelimiter is set to slash - slash is predefined constant that is easier to read than "/" but has the same effect. Setting the itemDelimiter to any character allows us to break up a chunk of text into items separated by the specified delimiter. Normally, the itemDelimiter is comma, but any single character can be used. Deleting the last item of the file path then gives us the folder path, no matter how deeply the file is nested in folders and sub-folders.

Using the folder path

You will notice that deleting the last item of the file path also deleted the trailing item delimiter.

This means that if you need to refer to any other folders or files relative to this folder path, you will need to add a slash on to the end first.

Imagine your stack file was in a folder that also contained a sub-folder called "images".

To access the "images" folder, you would need something like this:

put the effective filename of this stack into tPath

set the itemDelimiter to slash

delete last item of tPath

put tPath & slash & "images" into tImagesFolder

Getting the path of a standalone application

For Windows and Linux applications, you can use "the effective filename" and extract the path to the folder containing the application, just as if it was a normal stack working inside Revolution.

For Macs, this is a bit more difficult. This is because a Mac application is actually a folder, sometimes called an application bundle. When a folder is named with the extension ".app", it changes so that it cannot be opened by a double-click. However you can right-click or Control-click on an application bundle and select "Show Package Contents" from the popup menu. This opens the folder so you can see what is inside it.

Mac application bundles

Mac application bundles

I built this stack into a Mac standalone. When I click the "File Path" button, I get the path as shown above.

The actual executable file is buried in the "MacOS" folder deep inside the bundle.

If you have files hidden in the bundle, then this may be the path you need, but if you need to get the folder containing the application, then we can't just delete the last item.

In this case, we need to keep deleting items until we get to the item that includes ".app". That gives us the path to the actual application and then we can trim that as usual to get the path to the containing folder.

To make the script work for anything (stack or standalone, regardless of platform) change the "File Path" script to this:

on mouseUp

-- get the file path for this stack

put the effective filename of this stack into tPath

set the itemDelimiter to slash

if the platform = "MacOS" and the environment = "standalone application" then

-- if this is a standalone application running on a Mac, find the path to the .app file

repeat until last item of tPath contains ".app"

delete last item of tPath

end repeat

end if

-- remove the last item to get the path to the containing folder

delete last item of tPath

answer tPath

end mouseUp

0 Comments

Add your comment

E-Mail me when someone replies to this comment