How to control where the answer file/folder dialog starts

This is a very simple lessons that explains how to make the 'answer file' and 'answer folder' dialogs start at a specific location of your computer.

The basic answer file command

The basic answer file command

Start by creating a new stack and:

1) Drag on a new button

2) Bring up the script editor for the button

3) Put in the following script

on mouseUp
	answer file "Select a file"
end mouseUp

The result

The result

Switch to Run mode, click on the button and see where the dialog has taken you. In my case, the last time I loaded the the file dialog it was in the 'Downloads' folder. If you now navigate anywhere on your drive, close the dialog, and press the button again you will notice that it loads in the same folder as when you closed the dialog last.

Make the file chooser always start in a specific location

We need to use the 'with' syntax which is detailed in the Documentation. By specifying '/users/ben/Desktop' I am telling the dialog to open at that folder. I can still choose to go anywhere but each time I click my button I will be presented with my 'Desktop' folder as my start point.

on mouseUp
	answer file "Select a file" with "/users/ben/Desktop"
end mouseUp

You can of course set this in script allowing you to dynamically alter the place a user starts from.

on mouseUp
	local tStartDirectory
	
	put "/users/ben/Desktop" into tStartDirectory
	answer file "Select a file" with tStartDirectory
end mouseUp

Of course, this is hard coded to my own computer. If I want to write a dialog that accesses a users desktop folder without knowing their username, I would use the SpecialFolderPath syntax, which you can learn about in the LiveCode Dictionary:

on mouseUp
   local tStartDirectory
   set the defaultFolder to specialFolderPath("desktop")
   put the defaultFolder into tStartDirectory
   answer file "Select a file" with tStartDirectory
end mouseUp

2 Comments

Tim

Very nice, it worked perfectly for opening up a folder in a specific directory, thank you for posting this.

Warren

It should be mentioned that some directories appear to require the use of the trailing slash in order to work, at least in OS X 10.5. For example:

answer "select a file" with "/Applications/"

or: ........ with "/Volumes/mydisk/"

I haven't found any directories which fail using the trailing slash. It also seems that setting a folder within any of these "troublesome" locations works without using it.

It may be worth adopting the convention of always using the trailing slash in order to avoid some head scratching.

Add your comment

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