I want to determine the positions of hyphens in German compound words, like Wieder-Verschluss-Streifen. These words can contain 1 or more (3?) hyphens. I found this old post. Based on that I created this macro:
Easily done. In fact, you already did the hard part. Your loop already gives you the positions. You could either save all the positions that you collected, or process them right inside the loop you wrote. Since I don't know what your plan is regarding those hyphen positions, I'm not sure which direction to go here. I don't want to give the wrong answer, so I need to ask you what you plan to do with the position numbers.
I want to have access to the hyphen positions, in order to determine the positions of the composing words of the compound terms. I want to select these individual composing words:
Here is a macro example with "hard coded" hyphen positions:
Then you are doing the wrong thing. You don't want the position values of the string, you want the strings themselves. Fortunately, KM lets you do that, easily. Let me modify your action to show you how. In a minute...
The above code will give you the words. You don't need the position numbers of the words to get the words.
Perhaps the next question is what do you plan to do with the words?
EDIT: either I didn't read carefully or you edited your post. No matter. You said,
I want to select these individual composing words:
That's a different task. Okay, then my solution is inappropriate.
I'm puzzled why you would want to select the words. There are entirely different ways to select words using AppleScript or menu items. So may I ask why you want to select the words?
The result will display the position numbers of the hyphens. If you want to trim the output to contain only the numeric values, that's easy. If you want to replace the newlines with commas, that's easy too. Let me know what you want.
I'm using CafeTran Espresso, a Java app. In its current incarnation double-clicking on a hyphenated word selects the whole compound term, instead of one of the composing parts (as in BBEdit, TextEdit, etc.). Though the developer promised to change this unpractical behavior, I'd like to have a technique for selecting the parts on shelf. May in come handy in future :). After identifying the position of the hyphens, it's easy to select the first, second, third, etc. word. (By moving the text cursor macro-wise, with Shift pressed.)
As @Airy spotted, you don't actually want the positions of the hyphens in this case -- you want the length of each chunk. But once you know how to build one list with the "For Each" it's pretty easy to change the actions to suit:
Next step, should you want to try, would be to make that into a subroutine that accepted separator and a text-containing variable as parameters, returning the list...
Than you too! Actually, I need both. I figure that I'll let the selection macro start by selecting the middle part (in case of a compound with three words). I will have to come up with a smart way to switch to selecting the left or right word. For this I'll need the positions of the dashes too.
Ahhh. That's much clearer. Perhaps your approach is the best approach, but it's always wise to state the original problem so that we can offer different approaches.
Apologies if I'm missing the point, but if you just search/replace "-" with "\n" you can have a variable with all of the words on their own line. That's easy to process.
Alas, selecting the previous word that way is only possible when the macro creates the selection of the middle word from right to left: in that case, the collapsing ends at the start of the middle word (a Java quirk, I guess).
To make this technique work for selecting the right word ... the selection of the middle word has to be made from left to right.