[Numbers]タブ区切りテキストで保存する
#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions
property refMe : a reference to current application
property refNSString : a reference to refMe's NSString
property refNSMutableString : a reference to refMe's NSMutableString
property refNSArray : a reference to refMe's NSArray
property refNSURL : a reference to refMe's NSURL
set objFileManager to refMe's NSFileManager's defaultManager()
####起動させる
tell application "Numbers"
launch
activate
end tell
####起動を待つ
repeat
tell application "Numbers"
activate
set boolActivate to frontmost
end tell
if boolActivate is false then
delay 1
else
exit repeat
end if
end repeat
tell application "Numbers"
set numCntWindow to (count of every window) as integer
end tell
if numCntWindow = 0 then
return "ファイルがありません"
end if
tell application "Numbers"
tell front window
set strFileName to name as text
end tell
tell front document
tell active sheet
set strSheetName to name as text
tell first table
set strTableName to name as text
set rangeTable to cell range
set selection range to rangeTable
set listValue to value of cells of rows
end tell
end tell
end tell
end tell
set ocidValueArray to refNSArray's alloc()'s initWithArray:listValue
set ocidTSVstrings to refNSMutableString's alloc()'s initWithCapacity:0
repeat with itemValueArray in ocidValueArray
####行の最後にタブを入れない用
set numCntArrayItem to count of itemValueArray
repeat with itemLineValueArray in itemValueArray
log itemLineValueArray
set ocidTSVstrings to (ocidTSVstrings's stringByAppendingString:itemLineValueArray)
####最後のアイテムは処理しなし
if numCntArrayItem > 1 then
####区切り文字の挿入
set ocidTSVstrings to (ocidTSVstrings's stringByAppendingString:"\t")
end if
####カウントダウン
set numCntArrayItem to numCntArrayItem - 1 as integer
end repeat
###行毎改行
set ocidTSVstrings to (ocidTSVstrings's stringByAppendingString:"\n")
end repeat
###################################
#####ダイアログ
###################################a
###ダイアログのデフォルト
set ocidUserDesktopPath to (objFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set aliasDefaultLocation to ocidUserDesktopPath as alias
set strPromptText to "名前を決めてください"
set strDefaultName to (strFileName & "." & strSheetName & "." & strTableName & ".tsv")
tell current application to activate
####実在しない『はず』なのでas «class furl»で
set aliasSaveFilePath to (choose file name default location aliasDefaultLocation default name strDefaultName with prompt strPromptText) as «class furl»
####UNIXパス
set strSaveFilePath to POSIX path of aliasSaveFilePath as text
####ドキュメントのパスをNSString
set ocidSaveFilePath to refNSString's stringWithString:strSaveFilePath
####ドキュメントのパスをNSURLに
set ocidSaveFilePathURL to refNSURL's fileURLWithPath:ocidSaveFilePath
###拡張子取得
set strFileExtensionName to ocidSaveFilePathURL's pathExtension() as text
###ダイアログで拡張子を取っちゃった時対策
if strFileExtensionName is not "tsv" then
set ocidSaveFilePathURL to ocidSaveFilePathURL's URLByAppendingPathExtension:"tsv"
set ocidSaveFilePath to ocidSaveFilePathURL's |path|()
end if
#####属性を指定しておく
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
set strUID to user ID of (system info) as text
###所有者ID
ocidAttrDict's setValue:strUID forKey:(refMe's NSFileOwnerAccountID)
###グループID
ocidAttrDict's setValue:80 forKey:(refMe's NSFileGroupOwnerAccountID)
####パーミッション 700
ocidAttrDict's setValue:448 forKey:(refMe's NSFilePosixPermissions)
###ファイル作成時のダミーテキスト
set ocidTempText to refNSString's stringWithString:""
###ファイルを作る
set boolMakeNewFile to (objFileManager's createFileAtPath:ocidSaveFilePath |contents|:ocidTempText attributes:ocidAttrDict)
####内容を書き込む UTF8で書き込む
set boolFileWrite to (ocidTSVstrings's writeToFile:ocidSaveFilePath atomically:false encoding:(refMe's NSUTF8StringEncoding) |error|:(reference))
return
| 固定リンク
「Numbers」カテゴリの記事
- [Numbers]ナンバーズで祭日入りカレンダー(2023.06.10)
- [Numbers]タブ区切りテキストで保存する(2023.02.09)
- [Numbers]セルの値を順番に取得する(2023.02.09)
- [Numbers]Window Document Sheetまで(途中)(2023.02.09)
「Text TSV」カテゴリの記事
- ユーザー辞書作成支援スクリプト(追加辞書用テキストも作成するようにした)(2024.10.13)
- TSV2HTML TSVタブ区切りテキストをHTMLテーブルにする(少し修正)(2024.03.28)
- TSV行列入替(2024.03.28)
- TSV2AdobeXML タブ区切りテキストからAdobe Illustratorの変数印字用の『変数ライブラリ』を生成する(1ページに複数面付け版)(2024.03.06)
- TSV2AdobeXML タブ区切りテキストからAdobe Illustratorの変数印字用の『変数ライブラリ』を生成する(2024.03.06)