Basename, extension, parentFolder exercise in ASObjC

There are so many ways to get filename, extension or parentFolder from ex. Finder selections.

I have used NSString and NSURL but also NSFileManager from AppKit.
I prefer POSIX path instead of Alias so all return POSIX path.

To run the script examples… select a file in Finder…

use framework "Foundation"
use scripting additions

tell application "Finder"
	set finderSelection to POSIX path of ((get selection) as text)
end tell

(**
* [getBasenameExtension(string pathString)]
*)
-- its getBasenameExtension(finderSelection)
on getBasenameExtension(pathString)
	set theString to current application's NSString's stringWithString:pathString
	set {basename, pathExt} to (theString's lastPathComponent()'s componentsSeparatedByString:".")
	return {basename:basename as string, pathExtension:pathExt as string}
end getBasenameExtension

(**
* [getBasename(string pathString)]
*)
-- its getBasename(finderSelection)
on getBasename(pathString)
	set basename to current application's |NSURL|'s fileURLWithPath:pathString
	return (basename's URLByDeletingPathExtension()'s lastPathComponent()) as string
end getBasename

(**
* [setBasename:string basename pathExt:string pathExt fromPath:string pathString]
*)
-- its setBasename:"basename" pathExt:"pdf" fromPath:finderSelection
on setBasename:basename pathExt:pathExt fromPath:pathString
	set theURL to current application's |NSURL|'s fileURLWithPath:pathString
	set thePath to theURL's URLByDeletingLastPathComponent()'s valueForKey:"path"
	set basePath to current application's |NSURL|'s fileURLWithPath:thePath
	set appendingPath to (basePath's URLByAppendingPathComponent:(basename & "." & pathExt))'s valueForKey:"path"
	return appendingPath as string
end setBasename:pathExt:fromPath:

(**
* [getParentFolder(string finderSelection)]
*)
-- its getParentFolder(finderSelection)
on getParentFolder(pathString)
	set theString to current application's NSString's stringWithString:pathString
	set parentFolder to (theString's stringByDeletingLastPathComponent()) as string
	return parentFolder & "/"
end getParentFolder

(**
* [getBasenameAndExtension]
*)
-- its getBasenameAndExtension(finderSelection)
on getBasenameAndExtension(pathString)
	set theString to current application's NSString's stringWithString:pathString
	return theString's lastPathComponent() as string
end getBasenameAndExtension

(**
* [getBasenameAndExtension2(string pathString)]
*)
-- its getBasenameAndExtension2(finderSelection)
on getBasenameAndExtension2(pathString)
	set theString to current application's NSString's stringWithString:pathString
	set filemanager to current application's NSFileManager's defaultManager()
	return (filemanager's displayNameAtPath:theString) as string
end getBasenameAndExtension2

(**
* [getBasenameExtParentFolder(string pathString)]
*)
-- its getBasenameExtParentFolder(finderSelection)
on getBasenameExtParentFolder(pathString)
	set theURL to current application's |NSURL|'s fileURLWithPath:pathString
	set theKey to {(current application's NSURLNameKey), (current application's NSURLPathKey), (current application's NSURLParentDirectoryURLKey)}
	set theDict to (theURL's resourceValuesForKeys:theKey |error|:(missing value))
	set {basenameExt, thePath, parentFolder} to {(theDict's valueForKey:"NSURLNameKey") as string, (theDict's valueForKey:"_NSURLPathKey") as string, ((theDict's valueForKey:"NSURLParentDirectoryURLKey")'s valueForKey:"path") as string}
end getBasenameExtParentFolder


Shane made this very easy…

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use script "FileManagerLib" version "2.3.5"
tell application "Finder" to set finderSelection to POSIX path of ((get selection) as text)
set fileInfo to parse object finderSelection
set {FullName, nameExtension, nameStub, parentFolderPath, parentFolderName, displayedName, fileKind} to {full_name, name_extension, name_stub, parent_folder_path, parent_folder_name, displayed_name, file_kind} of fileInfo