Return to Snippet

Revision: 57041
at May 8, 2012 16:09 by ianthekirkland


Initial Code
(* This always gets a path to a folder *)
tell application "Finder"
	try
		-- Try to get the current item(s) selected in Finder first
		set mySel to selection
		if (count of mySel) < 1 then
			-- If there's no selection try to get the path of the window instead
			-- Otherwise just return the desktop since the user could have used the context menu from there
			if (count of windows) > 0 then
				set myFolder to target of first window
			else
				set myFolder to desktop
			end if
		else
			set myFolder to first item of mySel
		end if
		
		if (count of mySel) > 1 then
			display dialog "Multiple items selected, only getting first item of " & quoted form of (displayed name of myFolder as text) with icon
		end if
		
		-- If the selection is a file or an alias to a file we get the containing folder instead
		if class of myFolder is document file or (class of myFolder is alias file and file type of myFolder is missing value) then
			set myFolder to container of myFolder
		end if
		
		-- Do whatever you want with the folder here...
	on error errMsg
		beep
		display dialog errMsg
		return
	end try
end tell	

Initial URL


Initial Description


Initial Title
F-GetFolderPathToFinderItemOrWindow

Initial Tags


Initial Language
AppleScript