I have a string (not a yarn, mind you):
theString: “Macintosh HD:blah:more_blah:test:”
When I set the Text Delimiters to recognize “:” and request the last text item of theString I get “”. So, then, how do I get the second to last text item of the string so that it will return “test”?
Because your string ends with a colon, the last item is blank, so you want the second from the end. If the last item was not a colon, you’d want the last (-1) text item.
set theString to "Macintosh HD:blah:more_blah:test:"
set {ASTID, text item delimiters} to {text item delimiters, ":"}
set theString to the text item -2 of theString
set text item delimiters to ASTID
theString --> "test"
EDIT:
If you don’t know, then:
set theString to "Macintosh HD:blah:more_blah:test"
set {ASTID, text item delimiters} to {text item delimiters, ":"}
tell theString
set theEnd to text item -1
if theEnd is "" then
set theString to the text item -2 of theString
else
set theString to theEnd
end if
end tell
set text item delimiters to ASTID
theString
on adding folder items to this_folder after receiving dropped_items
set the_dropped_folder_path to item 1 of dropped_items as string
tell application "Finder" to display dialog ("the folder path is: " & the_dropped_folder_path)
set oldDelims to text item delimiters of AppleScript --saves the old delims
set text item delimiters of AppleScript to {":"} --sets new delimiters
try
set folderName to (text item -2) of the text item of the_dropped_folder_path
on error
set folderName to "oops"
end try
tell application "Finder"
activate
display dialog ("Hello, the folder is: " & folderName)
end tell
set text item delimiters to oldDelims
end
Ok, I have this as a folder action. I drop the folder on the hot-folder and the first dialog comes up perfect. the second always returns “oops.”
set t to "Macintosh HD:blah:more_blah:test"
set d to text item delimiters
set text item delimiters to ":"
set n to t's text item -(1 div ((count t's text item -1) + 1) + 1)
set text item delimiters to d
n
… which is a tad faster - but not quite as fast as:
set t to "Macintosh HD:blah:more_blah:test:"
set d to text item delimiters
set text item delimiters to ":"
if t ends with ":" then
set n to t's text item -2
else
set n to t's text item -1
end if
set text item delimiters to d
n
However, I’m not quite sure yet what the aim is here. If it’s to get the name of each dropped item, and we’re using Finder for other stuff, I’d be tempted to push the job in that direction:
on adding folder items to after receiving dropped_items
tell application "Finder" to repeat with d in dropped_items
set n to d's name
-- do something with name - for example:
display dialog n
end repeat
end adding folder items to