These are the routines I use for getting containers, item names, and extensions. I like them because they work on files and folders, HFS paths and POSIX paths, strings, aliases, or file specifications, and don’t rely on any application while still being vanilla AppleScript:
set item_path to (choose file)
set {container_path, item_name} to my get_container_and_name(item_path)
set {item_name, item_extension} to my get_name_and_extension(item_name)
get {container_path, item_name, item_extension}
-->{"Macintosh HD:Users:jon:Desktop:", "test", "txt"}
set {container_path, item_name} to my get_container_and_name(POSIX path of item_path)
set {item_name, item_extension} to my get_name_and_extension(item_name)
get {container_path, item_name, item_extension}
-->{"/Users/jon/Desktop/", "test", "txt"}
on get_container_and_name(p)
set p to p as Unicode text
set d to {"/", ":"}'s item (((p contains ":") as integer) + 1)
set {l, t} to {my string_to_list(p, d), ({{-1, -2}, {-2, -3}}'s item (((p ends with d) as integer) + 1))}
return {my list_to_string(l's items 1 thru (get t's item 2), d) & d, l's item (get t's item 1)}
end get_container_and_name
on get_name_and_extension(n)
set d to "."
set l to my string_to_list(n, d)
if (count l) > 1 then
return {my list_to_string(l's items 1 thru -2, d), l's item -1}
else
return {n, ""}
end if
end get_name_and_extension
on list_to_string(l, d)
tell (a reference to my text item delimiters)
set {o, contents} to {contents, d as Unicode text}
set {l, contents} to {"" & l, o}
end tell
return l as Unicode text
end list_to_string
on string_to_list(s, d)
tell (a reference to my text item delimiters)
set {o, contents} to {contents, d as Unicode text}
set {s, contents} to {s's text items, o}
end tell
return s
end string_to_list
Jon