I am still a beginner, but KM is indeed quite intuitive. My last macro does the following:
It goes in a text window (CafeTran, not Word) from the end to the beginning and deletes the first character. A very simple script. I have some trouble with trailing spaces in this kind of window, and it works. Unfortunately it deletes anything, but it shall only delete spaces, not any other characters.
Something like “check character to the right” -> IF character =“space” THEN delete ELSE continue with next action (in this case one more keystroke to move on to the next segment).
Am I assuming right that I need to copy the character to the right, set it as variable and check it or is there any easier way out?
I am not sure what you are trying to do. The following will remove all spaces (i.e., "trim") from the beginning and end of a string:
But it sounds like you want to keep the 2nd character even if it is a space (i.e., delete first character no matter what and delete trailing spaces). The straightforward way to do this is to filter the clipboard with a regex
^.(( *[^ ]+)*) *$. (My regex might not be the best, and I might have missed a simpler way to do this in KM.)
For any amount of knowledge and experience with regular expressions, there are always some that are uncomfortably obscure or complex, so let me explain this one for people who can't quite read it. This one says, starting with the contents of the clipboard,
- Skip the first character
- Include each run of –spaces followed by non-spaces–
- Skip all terminating spaces
- Put the characters included in step (2) into variable
Result back onto the Clipboard.
If instead of spaces you want to include any whitespace, use
\s instead of a space character in the regular expression.
An AppleScript solution might look like this:
-- Mitchell L Model, email@example.com, Jun 15, 2016
-- Remove the first character and all terminating spaces from the contents of the clipboard.
-- If the string consists entirely of spaces the clipboard will be empty.
set str to the clipboard
-- ignore return at end of lines
if length of str > 0 and (character -1 of str) is return then set str to text 1 thru -2 of str
set endPos to length of str
repeat while endPos > 2 and character endPos of str is in " "
set endPos to endPos - 1
set the clipboard to text 2 thru endPos of str
Hello, thanks for your help.
Indeed, I did not explain it well enough. Imaging the following text field
_This is a text with a leading space, marked with an underscore here to make it visible.
In most cases, my cursor will be at the right of the text or maybe somewhere in the middle. Now I want to delete the leading whitespace. This is all no problem with a very simple script, going to the first position of the sentence and delete the first character (the whitespace in this case).
But I only want to delete it if it is a whitespace, not if it is any other character.
Any leading whitespace or just the first character if it is a space?
And all terminating whitespace, correct?
And by “whitespace” do you mean exactly spaces, or do you mean to include other whitespace characters such as newlines, returns, tabs, etc.?
[Note I added an AppleScript above that we can use in addition to the two other approaches.]
And I left off solutions that Execute a Shell Script to run commands like the venerable “sed”.
It is in all cases only one leading space (space, nothing else) that has been inserted by Dragon Dictate, and that I want to get rid off with this. Though it would be nice to get rid off any trailing space(s), this is not necessary in this case.
You mentioned trailing spaces, so I added that feature.
Do you want to get rid of the first character no matter what it is?
set the clipboard to text 2 thru -1 of (the clipboard)
Or only if it is a space?
if character 1 of (the clipboard) is " " then
set startPosition to 2
set startPosition to 1
set the clipboard to text startPosition thru -1 of (the clipboard)
Neither of these deal with trailing spaces.
Regex break down of “
^ *(.*?) *$”
^ = match at start of line
* = match zero or more spaces (use “ ?” if you want to enforce at most one space)
(…) = capture the contents of the bracket so you can replace with $1
.*? = match zero or more but as few as possible (
*?), any characters (
* = match zero or more spaces
$ = match end of line
Note that I needed to add a short Pause to make it work in the web editor (ie, here), but that is because the web editor is very slow - in a native editor that generally would not be required.