Re: Need an AppleScript way to do this shell script
Re: Need an AppleScript way to do this shell script
- Subject: Re: Need an AppleScript way to do this shell script
- From: Nigel Smith <email@hidden>
- Date: Thu, 15 Jul 2004 10:39:46 +0100
On 15/7/04 2:00, "Joe Klemmer" <email@hidden> wrote:
>
I have a shell script that will take the full posix path of a file or
>
directory from stdin and print out only the very last part. i.e. If
>
given this -
>
>
$ namesrc.sh /usr/local/test/whatever/foo
>
>
it will print out -
>
>
foo
Others have pointed out the shell command "basename", which might at least
save you some shell scripting. Note that "basename" will also give you the
last name even if the string ends with a "/":
basename /Users/nigel/
--> nigel
Here's a variant function that includes the above behaviour:
on GetLast(theString)
repeat while theString contains "/"
try
set theString to text ((offset of "/" in theString) + 1) <linebreak>
thru -1 of theString
on error
return text 1 thru -2 of theString
end error
end repeat
return theString
end GetLast
GetLast("/Users/nigel/Documents/test")
--> "test"
GetLast("/Users/nigel/Documents/test/")
--> "test"
Wonder if we'll ever get a "reverse offset"? It'll save doing:
on GetLast(theString)
set oldTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to ""
set theString to (reverse of (every text item of theString)) as text
set theString to text 1 thru ((offset of "/" in theString) - 1) <break>
of theString
set theString to (reverse of (every text item of theString)) as text
set AppleScript's text item delimiters to oldTIDs
return theString
end GetLast
GetLast("/Users/nigel/Documents/test")
--> "test"
:-)
Later,
Nigel
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.