My_Play_Lists ~BlackCatWhiteTail’s blog~

Talk About My Favorite Songs

applescript + numbers > フォントを操作する。

rangeクラスのプロパティを設定することで行う。

 applescript > 用語説明(shift + command + o)でNumbersの用語説明をひらき、検索窓で 'font' を検索して使い方を調べる。

 フォントサイズなどは 'range' クラスに属する プロパティらしい。

 したがって、document > sheet > table > range ( or cell ) のプロパティを設定すれば、フォントサイズや文字の色を設定できる。

プロパティを調べてみた

 そこで、5行x 5列の、それぞれのセルに 'name of cell' を入力したテーブルを準備して セル 'A1' の properties を調べた。

tell application "Numbers"
    activate
    if ((count of document) = 0) then
        set tgtDoc to make new document
    end if
    tell tgtDoc
        set tgtSheet to sheet 1
        tell tgtSheet

            -- tabele preparation
            delete every table
            set tgtTable to make new table with properties {row count:5, column count:5, header row count:0, header column count:0}
            tell tgtTable
                
                repeat with i from 1 to (count of every cell)
                    set value of cell i to (name of cell i)
                end repeat
            end tell
            -- properties of range "A1"
            tell tgtTable
                font name of range "A1"
                font size of range "A1"
                format of range "A1"
                alignment of range "A1"
                name of range "A1"
                text color of range "A1"
                text wrap of range "A1"
                background color of range "A1"
                vertical alignment of range "A1"
                
            end tell
            
        end tell
    end tell
end tell

結果は

プロパティー          結果
-------------------------------------
font name           "HiraKakuProN-W3"
font size           10.0
format              automatic
alignment           auto align
name                "A1:A1"
text color          {0,0,0}
text wrap           true
background color    missing value
vertical alignment  top

実際に設定してみる

 フォントサイズやテキストカラー、バックグラウンドカラーなどはこれを利用して設定できます。

 カラー(色)は三原色を0~65535で指定するようです[1]。

 プロパティ「alignment」は行方向の水平方向の位置で「left」「center」「right」「justify(テキストを左右両端で揃える)」「auto align」から選べます。

 プロパティ「vertical alignment」は垂直方向の位置で「top」「center」「bottom」から選ぶ。

~~略~~
--例
set font size of range "A1" to 20
set text color of range "A1" to {65535, 0, 0}
set background color of range "A1" to {0, 65535, 0}
~~略~~

ボールドやイタリックは設定が複雑?

 フォントの種類によって設定の仕方が異なる?のでプロパティを操作してボールドにするのは面倒かもしれない[2]。

 上記の質問サイトの例によるとボールドにするには、プロパティ「font name」で指定しなければならないようだ(VBAと異なり、font nameを指定し、そこにBoldを付加するのではなく)。

「HelveticaNeue」というフォントでボールドにする際には-Boldとつけるらしい。

tell application "Numbers"
  set font name of cell "B2" of table 1 of sheet 1 of document 1 to "HelveticaNeue-Bold"
end tell

 一方、フォントにヒラギノ角ゴProNを用いる場合、「HiraKakuProN-W6」とする。

~~略~~
set font name of range "A1" to "HiraKakuProN-W6"
~~略

 以上のようにフォントによってボールドにする方法が異なり?統一的な方法はないのかもしれず、面倒臭い。

 手っ取り早くボールド、イタリック、アンダーラインなどの付加したい場合はキー操作をエミュレーション[3]するほうが早いと思う。

 ただし、ショートカットが割り当てられていない操作(取り消し線など)はキーエミュレートでは対応できない。

set selection range to range "A2"
tell application "System Events" to keystroke "b" using command down
set selection range to range "A3"
tell application "System Events" to keystroke "i" using command down
set selection range to range "A4"
tell application "System Events" to keystroke "s" using command down

[1]:AppleScript and Numbers: Cells https://iworkautomation.com/numbers/cell.html

[2]: Set Bold/Italic with AppleScript in Numbe… - Apple Community https://discussions.apple.com/thread/8262497

[3]: AppleScript のサンプルコード | DevelopersIO https://dev.classmethod.jp/articles/applescript-sample/

translated with DeepL

This is done by setting a property of the range class.

 Use applescript > glossary (shift + command + o) to open the Numbers glossary, and search for 'font' in the search window to find out how to use it.

 It seems that font size and other properties belong to the 'range' class.

 Therefore, if you set the document > sheet > table > range ( or cell ) property, you can set the font size and the color of the text.

I checked the properties.

 So, I prepared a table with 5 rows x 5 columns, with 'name of cell' in each cell, and checked the properties of cell 'A1'.

Results.

Try to actually set it up.

 Font size, text color, background color, etc. can be set using this.

 The color (color) seems to be specified by the three primary colors in 0~65535 [1].

 The property "alignment" is the horizontal position in the line direction, and you can choose from "left", "center", "right", "justify", and "auto align".

 The property "vertical alignment" is the vertical position and you can choose from "top", "center", and "bottom".

Bold and italic are complicated to set up?

 Different font types have different settings? It may be troublesome to make it bold by manipulating the properties [2].

 According to the example of the above question site, to make it bold, you have to specify it with the property "font name" (unlike VBA, where you specify the font name and add Bold to it).

"(Unlike VBA, you don't specify the font name and add Bold to it.

 On the other hand, if you use Hiragino Kakugo ProN as the font, use "HiraKakuProN-W6".

 As mentioned above, different fonts have different ways to make bold? There may not be a uniform method, and it is troublesome.

 If you want to add bold, italic, underline, etc. quickly, it would be faster to emulate keystrokes [3].

 However, operations that do not have shortcuts assigned (such as strikethrough) cannot be handled by key emulation.

 このウェブサイトについて

 練習用のサイトです.

 自分の好きな曲について感想を述べます.

 また,世に役に立つかもと考えたことも書きます.

 

TOP 1 To 5

Title Artist Album
ロックバンド 中村中 私を抱いて下さい
犬の生活 SEX MACHINEGUNS SEX MACHINEGUNS
Megalovania Toby Fox UNDERTALE Soundtrack
Bokura No Fly Away Base Ball Bear Bokura No Fly Away - single
人魚の檻 陰陽座 迦陵頻伽(Ka Ryo Bin Ga