Numbers上の選択中のテーブルの内容をMarkdown形式のデータに書き出して、書き出したデータをMacDownでオープンしてHTMLのソース文字列を取得するAppleScriptです。
Numbersの表の内容をHTMLに書き出すのに、2D Arrayの内容を直接HTML Tableに変換するScriptが手元に見当たらなかったので、いったんMarkdown書類を経由させてみました。
MarkdownのレンダラーのFrameworkもいくつかチェックはしていますが、なぜかどれもこれもTableタグをサポートしていません(ーー;;
AppleScript名:Numbersの選択範囲を取得してMarkdown書式の表に変換する v2 |
— Created 2016-05-12 by Takaaki Naganoya — Modified 2018-03-29 by Takaaki Naganoya — 2018 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" property NSUUID : a reference to current application’s NSUUID property NSString : a reference to current application’s NSString property NSFileManager : a reference to current application’s NSFileManager set htmlRes to selectedNumbersTableToHTML() of me –選択中のNumbersのワークシート上で選択中のテーブルのデータをHTML化 on selectedNumbersTableToHTML() set aSeparator to "|" set aCell to ":—" –Align Left Tag set aLineTerminator to return –Numbersの選択範囲からExcel仕様で2Dリストを返す set aSel to get2DListFromNumbersSelection() of me if aSel = "" then tell current application display dialog "Numbersのワークシート上で何も選択されていません" with icon 2 buttons {"OK"} default button 1 with title "選択範囲エラー" return false end tell end if –選択範囲の横幅チェック、ヘッダー部のデータ作成 set colNum to length of (contents of first item of aSel) set sepList to {} repeat colNum times set end of sepList to aCell end repeat –ヘッダーセパレータを配列の2番目に挿入する set newList to insListItem(aSel, sepList, 2) of me –Markdown書式の表を文字列で作成する set outStr to "" repeat with i in newList set outStr to outStr & retStrFromArrayWithDelimiter(contents of i, aSeparator) of me & aLineTerminator end repeat –Desktopにmarkdown書類を書き出してMacDownでオープンしてHTMLソースを取得する set targFol to POSIX path of (path to desktop) set fRes to savePlainText(targFol, outStr, "md") of me tell application "MacDown" open ((POSIX file fRes) as alias) tell front document set aProp to properties set aHtmlDat to html of aProp close end tell end tell –デスクトップフォルダ上に作成したMarkdown書類を削除する set aRes to deleteItemAt(fRes) of me return aHtmlDat end selectedNumbersTableToHTML –指定のリスト(2次元配列)に、要素(1次元配列)を、指定箇所(1はじまり)にインサートする on insListItem(aList as list, insList as list, insertItemNum as integer) set newList to {} set itemCounter to 1 repeat with i in aList if itemCounter = insertItemNum then set the end of newList to insList end if set end of newList to (contents of i) set itemCounter to itemCounter + 1 end repeat return newList end insListItem –リストを指定デリミタをはさんでテキスト化 on retStrFromArrayWithDelimiter(aList as list, aDelim as string) set anArray to current application’s NSArray’s arrayWithArray:aList set aRes to anArray’s componentsJoinedByString:aDelim return aRes as text end retStrFromArrayWithDelimiter on retArrowText(aList as list, aDelim as string) –自分のASでよく使うハンドラ名称なので、同じものを用意 return my retStrFromArrayWithDelimiter(aList, aDelim) end retArrowText –テキストを指定デリミタでリスト化 on parseByDelim(aData, aDelim) set aText to current application’s NSString’s stringWithString:aData set aList to aText’s componentsSeparatedByString:aDelim return aList as list end parseByDelim on get2DListFromNumbersSelection() –Numbersで選択範囲を縦に区切ったリストを返す tell application "Numbers" tell front document tell active sheet set theTable to first table whose class of selection range is range tell theTable try set selList to value of every cell of selection range –選択範囲のデータを取得 on error return "" –何も選択されてなかった場合 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 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 get2DListFromNumbersSelection –プレーンテキストを指定フォルダ(POSIX path)にUTF-8で書き出し on savePlainText(targFol, aString, aExt) set aString to current application’s NSString’s stringWithString:aString set theName to (NSUUID’s UUID()’s UUIDString()) set thePath to NSString’s stringWithString:targFol set thePath to (thePath’s stringByAppendingPathComponent:theName)’s stringByAppendingPathExtension:aExt set aRes to aString’s writeToFile:thePath atomically:false encoding:(current application’s NSUTF8StringEncoding) |error|:(missing value) if aRes as boolean = true then return thePath as string else return false end if end savePlainText –指定のPOSIX pathのファイルを削除 on deleteItemAt(aPosixPath) set theNSFileManager to NSFileManager’s defaultManager() set theResult to theNSFileManager’s removeItemAtPath:(aPosixPath) |error|:(missing value) return (theResult as integer = 1) as boolean end deleteItemAt |
More from my site
(Visited 150 times, 1 visits today)