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 | |
---|
020 | ##################### |
---|
021 | #処理開始 |
---|
022 | set appFileManager to refMe's NSFileManager's defaultManager() |
---|
023 | |
---|
024 | set aliasPathToMe to (path to me) as alias |
---|
025 | set strPathToMe to (POSIX path of aliasPathToMe) as text |
---|
026 | set ocidPathToMeStr to refMe's NSString's stringWithString:(strPathToMe) |
---|
027 | set ocidPathToMe to ocidPathToMeStr's stringByStandardizingPath() |
---|
028 | set ocidPathToMeURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidPathToMe) isDirectory:(false) |
---|
029 | set ocidContainerDirPathURL to ocidPathToMeURL's URLByDeletingLastPathComponent() |
---|
030 | #保存先 |
---|
031 | set ocidSavePlistDirPathURL to (ocidContainerDirPathURL's URLByAppendingPathComponent:("ユーザー辞書登録用PLIST") isDirectory:(true)) |
---|
032 | #追加辞書用テキスト保存先 |
---|
033 | set ocidSaveTextDirPathURL to (ocidContainerDirPathURL's URLByAppendingPathComponent:("追加辞書用テキスト") isDirectory:(true)) |
---|
034 | #フォルダを作って |
---|
035 | set appFileManager to refMe's NSFileManager's defaultManager() |
---|
036 | set ocidAttrDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0) |
---|
037 | (ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)) |
---|
038 | set listDone to (appFileManager's createDirectoryAtURL:(ocidSavePlistDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference)) |
---|
039 | set listDone to (appFileManager's createDirectoryAtURL:(ocidSaveTextDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference)) |
---|
040 | #TSVのあるディレクトリ |
---|
041 | set ocidDataDirPathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:("Data") isDirectory:(true) |
---|
042 | #収集 |
---|
043 | set ocidOption to (refMe's NSDirectoryEnumerationSkipsHiddenFiles) |
---|
044 | set ocidKeyArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
---|
045 | ocidKeyArray's addObject:(refMe's NSURLPathKey) |
---|
046 | ocidKeyArray's addObject:(refMe's NSURLIsDirectoryKey) |
---|
047 | set listResponse to (appFileManager's contentsOfDirectoryAtURL:(ocidDataDirPathURL) includingPropertiesForKeys:(ocidKeyArray) options:(ocidOption) |error| :(reference)) |
---|
048 | set ocidSubPathURLArray to (item 1 of listResponse) |
---|
049 | set numCntArray to ocidSubPathURLArray's |count|() |
---|
050 | |
---|
051 | #順番に処理 |
---|
052 | repeat with itemNo from 0 to (numCntArray - 1) by 1 |
---|
053 | set ocidItemPathURL to (ocidSubPathURLArray's objectAtIndex:(itemNo)) |
---|
054 | |
---|
055 | #保存パス PLIST保存先 |
---|
056 | set ocidBaseFilePathURL to ocidItemPathURL's URLByDeletingPathExtension() |
---|
057 | set ocidFileName to ocidBaseFilePathURL's lastPathComponent() |
---|
058 | |
---|
059 | #保存用のパスにしておく |
---|
060 | set ocidSaveBasePlistFilePathURL to (ocidSavePlistDirPathURL's URLByAppendingPathComponent:(ocidFileName) isDirectory:(false)) |
---|
061 | set ocidSaveBaseTextFilePathURL to (ocidSaveTextDirPathURL's URLByAppendingPathComponent:(ocidFileName) isDirectory:(false)) |
---|
062 | set ocidSavePlistPathURL to (ocidSaveBasePlistFilePathURL's URLByAppendingPathExtension:("plist")) |
---|
063 | set ocidSaveTextPathURL to (ocidSaveBaseTextFilePathURL's URLByAppendingPathExtension:("csv.txt")) |
---|
064 | |
---|
065 | ##################### |
---|
066 | #NSDATA |
---|
067 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
---|
068 | set listResponse to (refMe's NSData's alloc()'s initWithContentsOfURL:(ocidItemPathURL) options:(ocidOption) |error| :(reference)) |
---|
069 | if (item 2 of listResponse) = (missing value) then |
---|
070 | set ocidReadData to (item 1 of listResponse) |
---|
071 | else if (item 2 of listResponse) ≠ (missing value) then |
---|
072 | set strErrorNO to (item 2 of listResponse)'s code() as text |
---|
073 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
---|
074 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
---|
075 | return "エラーしました" & strErrorNO & strErrorMes |
---|
076 | end if |
---|
077 | ##################### |
---|
078 | #NSSTRING |
---|
079 | set ocidReadString to (refMe's NSString's alloc()'s initWithData:(ocidReadData) encoding:(refMe's NSUTF8StringEncoding)) |
---|
080 | |
---|
081 | ##################### |
---|
082 | #NSMutableArray's |
---|
083 | set ocidPlistArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:(0)) |
---|
084 | |
---|
085 | ##################### |
---|
086 | #NSMutableString |
---|
087 | set ocidCsvString to (refMe's NSMutableString's alloc()'s initWithCapacity:(0)) |
---|
088 | |
---|
089 | ##################### |
---|
090 | #NSARRAY |
---|
091 | set ocidStringArray to (ocidReadString's componentsSeparatedByString:("\n")) |
---|
092 | ##################### |
---|
093 | #NSPredicateで空の項目を削除 |
---|
094 | set appPredicate to (refMe's NSPredicate's predicateWithFormat:"SELF != ''") |
---|
095 | set ocidPredicateArray to (ocidStringArray's filteredArrayUsingPredicate:(appPredicate)) |
---|
096 | |
---|
097 | #行テキストを順番に処理 |
---|
098 | repeat with itemLineText in ocidPredicateArray |
---|
099 | #行テキスト=タブ区切りなのでタブで前後にわける |
---|
100 | set ocidTabArray to (itemLineText's componentsSeparatedByString:("\t")) |
---|
101 | ##################### |
---|
102 | #NSMutableDictionary |
---|
103 | set ocidPlistDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:(0)) |
---|
104 | #前が入力値 |
---|
105 | (ocidPlistDict's setValue:(ocidTabArray's firstObject()) forKey:("shortcut")) |
---|
106 | #後が変換値 |
---|
107 | (ocidPlistDict's setValue:(ocidTabArray's lastObject()) forKey:("phrase")) |
---|
108 | #出来たDICTをARRYにセット |
---|
109 | (ocidPlistArray's addObject:(ocidPlistDict)) |
---|
110 | ##################### |
---|
111 | #NSMutableString |
---|
112 | (ocidCsvString's appendString:(ocidTabArray's firstObject())) |
---|
113 | (ocidCsvString's appendString:(",")) |
---|
114 | (ocidCsvString's appendString:(ocidTabArray's lastObject())) |
---|
115 | (ocidCsvString's appendString:(",")) |
---|
116 | (ocidCsvString's appendString:(strLangLind)) |
---|
117 | (ocidCsvString's appendString:("\n")) |
---|
118 | |
---|
119 | end repeat |
---|
120 | |
---|
121 | ##################### |
---|
122 | #NSPropertyListSerialization |
---|
123 | set ocidFormat to (refMe's NSPropertyListBinaryFormat_v1_0) |
---|
124 | set listResponse to (refMe's NSPropertyListSerialization's dataWithPropertyList:(ocidPlistArray) format:(ocidFormat) options:0 |error| :(reference)) |
---|
125 | if (item 2 of listResponse) = (missing value) then |
---|
126 | set ocidPlistData to (item 1 of listResponse) |
---|
127 | else if (item 2 of listResponse) ≠ (missing value) then |
---|
128 | set strErrorNO to (item 2 of listResponse)'s code() as text |
---|
129 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
---|
130 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
---|
131 | return "エラーしました" & strErrorNO & strErrorMes |
---|
132 | end if |
---|
133 | |
---|
134 | |
---|
135 | ########################################## |
---|
136 | ####【4】保存 ここは上書き PLIST |
---|
137 | set ocidOption to (refMe's NSDataWritingAtomic) |
---|
138 | set listDone to (ocidPlistData's writeToURL:(ocidSavePlistPathURL) options:(ocidOption) |error| :(reference)) |
---|
139 | if (item 1 of listDone) is true then |
---|
140 | log "正常処理 PLIST" |
---|
141 | else if (item 2 of listDone) ≠ (missing value) then |
---|
142 | set strErrorNO to (item 2 of listDone)'s code() as text |
---|
143 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
---|
144 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
---|
145 | return "エラーしました" & strErrorNO & strErrorMes |
---|
146 | end if |
---|
147 | |
---|
148 | ########################################## |
---|
149 | ####【4】保存 ここは上書き TEXT |
---|
150 | set ocidOption to (refMe's NSUTF8StringEncoding) |
---|
151 | set listDone to (ocidCsvString's writeToURL:(ocidSaveTextPathURL) atomically:(true) encoding:(ocidOption) |error| :(reference)) |
---|
152 | if (item 1 of listDone) is true then |
---|
153 | log "正常処理 TEXT" |
---|
154 | else if (item 2 of listDone) ≠ (missing value) then |
---|
155 | set strErrorNO to (item 2 of listDone)'s code() as text |
---|
156 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
---|
157 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
---|
158 | return "エラーしました" & strErrorNO & strErrorMes |
---|
159 | end if |
---|
160 | |
---|
161 | end repeat |
---|
162 | |
---|
163 | |
---|
164 | |
---|