How do I set the number of decimals in a Numbers cell using AppleScript? - numbers

I can set the format of a cell in a Numbers spreadsheet using:
tell application "Numbers"
activate
tell document 1
tell active sheet
set the selectedTable to ¬
(the first table whose class of selection range is range)
end tell
tell selectedTable
tell column "J"
set the format of cell 3 to number
end tell
end tell
end tell
end tell
But how do I set the number of decimal places of the cell?

Unfortunately the AppleScript dictionary of Numbers does not support to set the decimal places, however you can do it with GUI scripting.
Since GUI scripting strongly depends on the software version, this is for Numbers 3.6.2
tell application "Numbers"
activate
tell document 1
tell active sheet
set the selectedTable to ¬
(the first table whose class of selection range is range)
end tell
tell selectedTable
tell column "J"
set the format of cell 3 to number
end tell
end tell
end tell
end tell
tell application "System Events"
tell process "Numbers"
tell radio group 1 of window 1
if value of radio button "Cell" is 0 then
click radio button "Cell"
repeat until value of radio button "Cell" is 1
delay 0.2
end repeat
end if
end tell
tell text field 1 of scroll area 4 of window 1
set value of attribute "AXFocused" to true
set value to "2"
keystroke return
end tell
end tell
end tell
In a system language other than English the literal string Cell might be localized.

Unfortunately I don't have Numbers so I can't look at the dictionary directly myself, but in theory you should have a property for the cell for the decimal places.
This page talks about how you change the decimal places manually in the application. So it would stand to reason that you should be able to do similarly in a script.
Have you tried doing something along these lines...
tell application "Numbers"
tell document 1
tell selectedTable
tell column "J"
set props1 to properties of cell 3
set props2 to properties of format of cell 3
end tell
end tell
end tell
end tell
Take a look at what props1 and props2 contain in the previous code examples. With any luck, one of them will have a reference to "decimal places" (or something similar). Sorry for any mistakes in my code, as I mentioned, I don't have Numbers to actually test any of it.

Regrettably I believe this is not possible, at least not with Numbers v3.6.2. The Numbers dictionary shows that the a 'Decimals' property is not present for the cell or range classes
Taking your script and running with it:
tell application "Numbers"
activate
tell document 1
tell active sheet
set the selectedTable to ¬
(the first table whose class of selection range is range)
end tell
tell selectedTable
tell column "J"
set the format of cell 3 to number
get properties of cell 3
end tell
end tell
end tell
end tell
Reveals properties:
{vertical alignment:top, row:row "3" of table 1 of sheet 1 of document id "6F1021A0-BA54-47A2-A2DE-15B7E8DAFCED" of application "Numbers", class:cell, font name:"Helvetica", formatted value:"1.2345E+02", background color:missing value, formula:missing value, name:"J3", text wrap:true, text color:{0, 0, 0}, alignment:auto align, column:column "J" of table 1 of sheet 1 of document id "6F1021A0-BA54-47A2-A2DE-15B7E8DAFCED" of application "Numbers", format:scientific, font size:10.0, value:123.45}
.. none of which look like a decimals property :-(

Related

How to add phonetic guides to all the texts at once?

I have an essay with roughly 1000 Chinese words. I want to add phonetic guide (Pin Yin) on top of each Chinese word.
Therefore, in MS Words, I use Phonetic Guide. However, Phonetic Guide only allows me to create Pin Yin for 20 to 30 words each time. I tried to look for a function which allows me to add phonetic guides for all the words at once, but I cannot find an answer online.
I also want to make the phonetic guide font bigger and create more space between the Chinese text and the Pin Yin.
Can any expert give me some lights?
Not familiar with this area, but the starting point is that you can invoke the Phonetic Guide dialog box and get it to create the pinyin for the selection. For example
Sub testInsertPhoneticGuide()
Call insertPhoneticGuide(Selection.Range)
End Sub
Sub insertPhoneticGuide(r As Word.Range)
Dim d As Word.Dialog
Dim lng As Long
Dim lngChars As Long
Dim r1 As Word.Range
Dim r2 As Word.Range
On Error Resume Next
Set d = Word.Dialogs(wdDialogPhoneticGuide)
Set r1 = r.Duplicate
r1.TextRetrievalMode.IncludeFieldCodes = False
For lng = Len(r1.Text) To 1 Step -1
Set r2 = r1.Characters(lng)
' Do not insert pinyin for any range that
' contains a field (this will prevent the code from re-inserting
' pinyin, but you can change the way this works if you like)
If r2.Fields.Count = 0 Then
r2.Select
d.Show 1
' Error 6031 says there's no text to pinyin
If Err.Number = 6031 Then
Err.Clear
Else
On Error GoTo 0
End If
End If
Next
Set r2 = Nothing
Set r1 = Nothing
Set d = Nothing
End Sub
As far as I can tell, there is no way to specify the font and size/position parameters in the dialog box. They are not "sticky". But the Phonetic guide replaces each suitable character by an { EQ } field that contains the pinyin and the original character. The EQ looks somehting like this:
{ EQ \* jc2 \* "Font:SimSun" \* hps11 \o\ad(\s\up 10(fā),发) }
so as long as you want the same font, size and positioning, you should be able to display all the field codes and use Word Find/Replace to modify those values in every EQ field (or you could add code to modify the values for each character that you pinyin.
NB, there is also a PhoneticGuide() member of Word's Range object that lets you specify the pinyin text and the positioning parameters. However, to use that you would have to get the pinyin text somehow - the only way I know within Word is actually to use the Phonetic Guide dialog to insert it, but I imagine the necessary info for each character is available on the web.
In case anybody comes to this question again, after searching for a solution for a while I managed to add pinyin to my entire Chinese document by using the following two tools:
1) Open Office
2) The OO Pinyin Guide Extension for Open Office.
Hope this helps : )

How to bold the text in TOC, but not the dot leader not the page number?

I have created a table of contents (TOC) to my Microsoft Office Word 2013 document using different types of headings linked to the TOC. When I try to modify my TOC 1 style so that only the text inside that TOC paragraph would be bolded, it makes the whole paragraph bolded. What I want to achieve is this:
ONLY THE TEXT HERE IS BOLDED................................1
rather than bolding the dot leader lines nor the page number like this
THE WHOLE PARAGRAPH IS BOLDED.........................1
I can do this manually, but everytime I update my TOC, all these changes go away (everything is bolded/not bolded again). How can I bold only only the text (not dot leaders or page number) inside TOC-style?
Fields throw away edits when updated - there's little you can do to change this. There is a * MergeFormat switch that can be added to field codes in order to retain formatting. The major drawback, however, is that it "remembers" formatting by character location. So if the text changes (gets shorter or longer) the formatting "goes crazy". Therefore, I don't think that will help you.
So all you can really do is re-apply the formatting after each TOC update. The fastest way to do that is using code (a macro, for example).
Here's a macro that works in a quick test on my system. It searches for each TAB character in the TableOfContents. Then, based on the additional information from the OP that only the second tab should be recognized, uses a second range to check whether the position AFTER the tab is within one centimeter of the right margin.
If that's the case, it repositions the find-target Range from that point back towards the beginning of the paragraph, then applies the formatting.
Note 1: You can change the "cushion" used to determine the distance to the right margin by changing the formula assigned to PageNumLimit.
Note 2: I defined a STYLE named Toc1_Text and use that, rather than formatting with BOLD directly. Doesn't really matter, but it feels "cleaner" to me than direct formatting :-)
Sub FormatTextInTOC()
Dim rngFind As word.Range, rngFound As word.Range
Dim bFound As Boolean
Dim toc As word.TableOfContents
Dim infoH As Double, pageNumLimit As Double
Set toc = ActiveDocument.TablesOfContents(1)
toc.Update
Set rngFind = toc.Range
pageNumLimit = rngFind.Sections(1).PageSetup.RightMargin _
- CentimetersToPoints(1)
With rngFind.Find
.ClearFormatting
.Text = vbTab
.Style = word.WdBuiltinStyle.wdStyleTOC1
Do
bFound = .Execute
If bFound Then
Set rngFound = rngFind.Duplicate
'rngFound.Select
rngFound.Collapse wdCollapseEnd
infoH = rngFound.Information(_
wdHorizontalPositionRelativeToTextBoundary)
If infoH >= pageNumLimit Then
'rngFind.Select
rngFind.Collapse wdCollapseStart
rngFind.MoveStart wdParagraph, -1
rngFind.Style = "Toc1_Text"
rngFind.Start = rngFind.Paragraphs(1).Range.End
'rngFind.Select
End If
End If
Loop While bFound
End With
End Sub

In Applescript, finding only bold words in TextEdit causes app to hang

just trying to find a vanilla applescript way of setting a variable to all bold words of a document. I've looked for ways using Applescript in Word, Pages & TextEdit and TextEdit seems to be the only one (could be wrong, though).
The good news is that the following script works but the bad news is if the document is over 2 pages with let's say ~50 bolded words, TextEdit hangs.
Any other ways to get bolded words using Applescript?
tell application "TextEdit"
return words of text of document 1 where font contains "Bold"
end tell
Thanks
It may be that TextEdit is being unreasonably slow. I just used the following code for a document with 3600 words, with some random bold words interspersed throughout. It took way too long. I'd look into using different scriptable app, like Tex-Edit Plus (still goin' strong, still great).
The reason I used both "Black" and "Bold" is that some fonts use a "Black" variant instead of "Bold" when you make the text Bold (at least in TextEdit). Just to be clear, the following code works, but it is painfully slow. It may be that your code, if you waited long enough, works, too. :-( BUT keep reading :-)
tell application "TextEdit"
tell document 1
set thisManyRuns to count of attribute run of text of it
repeat with r from 1 to thisManyRuns
if font of attribute run r of text of it contains "Black" then
--do stuff
set color of attribute run r of text of it to {35555, 0, 0}
end if
if font of attribute run r of text of it contains "Bold" then
--do stuff
set color of attribute run r of text of it to {35555, 0, 0}
end if
end repeat
end tell
end tell
I just ran this text in Tex-Edit Plus and it completed in about two seconds:
tell application "Tex-Edit Plus"
tell document 1
set thisManyRuns to count of style run of text of it
repeat with r from 1 to thisManyRuns
set thisStyle to style of style run r of text of it
if on styles of thisStyle contains bold then
--do stuff
set color of style run r of text of it to {0, 35555, 0}
end if
end repeat
end tell
end tell
... so you might want to switch to that. Just a caveat that I had to put the style into the thisStyle variable before querying for the on styles property -- it would not work if I tried to do that in one line. I was working with an rtf file.

copy first three letters of some textbox and paste to another textbox

I know its a silly question but I still want to know it. I have two textboxes, textbox1 and textbox2. I entered some text in textbox1. Now I want that the first 3 characters of textbox1 first to be displayed in textbox2 when I move from textbox1 to textbox2 using tab index or by clicking my mouse on textbox2. I know I can make us of the mouse over event, but it will be great if I get some good opinions from you. Thanks in advance.
This should do the job :
Private Sub textbox2_GotFocus()
Dim length As Integer
length = Len(textbox1.Text)
If length > 3 Then length = 3
textbox2.Text = Left$(textbox1.Text, length)
End Sub
The GotFocus event will trigger if you click on the textbox or if you use tab key.

Macro, automatically adding one to a value

I have a problem with macro in notepad++
Just needs to have value automatically changed to +1 (from current value)
So the values should be like this 1000, 1001, 1002, 1003 and so on for the coming values between the MEDIA_ID tags..
<MEDIA>
<MEDIA_ID>1000</MEDIA_ID>
</MEDIA>
<MEDIA>
<MEDIA_ID>1001</MEDIA_ID>
</MEDIA>
I have no idea how do this..
Could anyone help?
Thanks
1) Generate the list of numbers from 1000 to the number you want using excel and copy this into notepad++.
2) Go to the start of the file.
3) Start the macro recorder.
4) type <MEDIA>(down arrow)(HOME button)<MEDIA_ID>(END button)</MEDIA>(down arrow)(HOME button)</MEDIA>(down arrow)(HOME button)
5) repeat this macro as many times as needed.
If you can align the tags vertically then you might be able to use the Column Editor to do the numbering. Say you format the XML like this:
<MEDIA><MEDIA_ID></MEDIA_ID></MEDIA>
<MEDIA><MEDIA_ID></MEDIA_ID></MEDIA>
....
<MEDIA><MEDIA_ID></MEDIA_ID></MEDIA>
Now, place your cursor betweeen the start and end tags of MEDIA_ID. Choose Column Editor... from the Edit menu (ALT+C keyboard shortcut). Change the radio button to Number to Insert with a starting value of 1001 incrementing by 1 (Decimal). Confirm using the OK button. You should end up with this:
<MEDIA><MEDIA_ID>1001</MEDIA_ID></MEDIA>
<MEDIA><MEDIA_ID>1002</MEDIA_ID></MEDIA>
....
<MEDIA><MEDIA_ID>1199</MEDIA_ID></MEDIA>
Sadly, it seems that this operations cannot be used in a macro. But maybe it helps.