Ok, first time playing with Applescript today and after 6-7 hours am 'almost' there. Basic premise of this script is that it should read two columns of data out of an Excel spreadsheet, one column is the phone number, the other is an alert. It then writes the message into iMessage/SMS and delivers it.
I have it working off of a CSV well, and now trying to use the source Excel file to bypass the conversion step. I am running into a classic number formatting issue where it reads the number into scientific notation and disrupts the phone number. On converting it back to a normal string iMessage seems to choke on the formatting. (at this point I know its something simple I am missing.)
set phoneCol to "O"
set messageCol to "P"
set startRow to 3
set endRow to 5
set xlsFilePath to (path to desktop as text) & "test.xlsx"
tell application "Microsoft Excel" to open file xlsFilePath
repeat with thisRow from startRow to endRow
tell application "Microsoft Excel"
set targetBuddyPhone to value of cell (phoneCol & thisRow) as string
set targetMessage to value of cell (messageCol & thisRow) as string
end tell
set targetBuddyPhone to number_to_string(targetBuddyPhone)
tell application "Messages"
send targetMessage to buddy targetBuddyPhone of service "SMS"
end tell
delay 2
end repeat
on number_to_string(this_number)
set this_number to this_number as string
if this_number contains "E+" then
set x to the offset of "." in this_number
set y to the offset of "+" in this_number
set z to the offset of "E" in this_number
set the decimal_adjust to characters (y - (length of this_number)) thru ¬
-1 of this_number as string as number
if x is not 0 then
set the first_part to characters 1 thru (x - 1) of this_number as string
else
set the first_part to ""
end if
set the second_part to characters (x + 1) thru (z - 1) of this_number as string
set the converted_number to the first_part
repeat with i from 1 to the decimal_adjust
try
set the converted_number to ¬
the converted_number & character i of the second_part
on error
set the converted_number to the converted_number & "0"
end try
end repeat
return the converted_number
else
return this_number
end if
end number_to_string
by contrast the following scrip to read the same variables from a CSV works with no issues at all, I did need to put a small delay in or I overran the buffer in iMessage.
set theFile to (choose file with prompt "Select the CSV file")
-- read the file contents:
set f to read theFile
-- break the file into paragraphs (c.f. rows)
repeat with row in (paragraphs of f)
-- parse the row into comma-delimited fields
set fields to parseCSV(row as text)
-- now you have your data:
set targetBuddyPhone to item 1 of fields
set targetMessage to item 2 of fields
tell application "Messages"
send targetMessage to buddy targetBuddyPhone of service "SMS"
end tell
delay 2
end repeat
on parseCSV(theText)
set {od, my text item delimiters} to {my text item delimiters, ","}
set parsedText to text items of theText
set my text item delimiters to od
return parsedText
end parseCSV
Related
I am trying to create a VBA project like this, but I'm having a hard time using the LIKE function and nothing seems to happen when I hit the run button.
What I'm trying to do:
If the first digit is either a number or a blank cell in B4:B245, then enter a text in range D4:245.
If the last digit of the numeric is even in C4:C245, then enter a text in range D4:D245.
More info:
Product codes were imported
LEFT function was used to find the "First digit of product code"
RIGHT function was used to find the "Numeric digits of product code"
My current position in excel and VBA:
Sub number()
Dim first As Range
Set first = Range("B4:B259")
Dim numeric As Range
Set numeric = Range("C4:B259")
Dim DColumn As Range
Set DColumn = Range("D4:D259")
For Each first In DColumn
If first Like " " Then
DColumn = "Invalid Part Number"
DColumn.Interior.ColorIndex = 6
End If
Next
End Sub
The below macro will perform 3 tests & each will get it's own output.
Check for Numeric or blank first character
Check for Even ending character
Check for Odd ending character
These test are not in unison - the output will be one, or none. As soon as a test statement is TRUE, the loop will end for that cell and other values will not be tested.
For example, this macro will not provide you outputs when #1 & #2 from above are true. It will only tell you if #1 is true.
This code does not require you to split the product codes. The macro will work with them as is
Sub MyNum()
Dim xCell As Range, Product_Code As Range
Set Product_Code = Sheets("Sheet1").Range("A2:A9") '<-- Update sheet name
For Each xCell In Product_Code
If IsNumeric(Left(xCell, 1)) Or Left(xCell, 1) = " " Then
xCell.Offset(0, 1) = "Invalid Product: Char 1 = Numeric or Null"
ElseIf Right(xCell, Len(xCell) - 1) Mod 2 = 0 Then
xCell.Offset(0, 1) = "Even Ending Range"
ElseIf Right(xCell, Len(xCell) - 1) Mod 2 <> 0 Then
xCell.Offset(0, 1) = "Odd Ending Range"
End If
Next xCell
End Sub
I'm currently creating an automation script where data from Excel will be searched in SAP GUI ALV List. I will be looping to the rows that, if it will match anything in the columns "Assignment", "DocumentNo" and "Quantity" to the "textToFind" in Excel, then I will be able to edit the text for each item matched:
How will I set the table and loop through the rows of the table until I find the text that I'm looking for?
I believe it will also only allow me to search for the visible rows.
I tried to record the steps in SAP GUI but it only gives me this if I position my cursor somewhere in the column "Assignment":
session.findById("wnd[0]").maximize
session.findById("wnd[0]/usr/lbl[18,15]").setFocus
session.findById("wnd[0]/usr/lbl[18,15]").caretPosition = 10
Which I know that it tells me the current cell address (column 18, row 15).
When I tried to check the table name on "Assignment" field (F1), it gives me the name of "RFPOSXEXT".
Let's assume you display the data in a ALV Grid and you have the session ready as you write in your post. Then the following code will copy the data from SAP into excel. You have to adjust the code according to your needs
Dim wks As Worksheet
Set wks = " your worksheet here ..."
Dim Table As Object
Dim cols As Long
Dim rows As Long
Dim i As Long, j As Long
Set Table = Session.FindById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell")
rows = Table.RowCount - 1
cols = Table.ColumnCount - 1
Dim columns As Object
Set columns = Table.ColumnOrder
Dim arrCol() As Variant
ReDim arrCol(cols)
For j = 0 To cols
arrCol(j) = (CStr(columns(j)))
Next
With wks
.Range(.Cells(1, 1), .Cells(1, cols + 1)).Value = arrCol()
End With
For i = 0 To rows
For j = 0 To cols
arrCol(j) = Table.GetCellValue(i, CStr(columns(j)))
Next
With wks
.Range(.Cells(i + 2, 1), .Cells(i + 2, cols + 1)).Value = arrCol()
End With
If i Mod 10 = 0 Then
Table.SetCurrentCell i, CStr(columns(0))
DoEvents
End If
Next
End Sub
The above code will fail if you don't use griv view control. "Session" must be a valid SAP Guisession pointing to FBL3N with the grid view open. In the link I provided above you will see hot to do that.
The screenshot shows an ALV (Grid View) but which is displayed via the "ABAP List" technology. It's not to be confused with the GuiGridView object.
A text in an ABAP List has no "table" or "field" name, whatever it's for representing an ALV or anything else (F1 is helpless). Only the column and row numbers can be used to get the texts or to move the cursor on these texts.
SAP GUI Scripting represents the texts from a screen of type ABAP List as a collection of contiguous GuiLabel, GuiTextField or GuiCheckBox, in the property Children of the GuiUserArea object.
You can loop at them by using this script, which shows all the fields in an ABAP List, and which is taken from this other answer about ABAP List (see that answer for more information):
text = ""
For Each field In session.findById("wnd[0]/usr").Children
text = text & field.CharTop & " " & field.CharLeft & " " & field.Text & " " _
& field.Id & " " & field.Type & chr(10)
Next
msgbox text
NB: here, the ABAP List is in the "window 0".
I need an applescript to find " and replace with just a space. I found an awesome script on here that works beautifully to find and replace whole words (hello, world) but when you manipulate the script to find " and replace with a space, the " corrupts the code and it no longer works. I am hoping somebody knows a way to alter this code to make it do what I want or have an other idea. here is the code (code credit goes to adamh):
searchAndReplaceTextInCells("hello", "world")
on searchAndReplaceTextInCells(search_str, replace_str)
tell application "Microsoft Excel"
set search_range to range "A:Z"
set all_found_ranges to {} -- store for the ranges, to manipulate after searching
set found_range to ""
set counter to 0
try
set found_range to find search_range what search_str with match case
on error
log ("No matches found")
end try
if (found_range is not "") then
set first_cell_address to (get address of the cells of found_range) -- we use this to break our loop
repeat while true
set counter to counter + 1
copy found_range to end of all_found_ranges
-- Now look for next result
set found_range to find next search_range after found_range
set cell_address to (get address of the cells of found_range)
if (cell_address = first_cell_address) then
-- have looped around so we are finished!
exit repeat
end if
end repeat
end if
-- walk all the ranges found and do the string replacing
repeat with r in all_found_ranges
set value of r to my replace_chars(the value of r, search_str, replace_str)
end repeat
log ("found and replaced " & counter & " items")
end tell
end searchAndReplaceTextInCells
on replace_chars(this_text, search_string, replacement_string)
set my text item delimiters to the search_string
set the item_list to every text item of this_text
set my text item delimiters to the replacement_string
set this_text to the item_list as string
set my text item delimiters to ""
return this_text
end replace_chars
As " is a reserved character we need to treat it differently when referencing it in strings.
You could use the quote constant as your argument:
searchAndReplaceTextInCells(quote, " ")
..or you could send it in as an escaped character:
searchAndReplaceTextInCells("\"", " ")
I'm working on cleaning a ton of data that has a common pattern like this:
REG#: 15082608 Date:15-JUN-15 BACKTRACK Cleared: Date:31-AUG-15 Recvd:13-MAY-15 Agency:OAKLAND (and about 25 other data points for each record but, following this pattern). A raw PDF file with a bunch of records is here: http://abc.ca.gov/reports/Actions2015/ActionsFinal_09-23-15.pdf
I'm not a programmer but, have tried Refine and a bunch of Excel tests but, haven't found a way I can do this for a large number of records (thousands but, will start with dozens :). So, my question is:
Could a script identify the colon ':' and then go backwards to the first space before that colon eg 'Date:15-JUN-15 BACKTRACK Cleared: Date:31-AUG-15' and enter a new line for each instance? So the resulting output would be:
Date:15-JUN-15 BACKTRACK
Cleared:
Date:31-AUG-15
The other question is that I can manually copy and paste each record (of all 25+ data points) into a unique cell but, what would be ideal is that I save the PDF as a spreadsheet and it basically builds a row for every row it finds - meaning in some cells there would be multiple colons and I would need the script to bump down the other rows accordingly.
Once I get to that place I can do a text-to-column and then build my database from there.
Select the cells containing the data and run this short macro:
Sub FixData()
Dim r As Range, v As String, vOut As String
For Each r In Selection
v = r.Text
vOut = ""
If v <> "" Then
ary = Split(v, " ")
For i = LBound(ary) To UBound(ary)
If InStr(1, ary(i), ":") > 0 Then
vOut = vOut & vbCrLf & ary(i)
Else
vOut = vOut & " " & ary(i)
End If
Next i
r.Value = vOut
End If
Next r
End Sub
Before:
and after:
I am trying to copy the nonblanks cells in my excel file to txt file. My data looks like:
1 2 3
1 2
1
1 2 3 4
So, if i select all and copy, txt file shows like the empty cells have data which is not something that i want.
I tried this:
Crtl+A
Go to special
Constants
Numbers
These commands selects the nonblank cells but I cannot copy them. Is there a way to copy them? I get:
That command cannot be used on multiple selections.
Thanks
If you got notepad++, you can use the regex find and replace to remove all the extra tabs at the end of a line.
Open the txt file in notepad++ and hit Ctrl+H.
In find, put:
\t+$
In replace, leave it blank.
Then check the radio button for the search mode from 'Normal' to 'Regular expression'. After that, hit 'Replace All' and this should be it.
Dim fso As FileSystemObject
Dim stream As TextStream
Dim str As String
Set fso = New FileSystemObject
Set stream = fso.CreateTextFile("c:\myTextFile.txt", True)
For i = 1 To 10
For Each cell In Range("A" & i & "F" & i)
If Not IsEmpty(cell) Then
str = str + cell.Text + " "
End If
Next cell
stream.writeline (str)
str = ""
Next i
stream.Close
End Sub
all you gotta do is change what you wanted separated by ( the " " at the end of the str line) and the range you want ( i = rows 1 through 10 as is) and ("a" & i ":f" & i which indicates a through f, in this case for rows 1 through 10)
hope this helps
With Word as your text editor, copy Excel as is and Paste Special as Unformatted Text. Replace ^t^t with ^t until no more replacements are made, then ^t^p with ^p.