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 Stack" 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.livecode" 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.livecode

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 LiveCode'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 
   local tPath
   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 a 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:

local tPath, tImagesFolder
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 LiveCode.

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 (File -> Save as Standalone Application...). 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
   local tPath
   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

7 Comments

Dan

I LOVE THIS IDE...
How do I DELETE A Stack

Elanor Buchanan

Hi Dan

If you want to remove a stack from memory in the IDE you have a few options.

1. Select "Close and Remove From Memory" from the File menu
2. Open the Project Browser from the Tools menu. Right/ctrl/cmd click the stack in the list to open the context menu and choose "Close and Remove From Memory"
3. In script use the delete stack command

I hope that helps.

Elanor

bogs

In the above code, deleting the last item of tPath requires putting the slash back in...

set the itemDelimiter to slash
delete last item of tPath <-- 1
put tPath & slash & "images" into tImagesFolder <-- 2

If you deleted all the characters of the last item, though, the slash remains as part of tPath...

set the itemDelimiter to slash
delete character 1 to -1 of item -1 of tPath <-- 1
put tPath & "images" into tImagesFolder <-- 2

My question is, aside from style, is one more efficient than the other?

Elanor Buchanan

Hi bogs

Any difference here would be negligible so I think this really comes down to style and preference.

Kind regards

Elanor

Robert Cailliau

What is the easiest way to find out by script if a stack is standalone? I have used a trick: look at "the filename of this stack" and if it ends in ".livecode" then I'm not executing inside a standalone stack. But is there a better way? Perhaps by setting a custom property at the moment of building a standalone? (somewhat convoluted though)

Robert Cailliau

Sorry, my question about how to find out if I'm in standalone was rather stupid: the message "startup" is sent only in standalone stacks, so that's where I find out. "openstack" is always sent but after "startup".
But it may be a good idea to put that in some lesson somewhere.
Sorry for this.

Panos Merakos

Hello Robert,

I think the most robust way is to use "the environment" function:

if the environment contains "standalone" then
// standalone
else if the environment is "development" then
// stack is running in the IDE
else
// other cases - see dictionary
end if

Kind regards,
Panos
--

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.