Getting the InitialPath for a Script to do work in/with.

This code is posted here in hope that it will be useful at least for some newbie scripter or scripters that need
to have a user set up the correct path to a script the first time the script is run, and use that path afterwards.

I have made this into a very small library of three routines, one with and one without qualification.
- And a primitive which takes care of the choose folder dialogue in itself and a quit handler that is
meant to take care of any resetting of properties during a quit operation, like reinstating original
AppleScript's text item delimiters and so on. 

The one with qualification. is made under the assumption that the scripter pretty much knows what kind
of directories are hiding below the topmost level of the subtree. Therefore can qualify by sub directory.
It also gives the user some flexibility (maybe too much) in that he or she, can name the root of the subtree
at will.

The other without any qualification is pretty vanilla. 
The "talkative parameter should be set to true or false, when set to true it will ask if the user really want to
quit when he/she hits escape in the choose folder dialogue. 

The paths are returned as HFS paths in either case.

Thanks to [b][color=blue]Yvan Koenig[/color][/b]: [b]The buttons of the dialogs are now localized[/b].

Should you need a more rigid solution, then that is an excercise left for you to do. 
-Please share your results. :)

-- The Idea and implementation and any faults is totally mine. © McUsr 2010 and put in the Public Domain.
-- The usually guarrantees about nothing what so ever applies, use it at your own risk.
-- Read the documentation.
-- You are not allowed to post this code elsewhere, but may of course refer to the post at
This applies only to posting code, as long as you don't post it, you are welcome to do
whatever you want to do with it without any further permission. 
Except for the following: Selling the code as is, or removing copyright statmentents and the embedded link in the code (without the http:// part) from the code. 

You must also state what you eventually have done with the original source. This obviously doesn't matter if you  distribure AppleScript as read only. I do not require you to embed any properties helding copyright notice for the code.

Credit for having contributed to your product would in all cases be nice!

If you use this code as part of script of yours you are of course welcome to post that code with my code in it here at If you then wish to post your code elsewhere after having uploaded it to please email me and ask for permission.

The ideal situation is however that you then refer to your code by a link to
The sole reason for this, is that it is so much better for all of us to have a centralized codebase which are updated, than having to roam the net to find any usable snippets. Which some of us probabaly originated in the first hand.

I'm picky about this. If I find you to have published parts of my code on any other site without previous permission, I'll do what I can to have the site remove the code, ban you, and sue you under the jurisdiction of AGDER LAGMANNSRETT of Norway. Those are the terms you silently agree too by using this code. 

The above paragraphs are also valid if you violate any of my terms.

If you use this or have advantage of this code in a professional setting, where professional setting means that you use this code to earn money by keeping yourself more productive. Or if you as an employee share the resulting script with other coworkers, enhancing the productivity of your company, then a modest donation to would be appreciated.
my localButtons's initializeLocalizationValues()
my localButtons's initializeLocalizationValues()
-- must initiate the local buttons first!
select_initial_path_qualified_by_a_child_folder("Desktop", true, "Testing")

on select_initial_path_qualified_by_a_child_folder(existing_child_folderAsText, blnAskToConfirm, txtTitle)
	-- This handler is called the first time the script is beeing run after editing in order to set the initial path
	-- to the root of the subtree which all files in the system are within.
	-- Returns a chosen path if the path contains the folder EXISTING_FOLDER.
	-- Parameters: existing_child_folder : the folder used to qualify the path.
	-- 			     talkative           		: parameter to avoid the "really want to exit? question.
	--				main_title			:Title of your app/script.
	local new_path, ok_path, theButton, selection_title
	set selection_title to txtTitle & ": Select the parent folder of folder \"" & existing_child_folderAsText & " \""
	set ok_path to false
		set new_path to getFolder(selection_title, "/", blnAskToConfirm)
		tell application "Finder"
			if (count of (every folder of folder new_path whose name is existing_child_folderAsText)) is 1 then
				set ok_path to true
			end if
		end tell
		if ok_path is true then
			return new_path
			beep 2
			tell me
					set theButton to the button returned of (display dialog "The chosen folder does not contain the folder  " & existing_child_folder & " which is how we determine the correct path to our filesystem . Please try again!" with title selection_title buttons {my localButtons's Cancel_loc, my localButtons's Continue_loc} default button 2 cancel button {my localButtons's Cancel_loc} with icon 2)
				on error number -128
					tell me to quit
				end try
			end tell
		end if
	end repeat
end select_initial_path_qualified_by_a_child_folder

on select_initial_path(blnAskToConfirm, txtTitle, txtTheObjective)
	-- This handler is called the first time the script is beeing run after editing in order to set the initial path
	-- Returns a chosen path
	-- parameters.
	-- 			     talkative           		: parameter to avoid the "really want to exit? question.
	--				main_title			:Title of your app/script.
	local new_path, selection_title
	set selection_title to txtTitle & ": Select the intitial folder for " & txtTheObjective
	set new_path to getFolder(selection_title, "/", blnAskToConfirm)
	return new_path
end select_initial_path

-- its a generic hander for choosing a folder which can ask if your really want to quit, or just quits the script otherwise.
-- calls an on_quit handler for quitting when run as an applet. this can be configured by a variable is_Applet or just
-- uncomment som lines.
on getFolder(txtTitle, pxStartPathAsText, blnAskToConfirm)
	local new_path, iConfirmTimes, theButton
	set iConfirmTimes to 0
	tell me
		repeat while true
			tell application "System Events"
				tell application process "Finder"
						set new_path to (choose folder with prompt txtTitle default location (POSIX file pxStartPathAsText) without invisibles) as text
						return new_path
					on error e number n
						-- display dialog "err: " & e & " : " & n
						if n is -128 then
							set iConfirmTimes to iConfirmTimes + 1
								if blnAskToConfirm is true and iConfirmTimes < 2 then
									set theButton to the button returned of (display dialog "Do you want to try again?" with title txtTitle with icon 2)
									-- falls through the end of the repeat loop and repeat ourselves.
								else -- not talking back
									exit repeat
								end if
							on error -- user hit "cancel" or equiv in the dialog.
								exit repeat
							end try
						end if
					end try
				end tell
			end tell
		end repeat
	end tell
	error number -128
	-- tell me to quit
end getFolder

-- script for localizing the dialogs were kindly given to my by KOENIG,Yvan.
-- The call chain and any problems that may cause is solely a responsibility of mine.
script localButtons -- © KOENIG,Yvan
	property Cancel_loc : missing value
	--	property Save_loc : missing value
	property Continue_loc : missing value
	--	property Ok_loc : missing value
	--	property Authenticate_loc : missing value
	--	property Skip_loc : missing value
	--	property Delete_loc : missing value
	--	property Eject_loc : missing value
	on initializeLocalizationValues()
		set localButtons's Cancel_loc to localButtons's getLocalizedString("Finder", "AL1") -- Annuler
		--		set localButtons's Save_loc to localButtons's getLocalizedString("Finder", "AL2") -- Enregistrer
		set localButtons's Continue_loc to localButtons's getLocalizedString("Finder", "AL3") -- Continuer
		--		set localButtons's Ok_loc to my getLocalizedString("Finder", "AL4") -- OK
		--		set localButtons's Authenticate_loc to localButtons's getLocalizedString("Finder", "AL5") -- Authentifier
		--		set localButtons's Skip_loc to my getLocalizedString("Finder", "AL6") -- Ignorer
		--		set localButtons's Delete_loc to localButtons's getLocalizedString("Finder", "AL7") -- Supprimer
		--		set localButtons's Eject_loc to localButtons's getLocalizedString("Finder", "AL8") -- Éjecter
	end initializeLocalizationValues
	on getLocalizedString(a, x)
		tell application a to return localized string x
	end getLocalizedString
end script

-- this routine is provided for centralized cleanup like setting back AppleScript's text item delimiters.
on quit
	error number -128 -- comment this wen saved as a stay open handler
	-- uncomment line below when saved as a stay open applet.
	-- continue quit
end quit

Best Regards



I have updated the handler select_initial_path in the post above in order to take an extra parameter theObjective which caters for far more useful replies than the previous version.

Best Regards



I have removed a bug from getFolder(), and localized the handlers buttons.
Thanks to Yvan Koenig.
Best Regards



Added the functionality to getFolder that it quits the second time the user cancels the file choose dialog if it was originally set up to ask for confirmation.

Made some variable names a little bit more meaningful as well.

Best Regards