ParentFolder() and itemName() takes *Anything* returns text

Hello.

These two handlers returns either the item name of a file or folder specifier of some kind, or the parent folder of a file specifier of some kind, as text.

You can feed the handlers with file references, aliases, hfs paths and posix paths as text.
I did call the handler that returns just the filename and folder name for item name, avoiding ambiguities by not calling it base name, since that is a different story which only consist of the filename, without any suffix.

If you feed it with posix, it returns posix text, else hfs text.

They are a convenience, even if you know what you are doing. At least you have to write it every time.
They should be even better for people who are a little insecure.

Here they are, they are tested.

I have added two more handlers that returns the absolute posix path and hfs path, they do consider quotes inside quotes, and posix paths relative to their home folder, they also considers disks and volumes when converting from hfs to posix or vice versa.

The idea is to have something really robust, which you just use, and won’t have to give a thought.

Enjoy :smiley:



-- 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 macscripter.net.
-- macscripter.net/viewtopic.php?pid=macscripter.net/viewtopic.php?pid=131797#p131797
(*
TERMS OF USE. 
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 macscripter.net. If you then wish to post your code elsewhere after having uploaded it to MacScripter.net please email me and ask for permission.

The ideal situation is however that you then refer to your code by a link to MacScripter.net
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 MacScripter.net would be appreciated.
*)
-- © McUsr 2010 and put in Public Domain see: macscripter.net/viewtopic.php?pid=131797#p131797 for reference and terms of use.

script PathWay
	
	on getPathToParentFolderOfAnythingAsText(anyFileOrFolderPath)
		-- whether it is a file, a folder,application or bundle.
		local tids, theFile, lastItem, tidsToUse, singleQuoteCheck
		set singleQuoteCheck to false
		try
			(get class of anyFileOrFolderPath)
		on error number -1728 -- it was a filereference
			set anyFileOrFolderPath to anyFileOrFolderPath as alias
		end try
		set anyFileOrFolderPath to "" & anyFileOrFolderPath -- doesn't harm.
		if anyFileOrFolderPath starts with "'" and anyFileOrFolderPath ends with "'" then
			set anyFileOrFolderPath to text 2 thru -2 of anyFileOrFolderPath
			set singleQuoteCheck to true
		end if
		set tidsToUse to ":"
		if anyFileOrFolderPath starts with "/" or anyFileOrFolderPath starts with "~" then set tidsToUse to "/"
		set lastItem to -2
		if anyFileOrFolderPath ends with tidsToUse then set lastItem to -3
		-- we gain an extra text item  the empty one created at the end.
		set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, tidsToUse}
		set anyFileOrFolderPath to text items 1 thru lastItem of anyFileOrFolderPath
		set anyFileOrFolderPath to "" & anyFileOrFolderPath
		if singleQuoteCheck is true then
			set AppleScript's text item delimiters to "'\\''"
			set anyFileOrFolderPath to text items of anyFileOrFolderPath
			set AppleScript's text item delimiters to "'"
		end if
		set anyFileOrFolderPath to "" & anyFileOrFolderPath -- necessary here.
		set AppleScript's text item delimiters to tids
		return anyFileOrFolderPath
	end getPathToParentFolderOfAnythingAsText
	
	
	on itemNameOfAnythingAsText(fileOrFolderPath)
		-- whether it is a file, a folder,application or bundle.
		local tids, theFile, lastItem, tidsToUse, singleQuoteCheck -- Thanks to Yvan Koenig :)
		set singleQuoteCheck to false
		set tidsToUse to ":"
		try
			(get class of fileOrFolderPath)
		on error number -1728 -- it was a filereference
			set fileOrFolderPath to fileOrFolderPath as alias
		end try
		
		set fileOrFolderPath to "" & fileOrFolderPath -- doesn't harm.
		if fileOrFolderPath starts with "'" and fileOrFolderPath ends with "'" then
			set fileOrFolderPath to text 2 thru -2 of fileOrFolderPath
			set singleQuoteCheck to true
		end if
		if fileOrFolderPath starts with "/" then set tidsToUse to "/"
		set lastItem to -1
		if fileOrFolderPath ends with tidsToUse then set lastItem to -2
		set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, tidsToUse}
		set fileName to text item lastItem of (fileOrFolderPath as text)
		if singleQuoteCheck is true then
			set AppleScript's text item delimiters to "'\\''"
			set fileName to text items of fileName -- as list
			set AppleScript's text item delimiters to "'"
		end if
		set fileName to "" & fileName
		set AppleScript's text item delimiters to tids
		return fileName
	end itemNameOfAnythingAsText
	
	on posixPathOfAnytingAsText(anyFileOrFolderPath)
		-- returns the full posix pathname of anything if the files exists
		local tids, theFile, lastItem, tidsToUse, singleQuoteCheck -- Thanks to Yvan Koenig :)
		set singleQuoteCheck to false
		set tidsToUse to ":"
		try
			(get class of anyFileOrFolderPath)
		on error number -1728 -- it was a filereference
			set anyFileOrFolderPath to POSIX path of (anyFileOrFolderPath as alias) as text
			return anyFileOrFolderPath
		end try
		
		set anyFileOrFolderPath to "" & anyFileOrFolderPath -- doesn't harm.
		if anyFileOrFolderPath starts with "'" and anyFileOrFolderPath ends with "'" then
			set anyFileOrFolderPath to text 2 thru -2 of anyFileOrFolderPath
			set singleQuoteCheck to true
		end if
		if anyFileOrFolderPath starts with "/" and singleQuteCheck is false then
			return anyFileOrFolderPath
		else if anyFileOrFolderPath starts with "/" or anyFileOrFolderPath starts with "~" then
			set tidsToUse to "/"
		end if
		
		set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, tidsToUse}
		set anyFileOrFolderPath to text items of anyFileOrFolderPath as text
		if singleQuoteCheck is true then
			set AppleScript's text item delimiters to "'\\''"
			set anyFileOrFolderPath to text items of anyFileOrFolderPath -- as list
			set AppleScript's text item delimiters to "'"
		end if
		set anyFileOrFolderPath to "" & anyFileOrFolderPath
		set AppleScript's text item delimiters to tidsToUse
		
		if tidsToUse is ":" then
			-- the first item isn' like disk nr 1 then we have to do something.
			if text item 1 of anyFileOrFolderPath is not item 1 of (list disks) then
				set anyFileOrFolderPath to {"Volumes"} & text items of anyFileOrFolderPath
			else
				set anyFileOrFolderPath to {""} & text items 2 thru -1 of anyFileOrFolderPath
			end if
			set AppleScript's text item delimiters to "/"
		else if text item 1 of anyFileOrFolderPath is "~" then
			-- mus get the posix path as text
			set anyFileOrFolderPath to text items 2 thru -2 of (POSIX path of (path to home folder) as text) & text items 2 thru -1 of anyFileOrFolderPath
		end if
		set anyFileOrFolderPath to "" & anyFileOrFolderPath
		set AppleScript's text item delimiters to tids
		return anyFileOrFolderPath
	end posixPathOfAnytingAsText
	
	
	on hfsPathOfAnythingAsText(anyFileOrFolderPath)
		-- returns the full hfs pathname of anything if the files exists
		local tids, theFile, lastItem, tidsToUse, singleQuoteCheck -- Thanks to Yvan Koenig :)
		set singleQuoteCheck to false
		set tidsToUse to ":"
		try
			(get class of anyFileOrFolderPath)
		on error number -1728 -- it was a filereference
			set fileOrFolderPath to fileOrFolderPath as alias as text
			return fileOrFolderPath
		end try
		
		set anyFileOrFolderPath to "" & anyFileOrFolderPath -- doesn't harm.
		if anyFileOrFolderPath starts with "'" and anyFileOrFolderPath ends with "'" then
			set anyFileOrFolderPath to text 2 thru -2 of anyFileOrFolderPath
			set singleQuoteCheck to true
		end if
		if anyFileOrFolderPath does not start with "/" and anyFileOrFolderPath does not start with "~" then
			return anyFileOrFolderPath -- we had a hfspath
		else
			set tidsToUse to "/"
		end if
		
		set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, tidsToUse}
		set anyFileOrFolderPath to text items of anyFileOrFolderPath as text
		if singleQuoteCheck is true then
			set AppleScript's text item delimiters to "'\\''"
			set anyFileOrFolderPath to text items of anyFileOrFolderPath -- as list
			set AppleScript's text item delimiters to "'"
		end if
		-- if tidstouse was "/" then we must add the disk name, - but which ???
		set anyFileOrFolderPath to "" & anyFileOrFolderPath
		set AppleScript's text item delimiters to tidsToUse
		if text item 1 of anyFileOrFolderPath is "~" then
			set anyFileOrFolderPath to {item 1 of (list disks)} & text items 2 thru -2 of (POSIX path of (path to home folder) as text) & text items 2 thru -1 of anyFileOrFolderPath
		else if text item 2 of anyFileOrFolderPath is "Volumes" then
			set anyFileOrFolderPath to text items 3 thru -1 of anyFileOrFolderPath
		else
			set anyFileOrFolderPath to {item 1 of (list disks)} & text items 2 thru -1 of anyFileOrFolderPath
		end if
		set AppleScript's text item delimiters to ":"
		set anyFileOrFolderPath to "" & anyFileOrFolderPath
		set AppleScript's text item delimiters to tids
		return anyFileOrFolderPath
	end hfsPathOfAnythingAsText
	
end script

Hello.

They don’t take quoted form of a posix path, just to be precise.

Hello.

I upgraded them to tackle quoted form of a posix path as well.

Hello.

I have updated those , with thanks to Yvan Koenig, who kindly pointed out the previous version wouldn’t tackle single quotes within the filenames particularly well. Well, the now do. -I would never have figured out the right . spell for the delimiter sequence to make the offending one go away, without having had his code to look at. Thanks! :slight_smile:

Hello.

Removed one final bug in the getPathToParentFolderOfAnythingAsText() as text, the bug was a missing coercion, that led to a loss the single quotes. Thanks to Yvan Koenig.

Hello.

I have removed two more “final” bugs (hopefully). The identical error in both handers was that I only checked if the filename ended with a semi colon “:” , and not slash “/” if it originally was a posix path.

Hello.

I have added two handlers that returns the full pathname of something that exists in either posix or hfs text.
You should be able to feed them anything. They are meant to be robust, and returns the correct names for disks and such given the from format, they also consider the usage of “~” for home folders posix paths. I have convoluted them into a script object named PathWay.

Hello.

I have removed a bug in [b]hfsPathOfAnything/b I didn’t add on the volume when given a path that started with “~”