I have a handler that was supplied with one of my apps that is used in quite a few of my scripts. I would like to know how to modify it. I think I need to use TID but not sure how. The first option I would like to be able to do is replace the spaces with underscores. The second thing would be to limit the length of the name returned. Its one of those as string/text things
W_h_e_n_I_t_r_i_e_d_I_g_o_t_t_h_i_s
-- Returns the document name without extension (if present)
on getBaseName(fName)
set baseName to fName
repeat with idx from 1 to (length of fName)
if (item idx of fName = ".") then
set baseName to (items 1 thru (idx - 1) of fName) as string
exit repeat
end if
end repeat
return baseName
end getBaseName
One problem with the script you posted is that it will truncate a file name at the first “.” it encounters, so if the file name is “22.11.Bkup.txt”, it will return “22”. That can be fixed by looking for the last “.”, i.e.
repeat with idx from (length of fName) to 1 by -1
If the file name has spaces in it and you want to replace them with underscores, then this will work:
on getBaseName(fName)
set baseName to fName
repeat with idx from (length of fName) to 1 by -1
if (item idx of fName = ".") then
set baseName to (items 1 thru (idx - 1) of fName) as string
exit repeat
end if
end repeat
set {tid, text item delimiters} to {text item delimiters, space}
set parts to text items of baseName
set text item delimiters to "_"
set _Name to parts as string
set text item delimiters to tid -- always set them back!
return _Name
end getBaseName
getBaseName("22.11 Bkup.txt") --> "22.11_Bkup"
I should warn you that this fails if the file name does not have an extension, but does have a “.” in it and without a lot more work, it can’t deal with myFile.txt.zip
Found this script by Bruce Phillips in another thread. It strips extensions even if there is a period in the text, and is quite short.
set nameWithExtension to "MPG_30_02.20_7PM.mov"
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"."}
try
set newname to (text items 1 thru -2 of nameWithExtension) as Unicode text
on error
set newname to nameWithExtension
end try
set AppleScript's text item delimiters to ASTID
return newname
to getBaseName from t
set d to text item delimiters
set text item delimiters to "."
if (count t's text items) > 1 then ¬
set t to t's text 1 thru text item -2
set text item delimiters to space
set t to t's text items
set text item delimiters to "_"
tell t to set t to beginning & ({""} & rest)
set text item delimiters to d
t
end getBaseName
getBaseName from "22.11 Bkup.txt" --> "22.11_Bkup"
I like the “if (count t’s text items) > 1 then…” to avoid the null case. And of course “tell t to set beginning… and rest” is neat as well. Thanks. [I’m always so keen to get an answer together, that I don’t refine what I’ve done].
Thank you both, I will try out your suggestions, I think that all of these would work as my files never use “.” except before the suffix. In Kai’s soloution I have no idea what ({“”} & rest) does but adam thinks its neat so i’ll try it. Is there a way to then limit the length of the returned base name with the underscores should I need this function with something like 1 thru 20.
There’s a rather lengthy analysis in this discussion that might help, Mark. (Scroll down my message of 2006-01-10 08:19:24 - to the section that begins: “Now let’s take another look at the code I actually suggested:”.)
Briefly, though, it’s intended to return either plain or Unicode text - depending on the encoding of the original text. (In other words, it avoids the need for a conditional statement to ensure that any original encoding is preserved.)
Sure. If you want to pad out a result of less than 20 characters with underscore characters:
to getBaseName from t
set d to text item delimiters
set text item delimiters to "."
if (count t's text items) > 1 then ¬
set t to t's text 1 thru text item -2
set text item delimiters to space
set t to t's text items
set text item delimiters to "_"
tell t to set t to beginning & ({""} & rest)
set text item delimiters to d
(t & "____________________")'s text 1 thru 20
end getBaseName
getBaseName from "22.11 Bkup.txt" --> "22.11_Bkup__________"
Or if you just want to limit the result to 20 characters or less (without padding):
to getBaseName from t
set d to text item delimiters
set text item delimiters to "."
if (count t's text items) > 1 then ¬
set t to t's text 1 thru text item -2
set text item delimiters to space
set t to t's text items
set text item delimiters to "_"
tell t to set t to beginning & ({""} & rest)
set text item delimiters to d
if (count t) > 20 then return t's text 1 thru 20
t
end getBaseName
getBaseName from "22.11 Bkup with a very long name.txt" --> "22.11_Bkup_with_a_ve"
Kai, the later was what I was hoping for “thank you” for your help on this matter. I had noticed the other subject in your link, will take a look at this when I have a little more time. cheers.