I have this code that finds every item in the front finder window, and removes the beginning stuff in the filename. Each file has a number followed by a “-” and then the actual name. So the files look like this:
1-babs.jpg
22-madiosn.tif
334-cassie.eps
etc…etc…etc…the script works fine at removing the number and the “-” leaving just the original name, but I am having trouble wrapping my head around the 2 lines that ask to find the “-” and add 1 and the second line that uses the -1. I sort of get that it is looking for the hyphen and adding 1 to find the first character in the name and then subtracting -1 to get back to the hyphen, but I have 2 questions.
1-how does it know to keep the rest of the name from the first letter to the last?
2- why do you need the +1 and -1. Why can’t you just find the “-” and delete it and everything prior?
…and if that was an option, how could something like that be written?
tell application "Finder"
set these_items to (every document file of the front Finder window)
repeat with i from 1 to count of these_items
set this_item to item i of these_items
set this_name to the name of this_item
set the hyphen_offset to (the offset of "-" in this_name) + 1
set the original_name to text from hyphen_offset to -1 of this_name
set the name of this_item to the original_name
end repeat
again…here are the 2 lines I am having trouble understanding…maybe someone out there could explain this to me
set the hyphen_offset to (the offset of "-" in this_name) + 1
set the original_name to text from hyphen_offset to -1 of this_name
set the hyphen_offset to (the offset of "-" in this_name) + 1
offset of requests the character index in the string of the first occurrence of a character, in this case a hyphen. Adding one to that value, you get the index of the first character after the first occurrence of the hyphen.
An example:
set aString to "Name: ief2"
set myOffset to offset of ": " in aString -- myOffset is 5 (: + space, first char of pattern)
set myOffset to myOffset + 2 -- the space and the first letter of the name
get text myOffset thru - 1 of aString --> ief2
-- text requests the text items = 1 character
-- is (almost) equal to:
-- get (character myOffset thru -1 of aString) as text
.
-- example: 334-cassie.eps
set the hyphen_offset to (the offset of "-" in this_name) + 1
-- offset means the position of the first occurrence of the character "-" in the string this_name
-- offset = 4, after incrementing hyphen_offset points to character 5 (the "c")
set the original_name to text from hyphen_offset to -1 of this_name
-- -1 is a synonym for last character
-- original_name is set to characters 5 thru last character of the string this_name
.
Sorry i wasn’t more clear…
Yes, I know it is deleting those characters…I just think I was so confused by the whole +1 and -1 text, that I just thought there might be another way to just find the hyphen and delete everything before it, and including it, without having to write it that way…
Again, you know me…just try to learn and sometimes when I find different ways to do things, it helps in other areas
babs
set this_name to "334-cassie.eps"
set {TID, text item delimiters} to {text item delimiters, "-"}
set original_name to text from text item 2 to -1 of this_name
set text item delimiters to TID
--> "cassie.eps"