Archive for the 'Numbers' Category
Numbers上で選択中のデータをもとに、OmniGraffleで選択中のグラフィックのサイズの表を作成するAppleScriptです。
Numbersのテーブル上で表を作成する対象のデータを選択しておきます。OmniGraffleの最前面の書類上で、四角のボックスを作成して選択状態にしておきます。
この状態で本AppleScriptを実行すると、OmniGraffle上で四角のボックスを削除したうえで、起点やサイズなどを参考にしつつ表を作成します。
| スクリプト名:Numbersで選択中のデータをもとにOmniGraffleの選択中のグラフィックのサイズで表作成 |
property cellYohakuPoint : 3 –セル内の余白(上下/左右を同値で指定) property textPointSize : 12 –セル内の文字サイズ(ポイント数) property textFontName : "HiraKakuProN-W3" –セル内のフォント property headerFillCol : {0.901961, 0.901961, 0.901961} –ヘッダー行の塗り色(うすいグレー)
–Numbersの選択範囲からデータを取得する(Excel-Like Nested List Format) set aList to retExcelLikeStyleDataFromNumbersSelection() of me
set heightOfList to length of aList –データの高さ(行数)を取得 set widthOfList to length of first item of aList –データの幅(列数)を取得
–Omni Graffleの選択オブジェクト情報を取得 tell application "OmniGraffle 5" tell front window set aSel to selection set aaSel to first item of aSel set {xSize, ySize} to size of aaSel set {xOrigPos, yOrigPos} to origin of aaSel delete aaSel –位置指定用のオブジェクトを削除 end tell end tell
–表のセルの大きさを計算する set xStep to xSize / widthOfList set yStep to ySize / heightOfList
–テーブル用のオブジェクトリスト set tObjList to {}
tell application "OmniGraffle 5" tell canvas of front window repeat with y from 1 to heightOfList repeat with x from 1 to widthOfList set aText to contents of item x of item y of aList –Numbersの表の空きセルからデータを取ったときに数値の0.0が返る現象への対策 if aText = 0.0 then set aText to "" end if set tmpXpos to xOrigPos + ((x - 1) * xStep) set tmpYpos to yOrigPos + ((y - 1) * yStep) –セルを作成する if y = 1 then –ヘッダー行の場合にはセル内を指定色で塗る set aCellObj to make new shape at end of graphics with properties {fill:solid fill, fill color:headerFillCol, draws shadow:false, size:{xStep, yStep}, side padding:cellYohakuPoint, thickness:0.25, autosizing:vertically only, vertical padding:cellYohakuPoint, origin:{tmpXpos, tmpYpos}, text:{text:aText, font:textFontName, size:textPointSize}} else –通常行はセル内を塗りつぶさない set aCellObj to make new shape at end of graphics with properties {fill:no fill, draws shadow:false, size:{xStep, yStep}, side padding:cellYohakuPoint, thickness:0.25, autosizing:vertically only, vertical padding:cellYohakuPoint, origin:{tmpXpos, tmpYpos}, text:{text:aText, font:textFontName, size:textPointSize}} end if set the end of tObjList to aCellObj end repeat end repeat –セルを表に合成する assemble tObjList table shape {heightOfList, widthOfList} end tell end tell
–Numbersで選択範囲を縦に区切ったリストを返す on retExcelLikeStyleDataFromNumbersSelection() tell application "Numbers" tell document 1 tell sheet 1 tell table 1 set selList to value of every cell of selection range –選択範囲のデータを取得 set selName to name of selection range –選択範囲のrange情報を取得 set {s1, s2} to parseByDelim(selName, ":") of me –始点の情報を取得する set s1Row to (address of row of range s1) as integer set s1Column to (address of column of range s1) as integer –終点の情報を取得する set s2Row to (address of row of range s2) as integer set s2Column to (address of column of range s2) as integer –選択範囲の情報を取得する set selHeight to s2Row - s1Row + 1 –高さ(Height of selection range) set selWidth to s2Column - s1Column + 1 –幅(Width of selection range) end tell end tell end tell end tell set aLen to length of selList set aaLen to selHeight set bList to {} repeat with i from 1 to aaLen set aHoriList to {} repeat with ii from 1 to selWidth set j1 to ii + (i - 1) * selWidth set tmpCon to contents of item j1 of selList set aClass to class of tmpCon if aClass = number or aClass = integer or aClass = real then set tmpCon to tmpCon as integer end if set the end of aHoriList to tmpCon end repeat set the end of bList to aHoriList end repeat return bList end retExcelLikeStyleDataFromNumbersSelection
on parseByDelim(aData, aDelim) set curDelim to AppleScript’s text item delimiters set AppleScript’s text item delimiters to aDelim set dList to text items of aData set AppleScript’s text item delimiters to curDelim return dList end parseByDelim
|
|
▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に
|
Posted in アプリケーション操作(app control), 10.6対応, Numbers, OmniGraffle | No Comments »
Numbersで選択中の内容をMac OS X 10.7で採用された日本語の読み上げキャラクター(kyoko)を指定して読み上げるAppleScriptです。

このような(1列の)選択状態のデータを読み上げます。複数行×複数列の内容を読み上げる場合には、適当に内容を変更してください。
日本語音声の「Kyoko」をインストールしていない状態(OSのインストール直後の状態)では日本語音声は出ないので、その点は注意してください。
Kyokoを指定しなければ(英語+数字の範囲であれば)、Mac OS X 10.6/10.5/10.4でも使えますし、そういう環境では「SayKana」などのフリーのツールを使えば日本語による読み上げは可能です。
| スクリプト名:Numbersで選択中の内容をText to Speechで読み上げる |
tell application “Numbers” tell table 1 of sheet 1 of document 1 set aList to value of every cell of selection range end tell end tell
repeat with i in aList say i using “Kyoko” end repeat
|
|
▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に
|
Posted in アプリケーション操作(app control), Numbers, 10.7対応 | No Comments »
指定フォルダ内に入っている画像を順次Numbersのドキュメントに貼り付けるAppleScriptです。
指定フォルダ内の画像をPhotoshopで順次オープンして、Numbersのドキュメントに貼り付けます。
Numbersには画像を貼り込むような命令は用意されていないので、GUI Scripting経由でペーストを行います。
| スクリプト名:フォルダ内の画像ファイルを順次Numbersのセルに貼り付ける |
property aRange : “B”
set aFol to choose folder with prompt “Numbersに貼り付ける画像が入っているフォルダを選択してください”
tell application “Finder” set aList to (every file of aFol) as alias list end tell
set aLen to length of aList
tell application “Numbers” tell document 1 tell sheet 1 tell table 1 set row count to aLen + 5 end tell end tell end tell end tell
set aCounter to 2
repeat with i in aList tell application “Adobe Photoshop CS3″ activate open i set d1Doc to (a reference to current document) tell d1Doc select all copy end tell tell document 1 close saving no end tell end tell set aRangeStr to aRange & (aCounter as string) & “:” & aRange & (aCounter as string) tell application “Numbers” tell document 1 tell sheet 1 tell table 1 set selection range to range aRangeStr end tell end tell end tell end tell activate application “Numbers” tell application “System Events” tell process “Numbers” keystroke “v” using {command down} end tell end tell set aCounter to aCounter + 1 end repeat |
|
▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に
|
Posted in アプリケーション操作(app control), 10.5対応, 10.6対応, Numbers, GUI Scripting, Photoshop CS3, 10.4対応 | No Comments »
Numbersのシート中の表(テーブル)の縦のセル数を増やすAppleScriptです。
意外といい加減に変更できてびっくりです。
| スクリプト名:Numbersでtableの縦サイズをふやす |
tell application "Numbers" tell document 1 tell sheet 1 tell table 1 set rowNum to row count set row count to rowNum + 5 end tell end tell end tell end tell |
|
▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に
|
Posted in アプリケーション操作(app control), 10.5対応, 10.6対応, Numbers, 10.4対応 | No Comments »
Numbers上の選択したセルから、指定の入れ子になっているリストの値で埋めるAppleScriptです。
選択されていなかった場合には、左上の原点を基準にデータが展開されます。

もしも、シート上の表のサイズよりもリストのデータ要素数が多かった場合にはエラーになります。対策方法もすでに分かっていて、表を適宜リサイズして広げてあげればエラーは回避できます。
「とりあえず動く」というレベルを目指したので、セルを埋める様子を目で追うことができる程度のスピードですが、真剣に最適化すればもう少し効率のよいコントロールは可能と思っています。
| スクリプト名:選択したセルからNumbers上のセルを指定リストの値で埋める |
set aList to {{“abc”, “10″}, {“bcd”, “20″}, {“cde”, “30″}} fillCellFromSelectedCellOnNumbersByList(aList) of me
–与えたリストのデータ(入れ子のリスト)を、Numbersで現在選択中のセルから展開する –セルが選択されていなかった場合には原点から展開 –展開後のデータ範囲が現在のテーブルの範囲を超える場合にはエラーになる on fillCellFromSelectedCellOnNumbersByList(aList) set dataRow to length of aList set dataColumn to length of first item of aList set {aRow, aColumn} to getSelectedRowColumn() of me if {aRow, aColumn} = {0, 0} then set aRow to 1 set aColumn to 1 end if set fromAddress to getNameOfNumbersAddress(aRow, aColumn) of me set toAddress to getNameOfNumbersAddress(aRow + dataRow - 1, aColumn + dataColumn - 1) of me set dataRange to fromAddress & “:” & toAddress –リストをフラット化 set aaList to FlattenList(aList) of me fillCellOnNumbers(aaList, dataRange) of me end fillCellFromSelectedCellOnNumbersByList
–指定範囲のNumbers上のセルを指定リストの値で埋める –ループでcellを埋めているので、処理が遅い on fillCellOnNumbers(aList, aRangeAddress) tell application “Numbers” tell document 1 tell sheet 1 tell table 1 –cellのリストが返ってくる set aCellList to cell of range aRangeAddress set cCount to 1 repeat with i in aCellList set aCon to contents of item cCount of aList set value of i to aCon set cCount to cCount + 1 end repeat end tell end tell end tell end tell end fillCellOnNumbers
–指定したNumbersのcell/rowのnameを返す on getNameOfNumbersAddress(aRow, aColumn) try tell application “Numbers” tell document 1 tell sheet 1 tell table 1 set aName to name of cell aRow of column aColumn end tell end tell end tell end tell on error return “” end try end getNameOfNumbersAddress
–Numbersで選択されているセルのRowとColumnを取得する on getSelectedRowColumn() tell application “Numbers” tell document 1 tell sheet 1 tell table 1 try set selList to value of every cell of selection range –選択範囲のデータを取得 on error return {0, 0} end try set selName to name of selection range –選択範囲のrange情報を取得 set {s1, s2} to parseByDelim(selName, “:”) of me –始点の情報を取得する set s1Row to (address of row of range s1) as integer set s1Column to (address of column of range s1) as integer –終点の情報を取得する set s2Row to (address of row of range s2) as integer set s2Column to (address of column of range s2) as integer –選択範囲の情報を取得する set selHeight to s2Row - s1Row + 1 –高さ(Height of selection range) set selWidth to s2Column - s1Column + 1 –幅(Width of selection range) end tell end tell end tell end tell return {s1Row, s1Column} end getSelectedRowColumn
–与えられた文字列を、指定デリミタ文字でparseしてリストにして返す on parseByDelim(aData, aDelim) set curDelim to AppleScript’s text item delimiters set AppleScript’s text item delimiters to aDelim set dList to text items of aData set AppleScript’s text item delimiters to curDelim return dList end parseByDelim
–By Paul Berkowitz –2009年1月27日 2:24:08:JST –Re: Flattening Nested Lists on FlattenList(aList) set oldDelims to AppleScript’s text item delimiters set AppleScript’s text item delimiters to {“????”} set aString to aList as text set aList to text items of aString set AppleScript’s text item delimiters to oldDelims return aList end FlattenList
|
|
▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に
|
Posted in アプリケーション操作(app control), 10.5対応, 10.6対応, Numbers, 10.4対応 | No Comments »
Numbers 2で、選択範囲を横に区切ったリストを返すAppleScriptです。
「機能がないならないなりに、Appleに頼らずに機能を追加するシリーズ」の第2弾です。
Numbersの選択範囲から、データが行ごとに入れ子になったリストを返します。Excelと同じ動作を行う、といったほうが早いかもしれません。
こういう状態で、

実行すると、
{{”Apple”, “Lemon”, “Strawberry”}, {10.0, 20.0, 30.0}}
と結果が返ってきます。
| スクリプト名:Numbersで選択範囲を横に区切ったリストを返す |
–Numbersで選択範囲を縦に区切ったリストを返す tell application "Numbers" tell document 1 tell sheet 1 tell table 1 set selList to value of every cell of selection range –選択範囲のデータを取得 set selName to name of selection range –選択範囲のrange情報を取得 set {s1, s2} to parseByDelim(selName, ":") of me –始点の情報を取得する set s1Row to (address of row of range s1) as integer set s1Column to (address of column of range s1) as integer –終点の情報を取得する set s2Row to (address of row of range s2) as integer set s2Column to (address of column of range s2) as integer –選択範囲の情報を取得する set selHeight to s2Row - s1Row + 1 –高さ(Height of selection range) set selWidth to s2Column - s1Column + 1 –幅(Width of selection range) end tell end tell end tell end tell
set aLen to length of selList set aaLen to selHeight
set bList to {} repeat with i from 1 to aaLen set aHoriList to {} repeat with ii from 1 to selWidth set j1 to ii + (i - 1) * selWidth set the end of aHoriList to contents of item j1 of selList end repeat set the end of bList to aHoriList end repeat
bList –> {{"Apple", "Lemon", "Strawberry"}, {10.0, 20.0, 30.0}}
on parseByDelim(aData, aDelim) set curDelim to AppleScript’s text item delimiters set AppleScript’s text item delimiters to aDelim set dList to text items of aData set AppleScript’s text item delimiters to curDelim return dList end parseByDelim
|
|
▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に
|
Posted in アプリケーション操作(app control), 10.5対応, 10.6対応, Numbers | No Comments »
Numbers 2で、選択範囲のデータを縦に区切ったリストを返すAppleScriptです。
そもそも、Excelでは行ごとに横に区切ったリストが返ってくるのですが、Numbersでは複数行にまたがって選択した状態でデータを取得しても、行ごとに分けられた入れ子のリストでデータが返ってくるわけではありません。
そこで、機能がないならないなりに、選択範囲からさまざまな形でデータを取り出すAppleScriptを書いてみました。
このScriptは、

という状態で実行すると、
{{”Apple”, 10.0}, {”Lemon”, 20.0}, {”Strawberry”, 30.0}}
と、結果が返ってきます。
| スクリプト名:Numbersで選択範囲を縦に区切ったリストを返す |
–Numbersで選択範囲を縦に区切ったリストを返す
tell application "Numbers" tell document 1 tell sheet 1 tell table 1 set selList to value of every cell of selection range –選択範囲のデータを取得 set selName to name of selection range –選択範囲のrange情報を取得 set {s1, s2} to parseByDelim(selName, ":") of me –始点の情報を取得する set s1Row to (address of row of range s1) as integer set s1Column to (address of column of range s1) as integer –終点の情報を取得する set s2Row to (address of row of range s2) as integer set s2Column to (address of column of range s2) as integer –選択範囲の情報を取得する set selHeight to s2Row - s1Row + 1 –高さ(Height of selection range) set selWidth to s2Column - s1Column + 1 –幅(Width of selection range) end tell end tell end tell end tell
set aLen to length of selList set aaLen to selWidth
set bList to {} repeat with i from 1 to aaLen set aVerList to {} repeat with ii from 1 to selHeight set j1 to i + (ii - 1) * aaLen set the end of aVerList to contents of item j1 of selList end repeat set the end of bList to aVerList end repeat
bList –> {{"Apple", 10.0}, {"Lemon", 20.0}, {"Strawberry", 30.0}}
on parseByDelim(aData, aDelim) set curDelim to AppleScript’s text item delimiters set AppleScript’s text item delimiters to aDelim set dList to text items of aData set AppleScript’s text item delimiters to curDelim return dList end parseByDelim
|
|
▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に
|
Posted in アプリケーション操作(app control), 10.5対応, 10.6対応, Numbers, 10.4対応 | No Comments »
Numbersで、オープン中の書類の中にあるシートのうち、指定のものの中にあるテーブルをコピーして、同じものを複数追加するAppleScriptです。
Numbers 2のAppleScript対応機能は不十分で、本当にやりたいことはほとんどできません。現在表示中のワークシートを取得することも、選択中のテーブルを取得することも、指定のオブジェクト(ワークシート/テーブル)を選択状態にすることもできません。機能のないないづくしです。
現在表示中のワークシート内のテーブルの内容をコピーすることはできないので、とりあえずワークシートの名前一覧を取得し、その中から選択して、テーブルをコピーすることに。

▲Numbersで書類をオープンしているところ

▲書類内のワークシート名称一覧から選択

▲指定のワークシート内のテーブル1から、幅と高さ、内容データを取得して同じものを追加
| スクリプト名:Numbersで指定シート上の最初のテーブルと同じデータとサイズでシート&テーブルを連続作成 |
tell application "Numbers" tell document 1 set sList to name of every sheet set aRes to choose from list sList if aRes = false then return –キャンセルした場合はリターン set aSheetName to first item of aRes –指定シート上のテーブル1をスキャンして学習する tell sheet aSheetName tell table 1 set widthNum to column count –幅 set hightNum to row count + 1 –高さ set aRange to name of cell range –セル範囲 set dataList to value of every cell –データ end tell end tell –同じものをひたすら連続して作成する repeat with i from 1 to 4 –回数は適当(根拠なし) set sRes to make new sheet tell sRes tell table 1 set column count to widthNum set row count to hightNum set selection range to range aRange set cellList to cell of selection range set iCount to 1 repeat with ii in cellList tell ii set aVal to (item iCount of dataList) –このへんはお好きなように(空きセルに0が入るのがいやだったもので) if aVal is not equal to 0 then set value to (aVal as string) end if end tell set iCount to iCount + 1 end repeat end tell end tell end repeat end tell end tell |
|
▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に
|
Posted in アプリケーション操作(app control), 10.5対応, 10.6対応, Numbers | No Comments »
Numbers上で選択した範囲を連続する数値で埋めるAppleScriptです。
Excelでものすごくよく使う「編集」>「フィル」>「連続データの作成…」をNumbers上に手っ取り早く実装してみました。Script Menuに入れて使ってみてください。

Numbersのシート上で範囲を指定して……

このAppleScriptを実行すると、開始値を入力するダイアログが表示されるので、好きな値を入力して[Return]キーを押すと……

連続する数値が入力されます。

ステップ値を変更したり、複数列を選択された場合への対処を行ったり、直前のセルの値を取得して初期値のデフォルト値にあてたりするなど、いろいろカスタマイズしてみると面白いと思われます。
| スクリプト名:Numbersで選択中の範囲を連続する数値で埋める |
set sNum to text returned of (display dialog “Input start Num“ default answer “”) tell application “Numbers“ tell document 1 tell sheet 1 tell table 1 set mySelectedRanges to value of every cell of selection range set cellList to cell of selection range set iCount to 0 set newList to {} repeat with i in mySelectedRanges set the end of newList to sNum + iCount set iCount to iCount + 1 end repeat repeat with i from 1 to (length of cellList) tell item i of cellList set value to item i of newList end tell end repeat end tell end tell end tell end tell
|
|
▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に
|
Posted in アプリケーション操作(app control), 10.5対応, 10.6対応, Numbers | No Comments »
Numbersで、選択範囲がどの範囲なのかという情報を取得します。
まっとうな方法ではなく「name」だというのが腑に落ちないのですが、いろいろ試してみたところ、「選択範囲がどこからどこまでなのか」を取得する方法はこれしかなさそうです。
| スクリプト名:Numbersで選択範囲の情報を取得する |
tell application “Numbers“ tell document 1 tell sheet 1 tell table 1 set mySelectedRanges to name of selection range end tell end tell end tell end tell –> “B2:C3″ |
|
▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に
|
Posted in アプリケーション操作(app control), 10.5対応, Numbers | No Comments »
Numbersで、指定の範囲(range)を選択します。
| スクリプト名:Numbersで指定範囲を選択 |
tell application "Numbers" tell document 1 tell sheet 1 tell table 1 set selection range to range "B4:C15" end tell end tell end tell end tell |
|
▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に
|
Posted in アプリケーション操作(app control), 10.5対応, Numbers | No Comments »
Numbersで、選択範囲の値(value)を取得します。

| スクリプト名:Nunbersで選択範囲の値を取得 |
tell application “Numbers“ tell document 1 tell sheet 1 tell table 1 set mySelectedRanges to value of every cell of selection range end tell end tell end tell end tell –> {1.0, 2.0, 3.0, 4.0} |
|
▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に
|
Posted in アプリケーション操作(app control), 10.5対応, Numbers | No Comments »
Numbersで選択範囲を取得するサンプルです。
結果は、
<<class>> “B2:C3″ of table “表 1″ of sheet “シート 1″ of document “名称未設定” of application “Numbers”
のように返ってきます。この「<<class>>」という返り値では、その後で処理できない(エラーになる)ので、バグでしょう。
| スクリプト名:Numbersで選択範囲を取得 |
tell application “Numbers“ tell document 1 tell sheet 1 tell table 1 set mySelectedRanges to selection range end tell end tell end tell end tell
|
|
▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に
|
Posted in アプリケーション操作(app control), 10.5対応, Numbers | No Comments »
Numbersの指定の表で昇順ソートを行うAppleScriptです。操作してみて分るのは、1行目だけは対象外だということで……そういう仕様のアプリケーションのようです。
(more…)
Posted in アプリケーション操作(app control), 10.5対応, Numbers | No Comments »
Numbersで、選択範囲内にある統合セルを元に戻します。選択範囲は、統合セルときっちり同じでなくても大丈夫です。
(more…)
Posted in アプリケーション操作(app control), 10.5対応, Numbers | No Comments »
Numbersで、選択中の範囲のセルを1つのセルに統合するAppleScriptです。「selection range」からデータを取得できないかと試していたものの、さっぱりダメで……結局、こういうセル結合のために作られたオブジェクトではないかと納得しているものです。
(more…)
Posted in アプリケーション操作(app control), 10.5対応, Numbers | No Comments »
Numbersで、特定のセルの値を取得します。セルの指定はA1形式で行うことになります。いろいろ試してみましたが、割と簡単にできて驚きました。逆に、rangeから値を取得する方法については、試してもいっこうに方法が見えてきません(バグかもしれない)。
(more…)
Posted in アプリケーション操作(app control), 10.5対応, Numbers | No Comments »
Numbersで、特定のシート内の表の特定行(row)のデータを取得します。
(more…)
Posted in アプリケーション操作(app control), 10.5対応, Numbers | No Comments »
Numbersで、特定のシート内の表からデータを取得します。Numbers上で表のサイズを自由に変更できますが、ここで得られるセルのデータ数は、変更した表のサイズに依存します。
(more…)
Posted in アプリケーション操作(app control), 10.5対応, Numbers | No Comments »
Numbersで、書類内のシートとテーブルにアクセスします。それぞれ、数をかぞえます。
(more…)
Posted in アプリケーション操作(app control), 10.5対応, Numbers | No Comments »
Numbersで、オープン中の書類の情報を取得します。pathはPOSIX pathが返ってきます。
(more…)
Posted in アプリケーション操作(app control), 10.5対応, Numbers | No Comments »
iWork 09同梱のNumbersがAppleScriptに対応したので、とりあえずひととおりサンプルを作ってみました。
(more…)
Posted in アプリケーション操作(app control), 10.5対応, Numbers | No Comments »