ユーザー辞書作成支援スクリプト(追加辞書用テキストも作成するようにした)
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | # com.cocolog-nifty.quicktimer.icefloe |
005 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
006 | ##自分環境がos12なので2.8にしているだけです |
007 | use AppleScript version "2.8" |
008 | use framework "Foundation" |
009 | use framework "AppKit" |
010 | use scripting additions |
011 | |
012 | property refMe : a reference to current application |
013 | |
014 | ##################### |
015 | #設定項目 |
016 | # 名詞 動詞 等 品詞名をお好みで |
017 | set strLangLind to ("普通名詞") as text |
018 | |
019 | ##順番に4回処理 |
020 | set strSetPath to ("Data/ローマ数字1-100.tsv") as text |
021 | #set strSetPath to ("Data/カタカナ濁音.tsv") as text |
022 | #set strSetPath to ("Data/カタカナ半濁.tsv") as text |
023 | #set strSetPath to ("Data/ひらがな濁音.tsv") as text |
024 | #set strSetPath to ("Data/ひらがな半濁.tsv") as text |
025 | #set strSetPath to ("Data/まる英字.tsv") as text |
026 | |
027 | ##################### |
028 | #PATH TO ME |
029 | set aliasPathToMe to (path to me) as alias |
030 | set strPathToMe to (POSIX path of aliasPathToMe) as text |
031 | set ocidPathToMeStr to refMe's NSString's stringWithString:(strPathToMe) |
032 | set ocidPathToMe to ocidPathToMeStr's stringByStandardizingPath() |
033 | set ocidPathToMeURL to refMe's NSURL's fileURLWithPath:(ocidPathToMe) |
034 | #読み込みファイル |
035 | set ocidContainerDirPathURL to ocidPathToMeURL's URLByDeletingLastPathComponent() |
036 | set ocidReadFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:(strSetPath) isDirectory:(false) |
037 | #保存パス PLIST保存先 |
038 | set ocidBaseFilePathURL to ocidReadFilePathURL's URLByDeletingPathExtension() |
039 | set ocidFileName to ocidBaseFilePathURL's lastPathComponent() |
040 | set ocidSavePlistDirPathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:("ユーザー辞書登録用PLIST") isDirectory:(true) |
041 | #追加辞書用テキスト保存先 |
042 | set ocidSaveTextDirPathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:("追加辞書用テキスト") isDirectory:(true) |
043 | #フォルダを作って |
044 | set appFileManager to refMe's NSFileManager's defaultManager() |
045 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
046 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
047 | set listDone to appFileManager's createDirectoryAtURL:(ocidSavePlistDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference) |
048 | set listDone to appFileManager's createDirectoryAtURL:(ocidSaveTextDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference) |
049 | #保存用のパスにしておく |
050 | set ocidSaveBasePlistFilePathURL to ocidSavePlistDirPathURL's URLByAppendingPathComponent:(ocidFileName) isDirectory:(false) |
051 | set ocidSaveBaseTextFilePathURL to ocidSaveTextDirPathURL's URLByAppendingPathComponent:(ocidFileName) isDirectory:(false) |
052 | set ocidSavePlistPathURL to ocidSaveBasePlistFilePathURL's URLByAppendingPathExtension:("plist") |
053 | set ocidSaveTextPathURL to ocidSaveBaseTextFilePathURL's URLByAppendingPathExtension:("csv.txt") |
054 | |
055 | ##################### |
056 | #NSDATA |
057 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
058 | set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidReadFilePathURL) options:(ocidOption) |error| :(reference) |
059 | if (item 2 of listResponse) = (missing value) then |
060 | set ocidReadData to (item 1 of listResponse) |
061 | else if (item 2 of listResponse) ≠ (missing value) then |
062 | set strErrorNO to (item 2 of listResponse)'s code() as text |
063 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
064 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
065 | return "エラーしました" & strErrorNO & strErrorMes |
066 | end if |
067 | ##################### |
068 | #NSSTRING |
069 | set ocidReadString to refMe's NSString's alloc()'s initWithData:(ocidReadData) encoding:(refMe's NSUTF8StringEncoding) |
070 | |
071 | ##################### |
072 | #NSMutableArray's |
073 | set ocidPlistArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
074 | |
075 | ##################### |
076 | #NSMutableString |
077 | set ocidCsvString to refMe's NSMutableString's alloc()'s initWithCapacity:(0) |
078 | |
079 | ##################### |
080 | #NSARRAY |
081 | set ocidStringArray to ocidReadString's componentsSeparatedByString:("n") |
082 | #行テキストを順番に処理 |
083 | repeat with itemLineText in ocidStringArray |
084 | #行テキスト=タブ区切りなのでタブで前後にわける |
085 | set ocidTabArray to (itemLineText's componentsSeparatedByString:("t")) |
086 | ##################### |
087 | #NSMutableDictionary |
088 | set ocidPlistDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:(0)) |
089 | #前が入力値 |
090 | (ocidPlistDict's setValue:(ocidTabArray's firstObject()) forKey:("shortcut")) |
091 | #後が変換値 |
092 | (ocidPlistDict's setValue:(ocidTabArray's lastObject()) forKey:("phrase")) |
093 | #出来たDICTをARRYにセット |
094 | (ocidPlistArray's addObject:(ocidPlistDict)) |
095 | ##################### |
096 | #NSMutableString |
097 | (ocidCsvString's appendString:(ocidTabArray's firstObject())) |
098 | (ocidCsvString's appendString:(",")) |
099 | (ocidCsvString's appendString:(ocidTabArray's lastObject())) |
100 | (ocidCsvString's appendString:(",")) |
101 | (ocidCsvString's appendString:(strLangLind)) |
102 | (ocidCsvString's appendString:("n")) |
103 | |
104 | end repeat |
105 | |
106 | ##################### |
107 | #NSPropertyListSerialization |
108 | set ocidFormat to (refMe's NSPropertyListBinaryFormat_v1_0) |
109 | set listResponse to refMe's NSPropertyListSerialization's dataWithPropertyList:(ocidPlistArray) format:(ocidFormat) options:0 |error| :(reference) |
110 | if (item 2 of listResponse) = (missing value) then |
111 | set ocidPlistData to (item 1 of listResponse) |
112 | else if (item 2 of listResponse) ≠ (missing value) then |
113 | set strErrorNO to (item 2 of listResponse)'s code() as text |
114 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
115 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
116 | return "エラーしました" & strErrorNO & strErrorMes |
117 | end if |
118 | |
119 | |
120 | ########################################## |
121 | ####【4】保存 ここは上書き PLIST |
122 | set ocidOption to (refMe's NSDataWritingAtomic) |
123 | set listDone to ocidPlistData's writeToURL:(ocidSavePlistPathURL) options:(ocidOption) |error| :(reference) |
124 | if (item 1 of listDone) is true then |
125 | log "正常処理 PLIST" |
126 | else if (item 2 of listDone) ≠ (missing value) then |
127 | set strErrorNO to (item 2 of listDone)'s code() as text |
128 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
129 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
130 | return "エラーしました" & strErrorNO & strErrorMes |
131 | end if |
132 | |
133 | ########################################## |
134 | ####【4】保存 ここは上書き TEXT |
135 | set ocidOption to (refMe's NSUTF8StringEncoding) |
136 | set listDone to ocidCsvString's writeToURL:(ocidSaveTextPathURL) atomically:(true) encoding:(ocidOption) |error| :(reference) |
137 | if (item 1 of listDone) is true then |
138 | return "正常処理 TEXT" |
139 | else if (item 2 of listDone) ≠ (missing value) then |
140 | set strErrorNO to (item 2 of listDone)'s code() as text |
141 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
142 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
143 | return "エラーしました" & strErrorNO & strErrorMes |
144 | end if |
145 | |
146 | |
147 | |
AppleScriptで生成しました |
| 固定リンク
「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)