text manipulation - get the filename and folder name

Any help would be greatly appreciated.

Objective:
setting valueChoosen to 1 returns “Filename of file.png” – works
setting valueChoosen to 2 returns “Folder Name” – doesn’t gets “”

? - How do I get the “Folder Name” when setting valueChoosen to 2?


set valueChoosen to 2

-- Debug
if valueChoosen is 1 then
	set a to "Macintosh HD:Developer:Filename of file.png" -- File	
else if valueChoosen is 2 then
	set a to "Macintosh HD:Developer:Folder Name:" -- Folder
else
	display dialog "error"
end if

-- 
set text item delimiters to ":"
set temp to last text item of a

Setting valueChosen to 1 doesn’t return the name, but the path.

Hi,

ask System Events


set valueChoosen to 2

-- Debug
if valueChoosen is 1 then
	tell application "System Events" to set a to name of file "Macintosh HD:Developer:Filename of file.png" -- File    
else if valueChoosen is 2 then
	tell application "System Events" to set a to name of folder "Macintosh HD:Developer:Folder Name:" -- Folder
else
	display dialog "error"
end if


PS: or with your method


set temp to text item -valueChosen of a

yes that’s the problem I would like it to return the “Folder Name” text.

? - is:

from: Macintosh HD:Developer:Folder Name:
how do I return “Folder Name”

StefanK. Thanks for the reply but that doesn’t achieve the objective.

Let me see if I can help clarify.

I have a function that captures the file or folder being added to a folder.

filename is “Hello World.txt”
foldername is “Projects”

Right now the function provides the absolute path to the folder or file.
Macintosh HD:Developer:Hello World.txt
Macintosh HD:Developer:Projects:

The issue I’m having is the script right now can grab the name of the file using the : but it can’t grab the name of the folder added of “Projects”.

How do I get the name of the folder?

hope that helps to clarify what I’m trying to do.

Hello.

I think your problem as Stefan almost said in the passing that, that your folder name ends with a text item delimiter, so you’ll have to skew the text item inwards, or it returns the empty string where the text item delimiter are, and the empty text string that are outside of the delimiter!

There is a great article about this, should you wish to delve into it in the Unscripted section.


set valueChoosen to 2

-- Debug
if valueChoosen is 1 then
	set a to "Macintosh HD:Developer:Filename of file.png" -- File	
else if valueChoosen is 2 then
	set a to "Macintosh HD:Developer:Folder Name:" -- Folder
else
	display dialog "error"
end if

-- 
set {tids, text item delimiters} to {text item delimiters, ":"}
if last character of a = ":" then set valuechosen to valueChoosen + 1
set temp to text item -valueChoosen of a
--> "Folder Name"
set text item delimiters to tids


Edit
Fixed the setting of the text item delimiters. :slight_smile:

Why? My snippet sets the variable a to the name of the file or folder (aka the last path component)

Thanks everyone for the reply greatly appreciate the time and effort.

This achieved what I was looking for, hope it also helps someone too.

tell application "Finder"
	
	set FolderPath to (choose folder) -- sets file path to folder you select
	set Foldername to name of folder FolderPath -- sets the folder name as text
	
	display dialog Foldername as text
	
end tell