com.apple.TextEncodingの値を再設定する
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # /usr/bin/xattr -p com.apple.TextEncoding /some/text/file.txt |
004 | (* |
005 | Non-lossy ASCII ;3071 |
006 | macintosh;0 |
007 | x-mac-japanese;1 |
008 | cp932;1056 |
009 | Shift_JIS;1576 |
010 | shift_jis;2561 |
011 | iso-2022-jp;2080 |
012 | iso-2022-jp-2;2081 |
013 | iso-2022-jp-1;2082 |
014 | euc-jp;2336 |
015 | windows-1252;1280 |
016 | windows-1250;1281 |
017 | cp852;1042 |
018 | |
019 | us-ascii;1536 |
020 | utf-7;67109120 |
021 | utf-8;134217984 |
022 | utf-16;256 |
023 | utf-16be;268435712 |
024 | utf-16le;335544576 |
025 | utf-32;201326848 |
026 | utf-32be;402653440 |
027 | utf-32le;469762304 |
028 | |
029 | iso-8859-10;522 |
030 | |
031 | big5-hkscs;2566 |
032 | x-mac-trad-chinese;2 |
033 | x-mac-korean;3 |
034 | x-mac-simp-chinese;25 |
035 | gb18030;1586 |
036 | *) |
037 | # ファイルの属性(アトリビュート)のテキストエンコードと |
038 | # 実際のファイルのテキストエンコードが不一致になった時用 |
039 | # com.cocolog-nifty.quicktimer.icefloe |
040 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
041 | use AppleScript version "2.6" |
042 | use framework "Foundation" |
043 | use framework "AppKit" |
044 | use scripting additions |
045 | |
046 | property refMe : a reference to current application |
047 | property listUTI : {"public.text"} as list |
048 | |
049 | ################################# |
050 | #【1】クリックでオープンした場合 |
051 | on run |
052 | #ダイアログ を前面に |
053 | set strName to (name of current application) as text |
054 | if strName is "osascript" then |
055 | tell application "Finder" to activate |
056 | else |
057 | tell current application to activate |
058 | end if |
059 | #デフォルトパス |
060 | set appFileManager to refMe's NSFileManager's defaultManager() |
061 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
062 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
063 | set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias |
064 | set strMes to ("ファイルを選んでください") as text |
065 | set strPrompt to ("ファイルを選んでください") as text |
066 | try |
067 | #ダイアログ |
068 | set listChooseAliasFilePath to (choose file strMes with prompt strPrompt default location (aliasDefaultLocation) of type listUTI with multiple selections allowed without invisibles and showing package contents) as list |
069 | on error |
070 | log "エラーしました" |
071 | return "エラーしました" |
072 | end try |
073 | #ファイルリストを次の処理に渡す |
074 | set boolDone to doPathSender(listChooseAliasFilePath) |
075 | #戻り値チェック |
076 | if boolDone is false then |
077 | display alert "エラーが発生しました" message "エラーが発生しました" |
078 | return "エラー終了run" |
079 | else |
080 | return "処理終了run" |
081 | end if |
082 | end run |
083 | |
084 | ################################# |
085 | #【2】ドロップした場合 |
086 | on open listDropAliasFilePath |
087 | #########【2−1】UTIチェック |
088 | #サブルーチンに渡すリスト |
089 | set listAliasFilePath to {} as list |
090 | #ドロップされたアイテムの数だけ繰り返す |
091 | repeat with itemDropAliasFilePath in listDropAliasFilePath |
092 | #エイリアス |
093 | set aliasItemFilePath to itemDropAliasFilePath as alias |
094 | tell application "Finder" |
095 | #Finder情報を取得して |
096 | set recordInfoFor to info for aliasItemFilePath |
097 | end tell |
098 | #UTIを取得 |
099 | set strItemUIT to (type identifier of recordInfoFor) as text |
100 | #UTIが対象ファイルならリストに追加 |
101 | repeat with itemUTI in listUTI |
102 | if strItemUIT is itemUTI then |
103 | copy aliasItemFilePath to end of listAliasFilePath |
104 | end if |
105 | end repeat |
106 | end repeat |
107 | #########【2−1】KINDチェック |
108 | set listSendAliasFilePath to {} as list |
109 | repeat with itemAliasFilePath in listAliasFilePath |
110 | #処理する判定 |
111 | set boolChkAliasPath to true as boolean |
112 | try |
113 | tell application "Finder" |
114 | set strKind to (kind of itemDropObject) as text |
115 | end tell |
116 | if strKind is "アプリケーション" then |
117 | log "アプリケーションは処理しない" |
118 | set boolChkAliasPath to false as boolean |
119 | else if strKind is "ボリューム" then |
120 | log "ボリュームは処理しない" |
121 | set boolChkAliasPath to false as boolean |
122 | else if strKind is "エイリアス" then |
123 | log "エイリアスは処理しない" |
124 | set boolChkAliasPath to false as boolean |
125 | else if strKind is "フォルダ" then |
126 | log "フォルダは処理しない" |
127 | set boolChkAliasPath to false as boolean |
128 | end if |
129 | on error |
130 | log "シンボリックリンク等kindを取得できないファイルは処理しない" |
131 | set boolChkAliasPath to false as boolean |
132 | end try |
133 | if boolChkAliasPath is true then |
134 | copy itemAliasFilePath to end of listSendAliasFilePath |
135 | end if |
136 | end repeat |
137 | #########【2−3】次工程に渡す |
138 | set boolDone to doPathSender(listSendAliasFilePath) |
139 | if boolDone is not true then |
140 | return false |
141 | else |
142 | return "処理終了open" |
143 | end if |
144 | end open |
145 | |
146 | ################################# |
147 | #【3】1と2からファイルパスのエイリアスを受け取り |
148 | #処理に順番に渡す |
149 | to doPathSender(argListAliasFilePath) |
150 | set appFileManager to refMe's NSFileManager's defaultManager() |
151 | #1回でいい処理はここに記述する |
152 | set recordTextEnc to {|日本語(Mac OS Roman)|:"macintosh;0", |日本語(Mac OS)|:"x-mac-japanese;1", |日本語(Windows, DOS)|:"cp932;1056", |日本語(Shift JIS X0213)|:"Shift_JIS;1576", |日本語(Shift JIS)|:"shift_jis;2561", |日本語(ISO 2022-JP)|:"iso-2022-jp;2080", |日本語(ISO 2022-JP-2)|:"iso-2022-jp-2;2081", |日本語(ISO 2022-JP-1)|:"iso-2022-jp-1;2082", |日本語(EUC)|:"euc-jp;2336", |Unicode (UTF-8)|:"utf-8;134217984", |Unicode (UTF-16)|:"utf-16;256", |Unicode (UTF-16BE)|:"utf-16be;268435712", |Unicode (UTF-16LE)|:"utf-16le;335544576", |Unicode (UTF-32)|:"utf-32;201326848", |Unicode (UTF-32BE)|:"utf-32be;402653440", |Unicode (UTF-32LE)|:"utf-32le;469762304"} as record |
153 | #DICTにしておく |
154 | set ocidTextEncDictJp to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
155 | ocidTextEncDictJp's setDictionary:(recordTextEnc) |
156 | set ocidAllKeys to ocidTextEncDictJp's allKeys() |
157 | #ソート |
158 | set ocidSortedArray to ocidAllKeys's sortedArrayUsingSelector:("localizedStandardCompare:") |
159 | #ダイアログ用のリスト |
160 | set listAllKeys to ocidSortedArray as list |
161 | #ダイアログ |
162 | tell current application |
163 | set strName to name as text |
164 | end tell |
165 | if strName is "osascript" then |
166 | tell application "Finder" to activate |
167 | else |
168 | tell current application to activate |
169 | end if |
170 | try |
171 | set listResponse to (choose from list listAllKeys with title "選んでください" with prompt "選んでください" default items (item 1 of listAllKeys) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) as list |
172 | on error |
173 | log "エラーしました" |
174 | return "エラーしました" |
175 | end try |
176 | if (item 1 of listResponse) is false then |
177 | return "キャンセルしました" |
178 | end if |
179 | ###ダイアログの戻り値 |
180 | set strComplianceName to (item 1 of listResponse) as text |
181 | # |
182 | set strErrorCode to (ocidTextEncDictJp's valueForKey:(strComplianceName)) as text |
183 | |
184 | ####1ファイルパス毎本処理に渡す |
185 | repeat with itemAliasFilePath in argListAliasFilePath |
186 | set aliasFilePath to itemAliasFilePath as alias |
187 | set strItemFilePath to (POSIX path of aliasFilePath) as text |
188 | ##まずは今設定されている属性を削除して |
189 | set strCommandText to ("/bin/zsh -c '/usr/bin/xattr -d \"com.apple.TextEncoding\" \"" & strItemFilePath & "\"'") as text |
190 | log strCommandText |
191 | try |
192 | do shell script strCommandText |
193 | end try |
194 | ##再設定する |
195 | set strCommandText to ("/bin/zsh -c '/usr/bin/xattr -w \"com.apple.TextEncoding\" \"" & strErrorCode & "\" \"" & strItemFilePath & "\"'") as text |
196 | log strCommandText |
197 | #本処理に渡す |
198 | set boolDone to doAction(strCommandText) |
199 | #戻り値がエラーだったら? |
200 | if boolDone is false then |
201 | tell application "Finder" |
202 | set strFileName to (name of itemAliasFilePath) as text |
203 | end tell |
204 | set strMes to (strFileName & "でエラーになりました") as text |
205 | display alert "エラーが発生しました" message strMes |
206 | return strMes |
207 | #エラーになったところで止める |
208 | end if |
209 | end repeat |
210 | return true |
211 | end doPathSender |
212 | |
213 | ################################# |
214 | #【4】実際の処理は全てここ |
215 | to doAction(argCommandText) |
216 | ### |
217 | try |
218 | do shell script argCommandText |
219 | on error |
220 | #エラーになったらfalseを戻す |
221 | return false |
222 | end try |
223 | |
224 | return true |
225 | end doAction |
AppleScriptで生成しました |
| 固定リンク