Applescript to resort piles of numbers - numbers

I'm trying to resort a bunch of numbers with Applescript. I'm very new to the language and I thought I'd ask you for help.
I have a group of numbers which looks like this in my TextEdit file:
v 0.186472 0.578063 1.566364
v -0.186472 0.578063 1.566364
v 0.335649 0.578063 1.771483
What i need is a script that resorts these numbers, making it appear like this:
(0.186472, 0.578063, 1.566364),
(-0.186472, 0.578063, 1.566364),
(0.335649, 0.578063, 1.771483),
So after each number, there has to be a comma, and always the three numbers on one line have to be put into brackets (). finally there has to be another comma after every bracketed group of three and the v before every line has to be deleted.
I've only so far managed to get rid of every "v" using:
set stringToFind to "v"
set stringToReplace to ""
But now im stuck and I'm hoping for help.

To find and replace strings in AppleScript the native way is using text item delimiters. There are a fixed number of values separated by spaces (or tabs) on each line, using text item delimiters, text itemsand string concatenation we can solve your problem.
I've added an addition linefeed in front and at the back of the string to show that lines that doesn't contain 4 words are ignored.
set theString to "
v 0.186472 0.578063 1.566364
v -0.186472 0.578063 1.566364
v 0.335649 0.578063 1.771483
"
set theLines to paragraphs of theString
set oldTIDs to AppleScript's text item delimiters
repeat with i from 1 to count theLines
set AppleScript's text item delimiters to {space, tab}
if (count of text items of item i of theLines) = 4 then
set theNumbers to text items 2 thru -1 of item i of theLines
set AppleScript's text item delimiters to ", "
set item i of theLines to "(" & (theNumbers as string) & "),"
else
set item i of theLines to missing value
end if
end repeat
set theLines to text of theLines
set AppleScript's text item delimiters to linefeed
set newString to theLines as string
set AppleScript's text item delimiters to oldTIDs
return newString

Related

Randomly choose 1 text-line from each of 3 lists of text-lines but instead it brings 3 blank text-lines

I have this code to randomly choose one line of text from each of the three lists of text lines within the same file, and copy them together to the clipboard.
It works but not properly because it brings three blank lines instead of three actual text lines.
It seems to me that the issue may be in the way this code is copying the text lines, but I'm not an expert.
Can somebody help me find where the issue is and maybe code it properly to bring the actual text lines instead of the blank ones?
My AutoHotkey is version 1.1.36.02 . Thanks in advance to everyone.
Random, rand1, 1, 4
Random, rand2, 1, 4
Random, rand3, 1, 4
; Definining 3 lists of text lines:
list1 = 1st text line, 2nd text line, 3rd text line, 4th text line
list2 = 5th text line, 6th text line, 7th text line, 8th text line
list3 = 9th text line, 10th text line, 11th text line, 12th text line
; Selecting randomly one text line from each list:
selectedLine1 := list1[rand1]
selectedLine2 := list2[rand2]
selectedLine3 := list3[rand3]
; Concatenating the 3 selected text lines and copy them to the clipboard:
clipboard = %selectedLine1% `n %selectedLine2% `n %selectedLine3%
; Seeing the result on a message box:
msgbox, Randoms: %rand1%, %rand2%, %rand3%`nSelected lines:`n%selectedLine1%`n%selectedLine2%`n%selectedLine3%
This solves the problem:
Random, rand1, 1, 4
Random, rand2, 1, 4
Random, rand3, 1, 4
; Definining 3 lists of text lines:
list1 = 1st text line, 2nd text line, 3rd text line, 4th text line
list2 = 5th text line, 6th text line, 7th text line, 8th text line
list3 = 9th text line, 10th text line, 11th text line, 12th text line
; Selecting randomly one text line from each list:
selectedLine1 := strsplit(list1,",")[rand1]
selectedLine2 := strsplit(list2,",")[rand2]
selectedLine3 := strsplit(list3,",")[rand3]
; Concatenating the 3 selected text lines and copy them to the clipboard:
clipboard = %selectedLine1% `n %selectedLine2% `n %selectedLine3%
; Seeing the result on a message box:
msgbox, Randoms: %rand1%, %rand2%, %rand3%`nSelected lines:`n%selectedLine1%`n%selectedLine2%`n%selectedLine3%

Finding text AND fields with variable content in Word

I need to find and delete every occurrence of the following pattern in a Word 2010 document:
RPDIS→ text {INCLUDEPICTURE c:\xxx\xxx.png" \*MERGEFORMAT} text ←RPDIS
Where:
RPDIS→ and ←RPDIS are start and end delimiters
Between the start and end delimiters there can be just text or text and fields with variable content
The * wildcard in the Word Find and Replace dialog box will find the pattern if it contains text only but it will ignore patterns where text is combined with fields. And ^19 will find the field but not the rest of the pattern until the end delimiter.
Can anyone help, please?
Here's a VBA solution. It wildcard searches for RPDIS→*←RPDIS. If the found text contains ^19 (assuming field codes visible; if objects are visible instead of field codes, then the appropriate test is text contains ^01), the found text is deleted. Note that this DOES NOT care about the type of embedded field --- it will delete ANY AND ALL embedded fields that occur between RPDIS→ and ←RPDIS, so use at your own risk. Also, the code has ChrW(8594) and ChrW(8592) to match right-arrow and left-arrow respectively. You may need to change that if your arrows are encoded differently.
Sub test()
Dim wdDoc As Word.Document
Dim r As Word.Range
Dim s As String
' Const c As Integer = 19 ' Works when field codes are visible
Const c As Integer = 1 ' Works when objects are visible
Set wdDoc = ActiveDocument
Set r = wdDoc.Content
With r.Find
.Text = "RPDIS" & ChrW(8594) & "*" & ChrW(8592) & "RPDIS"
.MatchWildcards = True
While .Execute
s = r.Text
If InStr(1, s, chr(c), vbTextCompare) > 0 Then
Debug.Print "Delete: " & s
' r.Delete ' This line commented out for testing; remove comments to actively delete
Else
Debug.Print "Keep: " & s
End If
Wend
End With
End Sub
Hope that helps.

Access, How can I change lowercase letter of first letter in last name to uppercase

I would like to change lowercase letter of first letter in last name to uppercase by using code
my code from form is :
Option Compare Database
Private Sub Text19_Click()
Text19 = UCase(Text19)
End Sub
but there is no change to my table!
Furthermore, how can I find last name with a space, comma or period and make it without a space, comma and period.
such as
Moon,
Moon.
[space] Moon
change them to just
Moon
If there is no change to your table, maybe your field is not bound to the recordset? Maybe you need to 'Refresh' your form.
Also, it looks like you are trying to use this code on a TextBox?
Code would be as follows:
Private Sub Text19_DblClick(Cancel As Integer)
Text19 = Trim(Text19) ' Get rid of leading and trailing spaces.
If right(Text19, 1) = "." Or right(Text19, 1) = "," Then ' Remove comma, period
Text19 = left(Text19, Len(Text19) - 1)
End If
Text19 = UCase(left(Text19, 1)) & Mid(Text19, 2)
End Sub

maskedEditColumn datagridview how to use class? is it what i need?

I am trying to mask user's input in a datagridview column and i found this ready class Masked edit column Class that adds a 'mask edit column' option in the column types list. When i select this column type a mask field is being added in the list of column properties. I tried to do my job by adding some mask elements in this 'Mask' field, but when I run the code it didnt restrict me from adding other characters. I re-opened the 'edit columns menu' and I saw that the 'Mask' field was empty.
I want the text cell to accept 20 chars maximum and only: 1.Capital Letters(English & Greek), 2.these three chars(.,-), 3.Numbers 0-9
So as a first test i used only this mask(>????????????????????) but it didnt work as it didnt convert my characters to Uppercase and accepted more than 20 chars when i end the cell edit.
i am not sure the way to go is the Masked Text Box way. i have made many projects on vb and i used to use a loop in the textChanged event of a text box to restrict characters entry. the loop is this : (but i cant use it now in the valueChanged event cause it seems that 'value' doesn't have a selectionStart property.)
Dim charactersDisallowed As String = "!##$%^&*()+=|}{][:;?/><.,~""
Dim theText As String = txtCopies.Text
Dim Letter As String
Dim SelectionIndex As Integer = txtCopies.SelectionStart
Dim Change As Integer
For x As Integer = 0 To txtCopies.Text.Length - 1
Letter = txtCopies.Text.Substring(x, 1)
If charactersDisallowed.Contains(Letter) Then
theText = theText.Replace(Letter, String.Empty)
Change = 1
End If
Next
txtCopies.Text = theText
txtCopies.Select(SelectionIndex - Change, 0)
So,
Is a masked text cell what i need? and if yes( Why is this mask box not keeping the mask i enter? And how can i use this class to do my job?)
What can i alternately do to restrict some characters in a column's cells? (I will then convert to Uppercase on cellEndEdit)
I finally did it by removing the unwanted characters on cellvaluechanged event, which seems that is being raised when I end the cell's edit by for example hitting "Enter".

Autohotkey: Splitting a concatenated string into string and number

I am using an input box to request a string from the user that has the form "sometext5". I would like to separate this via regexp into a variable for the string component and a variable for the number. The number then shall be used in a loop.
The following just returns "0", even when I enter a string in the form "itemize5"
!n::
InputBox, UserEnv, Environment, Please enter an environment!, , 240, 120
If ErrorLevel
return
Else
FoundPos := RegExMatch(%UserEnv%, "\d+$")
MsgBox %FoundPos%
retur
n
FoundPos, as its name implies, contains the position of the leftmost occurrence of the needle. It does not contain anything you specifically want to match with your regex.
When passing variable contents to a function, don't enclose the variable names in percent signs (like %UserEnv%).
Your regex \d+$ will only match numbers at the end of the string, not the text before it.
A possible solution:
myText := "sometext55"
if( RegExMatch(myText, "(.*?)(\d+)$", splitted) ) {
msgbox, Text: %splitted1%`nNumber: %splitted2%
}
As described in the docs, splitted will be set to a pseudo-array (splitted1, splitted2 ...), with each element containing the matched subpattern of your regex (the stuff that is in between round brackets).