Return (character position from string) or (file name from file path)

I am trying to do something very simple in AppleScript... search a string and have it return the character number. In VB there is a command to do this. I haven't found it in Applescript yet. I am left with walking through a string to find a delimater.

Here is what I am trying to do:
return a file name from a path:
myvolume:myfolder:myfile.txt

I want to return the file name.

Here is the handler I wrote to get the file name:

on getFileName(pathString)
set iCount to length of pathString

repeat while iCount > 1
if character iCount of pathString is ":" then
return (text (iCount + 1) thru (length of pathString)) of pathString as string
end if
set iCount to iCount - 1
end repeat
end getFileName

Is there an easier way to search a string?


Thank you.

g5 Dual 2ghz, Mac OS X (10.4.6), 2gb RAM

Posted on Jun 25, 2006 11:08 AM

Reply
12 replies

Jun 25, 2006 2:44 PM in response to StephenZcat

This is how it works with AS's text item delimiters:


on getfilename(pathString)
set {olddelims, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
set file_name to last text item of pathString
set AppleScript's text item delimiters to olddelims
return file_name
end getfilename

getfilename("myvolume:myfolder:myfile.txt")


Hope this helps.

H

Jun 26, 2006 6:29 PM in response to StephenZcat

on searchString(targetString, searchString, forwardDirection)
(*
This Handler searches a string for a smaller string and returns the positon of the found string.
Inputs:
targetString = string to be searched
searchString = string to be searched for
forwardDirection = (true/false) start at beginning of string or at end
0 is returned if no string is found.
*)

if forwardDirection is true then
set direction to 1
set iCount to 1
set endOfString to length of targetString
else
set direction to -1
set iCount to length of targetString
set endOfString to 1
end if

repeat until iCount = endOfString
if character iCount of targetString is searchString then
return iCount
else
set iCount to iCount + direction
end if
end repeat

set iCount to 0

end searchString

-------------------------------------------------------------------------------- ---------------------------------

on getFileName(pathString)
(*
Purpose: This Handler searches a file path string and looks for the last item in the
file path.
Dependancies: searchString handler
Input: pathString = myVolume:myFolder:myFile.txt
Output: myFile.txt
*)
set iCount to my searchString(pathString, ":", false)
return (text (iCount + 1) thru (length of pathString)) of pathString as string

end getFileName

Jun 26, 2006 6:30 PM in response to StephenZcat

on cutOutString(targetString, cutStart, cutLength)
(*
Purpose:
to cut out a string passed in from a larger string.
This handler became complex because applescript does not allow a range of 1 to 1
*)
set endOfString to length of targetString

if cutLength ≥ endOfString then
return ""
end if

if (cutStart + cutLength) ≥ endOfString then
cutLength = endOfString - cutStart
end if

if cutStart is 1 then
--return right portion of string
if (cutStart + cutLength) is endOfString then
set returnText to (character endOfString of targetString)
else
set returnText to text (cutStart + cutLength) thru endOfString of targetString
end if
else if (cutStart + cutLength) is endOfString then
--return left portion of string
if (cutStart - 1) is 1 then
set returnText to (character 1 of targetString)
else
set returnText to text 1 thru (cutStart - 1) of targetString
end if
else
--return left & right of string
if (cutStart - 1) is 1 then
set returnText to character 1 of targetString
else
set returnText to text 1 thru (cutStart - 1) of targetString
end if

if (cutStart + cutLength) is endOfString then
set returnText to returnText & character endOfString of targetString
else
set returnText to returnText & (text (cutStart + cutLength) thru endOfString of targetString)
end if
end if

return returnText
end cutOutString

Jun 27, 2006 8:53 AM in response to StephenZcat

Use this:

set my_string to "myvolume:myfolder:myfile.txt"
repeat until (offset of ":" in my_string) is 0
set the_offset to offset of ":" in my_string
set my_string to items (the_offset + 1) thru (count my_string) of my_string as string
end repeat
my_string

The offset command is located in the Standard Additions scripting addition, which is bundled with all versions of Mac OS 9 and Mac OS X. It is under String Commands when you open the addition.

(13598)

Jun 27, 2006 9:01 PM in response to Niel

Excellent! That's what I was looking for!

Thank-you Niel.

I did some more looking around on the internet and found someone else who had done something similar. They used reverse to reverse the string and find the last :

if not (thePath ends with ":") then
set x to the offset of ":" in (the reverse of every character of thePath) as string
set thePath to (characters 1 thru -(x) of thePath) as string
end if

here's where I found this one:

http://www.bigbold.com/snippets/posts/show/1046

Jun 27, 2006 11:36 PM in response to StephenZcat

Leaner version...

on cutOutString(targetString, cutStart, cutLength)

set endOfString to length of targetString
set leftText to ""
set rightText to ""

if cutStart = endOfString then
cutStart = endOfString - 1
end if

if cutStart + cutLength > endOfString then
set cutLength to endOfString - cutStart + 1
end if

set cutEnd to cutStart + cutLength

if cutStart > 1 then set leftText to text 1 thru (cutStart - 1) of targetString
if cutEnd < endOfString then set rightText to text cutEnd thru endOfString of targetString

return leftText & rightText

end cutOutString

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Return (character position from string) or (file name from file path)

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.