TSVタブ区切りのテキストをMDファイルの表にする
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #! /usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # com.cocolog-nifty.quicktimer.icefloe |
004 | # |
005 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
006 | use AppleScript version "2.8" |
007 | use framework "Foundation" |
008 | use framework "AppKit" |
009 | use scripting additions |
010 | property refMe : a reference to current application |
011 | |
012 | ################# |
013 | # 【1】入力ファイル |
014 | #ダイアログ |
015 | tell current application |
016 | set strName to name as text |
017 | end tell |
018 | #スクリプトメニューから実行したら |
019 | if strName is "osascript" then |
020 | tell application "Finder" to activate |
021 | else |
022 | tell current application to activate |
023 | end if |
024 | #デフォルトロケーション |
025 | set appFileManager to refMe's NSFileManager's defaultManager() |
026 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
027 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
028 | set aliasDesktopDirPath to (ocidDesktopDirPathURL's absoluteURL()) as alias |
029 | # |
030 | # TSV専用の場合 |
031 | #set listUTI to {"public.tab-separated-values-text"} |
032 | # テキストでもOKな場合 |
033 | set listUTI to {"public.tab-separated-values-text", "public.plain-text"} |
034 | set strMes to ("TSVファイルを選んでください") as text |
035 | set strPrompt to ("TSVファイルを選んでください") as text |
036 | try |
037 | ### ファイル選択時 |
038 | set aliasFilePath to (choose file strMes with prompt strPrompt default location aliasDesktopDirPath of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias |
039 | on error |
040 | log "エラーしました" |
041 | return "エラーしました" |
042 | end try |
043 | if aliasFilePath is (missing value) then |
044 | return "選んでください" |
045 | end if |
046 | #入力パス |
047 | set strFilePath to (POSIX path of aliasFilePath) as text |
048 | set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath)) |
049 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
050 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false) |
051 | #保存するタブ区切りテキストのパス |
052 | set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension() |
053 | set ocidFileName to ocidBaseFilePathURL's lastPathComponent() |
054 | set ocidSaveFilePathURL to (ocidBaseFilePathURL's URLByAppendingPathExtension:("md")) |
055 | ################# |
056 | ### 【2】 ファイルのテキストを読み込み |
057 | set listResponse to (refMe's NSString's alloc()'s initWithContentsOfURL:(ocidFilePathURL) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference)) |
058 | if (item 2 of listResponse) = (missing value) then |
059 | set ocidReadString to (item 1 of listResponse) |
060 | else if (item 2 of listResponse) ≠ (missing value) then |
061 | set strErrorNO to (item 2 of listResponse)'s code() as text |
062 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
063 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
064 | return "エラーしました" & strErrorNO & strErrorMes |
065 | end if |
066 | #可変にして |
067 | set ocidLineString to (refMe's NSMutableString's alloc()'s initWithCapacity:(0)) |
068 | (ocidLineString's setString:(ocidReadString)) |
069 | #改行をUNIXに強制 |
070 | set ocidLineStringLF to (ocidLineString's stringByReplacingOccurrencesOfString:("\r\n") withString:("\n")) |
071 | set ocidLineString to (ocidLineStringLF's stringByReplacingOccurrencesOfString:("\r") withString:("\n")) |
072 | #改行毎でリストにする |
073 | set ocidCharSet to (refMe's NSCharacterSet's newlineCharacterSet) |
074 | set ocidLineArray to (ocidLineString's componentsSeparatedByCharactersInSet:(ocidCharSet)) |
075 | set nunCntArray to (ocidLineArray's |count|()) as integer |
076 | ################# |
077 | #【3】データをMDに整形 |
078 | #出力(MD)用のテキスト |
079 | set ocidSaveString to (refMe's NSMutableString's alloc()'s initWithCapacity:(0)) |
080 | #表題を#で付与 |
081 | (ocidSaveString's appendString:("# 表のタイトル表のタイトル表のタイトル")) |
082 | (ocidSaveString's appendString:("\n\n")) |
083 | #行の数だけ繰り返し |
084 | repeat with itemNo from 0 to (nunCntArray - 1) by 1 |
085 | #行テキストを取り出して |
086 | set ocidLineText to (ocidLineArray's objectAtIndex:(itemNo)) |
087 | #タブでリスト化する |
088 | set ocidLineTextArray to (ocidLineText's componentsSeparatedByString:("\t")) |
089 | #リスト化された行テキストを順番に |
090 | repeat with itemLineText in ocidLineTextArray |
091 | (ocidSaveString's appendString:("|")) |
092 | (ocidSaveString's appendString:(itemLineText)) |
093 | end repeat |
094 | (ocidSaveString's appendString:("|")) |
095 | (ocidSaveString's appendString:("\n")) |
096 | #文字寄せ方向を付与(全部中央揃) |
097 | # 出力結果をデータの種類によって:を取れば良い |
098 | if itemNo = 0 then |
099 | set numCntLineTextArray to (ocidLineTextArray's |count|()) as integer |
100 | repeat numCntLineTextArray times |
101 | (ocidSaveString's appendString:("|")) |
102 | (ocidSaveString's appendString:(":---:")) |
103 | end repeat |
104 | (ocidSaveString's appendString:("|")) |
105 | (ocidSaveString's appendString:("\n")) |
106 | end if |
107 | end repeat |
108 | |
109 | |
110 | ################# |
111 | #保存 |
112 | set listDone to ocidSaveString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference) |
113 | if (item 2 of listDone) ≠ (missing value) then |
114 | set strErrorNO to (item 2 of listDone)'s code() as text |
115 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
116 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
117 | return "エラーしました" & strErrorNO & strErrorMes |
118 | end if |
119 | |
120 | |
AppleScriptで生成しました |
| 固定リンク