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
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
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.
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