| 001 | #!/usr/bin/env osascript |
| 002 | #coding: utf-8 |
| 003 | ----+----1----+----2----+----3----+----4----+----5----+----6----+----7 |
| 004 | (* |
| 005 | Jedit |
| 006 | 選択中テキストを項番再採番して |
| 007 | 戻す |
| 008 | 項番は、ゼロサプレスする |
| 009 |
|
| 010 | 重要 ヘッダーやフッター部分を選択しない |
| 011 | 採番される範囲を選択するように作ってあります |
| 012 |
|
| 013 |
|
| 014 | v1初回作成 |
| 015 | v1.1 全角 半角対応 項番ゼロサプレス対応 |
| 016 | v1.2 Jedit Open 用に変更した版 |
| 017 |
|
| 018 | com.cocolog-nifty.quicktimer.icefloe *) |
| 019 | ----+----1----+----2----+----3----+----4----+----5----+----6----+----7 |
| 020 | use AppleScript version "2.8" |
| 021 | use framework "Foundation" |
| 022 | use framework "AppKit" |
| 023 | use scripting additions |
| 024 | property refMe : a reference to current application |
| 025 |
|
| 026 | ######################## |
| 027 | # |
| 028 | tell application "Jedit" |
| 029 | set numCntWindow to (count of every window) as integer |
| 030 | end tell |
| 031 | if numCntWindow = 0 then |
| 032 | display alert "Windowがありません" as informational giving up after 2 |
| 033 | return false |
| 034 | end if |
| 035 | tell application "Jedit" |
| 036 | set numCntDoc to (count of every document) as integer |
| 037 | end tell |
| 038 | if numCntDoc = 0 then |
| 039 | display alert "開いている書類がありません" as informational giving up after 2 |
| 040 | return false |
| 041 | end if |
| 042 |
|
| 043 | ######################## |
| 044 | #テキスト取得 |
| 045 | #前面ドキュメントのテキストを取得 |
| 046 | tell application "Jedit" |
| 047 | tell front document |
| 048 | properties |
| 049 | set strOrgText to (its selection) as string |
| 050 | set numLocation to (its selection location) as integer |
| 051 | set numLength to (its selection length) as integer |
| 052 | tell its selection |
| 053 | set strPsFontName to (its font) as string |
| 054 | set numFontSize to (its size) as integer |
| 055 | end tell |
| 056 | set listCharColor to (its charcolor) as list |
| 057 | end tell |
| 058 | end tell |
| 059 |
|
| 060 |
|
| 061 | if strOrgText is "" then |
| 062 | tell application "SystemUIServer" |
| 063 | activate |
| 064 | display alert "採番する部分を本文選択してください" |
| 065 | end tell |
| 066 | return false |
| 067 | |
| 068 | else if numLength = 0 then |
| 069 | tell application "SystemUIServer" |
| 070 | activate |
| 071 | display alert "採番する部分を本文選択してください" |
| 072 | end tell |
| 073 | return false |
| 074 | |
| 075 | end if |
| 076 |
|
| 077 | if strOrgText does not contain tab then |
| 078 | tell application "SystemUIServer" |
| 079 | activate |
| 080 | display alert "タブ区切りテキスト専用です" |
| 081 | end tell |
| 082 | return false |
| 083 | end if |
| 084 |
|
| 085 |
|
| 086 |
|
| 087 | #################################### |
| 088 | #改行コード判定 簡易版 |
| 089 | set ocidOrgText to refMe's NSString's stringWithString:(strOrgText) |
| 090 | #改行文字の数を数えて |
| 091 | set ocidCRLFArray to ocidOrgText's componentsSeparatedByString:(return & linefeed) |
| 092 | set ocidCRArray to ocidOrgText's componentsSeparatedByString:(return) |
| 093 | set ocidLFArray to ocidOrgText's componentsSeparatedByString:(linefeed) |
| 094 | set numCntCRLFArray to ocidCRLFArray's |count|() as integer |
| 095 | set numCntCRArray to ocidCRArray's |count|() as integer |
| 096 | set numCntLFArray to ocidLFArray's |count|() as integer |
| 097 | #改行文字の数で判定する |
| 098 | if (numCntCRArray = 1) and (numCntCRArray = 1) and (numCntLFArray = 1) then |
| 099 | #改行無しなので SingleArrayに |
| 100 | set strNewlineChar to ("") as text |
| 101 | |
| 102 | else if numCntCRArray = 1 then |
| 103 | #改行 UNIX |
| 104 | set strNewlineChar to (linefeed) as text |
| 105 | |
| 106 | else if ocidCRArray = ocidLFArray then |
| 107 | #改行 Win |
| 108 | set strNewlineChar to (return & linefeed) as text |
| 109 | |
| 110 | else if numCntLFArray = 1 then |
| 111 | #改行 Mac |
| 112 | set strNewlineChar to (return) as text |
| 113 | |
| 114 | end if |
| 115 | #################################### |
| 116 | #最後が改行終わりだと空の項目が出来るので除去 |
| 117 | set boolHasSuffix to ocidOrgText's hasSuffix:(strNewlineChar) |
| 118 | if boolHasSuffix is true then |
| 119 | set ocidLength to ocidOrgText's |length|() |
| 120 | set ocidRange to refMe's NSRange's NSMakeRange(0, ocidLength - 1) |
| 121 | set ocidIndex to ocidRange's |length|() |
| 122 | set ocidOrgText to ocidOrgText's substringToIndex:(ocidIndex) |
| 123 | log ocidOrgText as text |
| 124 | end if |
| 125 | #################################### |
| 126 | #改行毎でArrayにする |
| 127 | set ocidTempArray to ocidOrgText's componentsSeparatedByString:(strNewlineChar) |
| 128 |
|
| 129 | #################################### |
| 130 | #空の項目を削除 |
| 131 | set appPredicate to refMe's NSPredicate's predicateWithFormat:("self.length > 0") |
| 132 | set ocidOrgTextArray to ocidTempArray's filteredArrayUsingPredicate:(appPredicate) |
| 133 |
|
| 134 | #Array=行数 |
| 135 | set numCntLine to ocidOrgTextArray's |count|() |
| 136 | #################################### |
| 137 | #ゼロパディングの桁数 |
| 138 | if numCntLine < 100 then |
| 139 | set ocidZeroPdd to refMe's NSMutableString's stringWithString:("00") |
| 140 | else if numCntLine < 1000 then |
| 141 | set ocidZeroPdd to refMe's NSMutableString's stringWithString:("000") |
| 142 | else |
| 143 | set ocidZeroPdd to refMe's NSMutableString's stringWithString:("0000") |
| 144 | end if |
| 145 | #出力用のテキスト |
| 146 | set ocidOutString to refMe's NSMutableString's alloc()'s init() |
| 147 |
|
| 148 | repeat with itemLineNo from 0 to (numCntLine - 1) by 1 |
| 149 | #################################### |
| 150 | #行番号 |
| 151 | set numSetLineNo to (itemLineNo + 1) as integer |
| 152 | set ocidZeroAdd to (ocidZeroPdd's stringByAppendingString:(numSetLineNo as text)) |
| 153 | set ocidZeroAddLength to ocidZeroAdd's |length|() |
| 154 | if numCntLine < 100 then |
| 155 | set ocidIndex to (ocidZeroAddLength - 2) |
| 156 | set ocidZeroPaddString to (ocidZeroAdd's substringFromIndex:(ocidIndex)) |
| 157 | else if numCntLine < 1000 then |
| 158 | set ocidIndex to (ocidZeroAddLength - 3) |
| 159 | set ocidZeroPaddString to (ocidZeroAdd's substringFromIndex:(ocidIndex)) |
| 160 | end if |
| 161 | #################################### |
| 162 | # |
| 163 | set ocidLineText to (ocidOrgTextArray's objectAtIndex:(itemLineNo)) |
| 164 | set ocidLineArray to (ocidLineText's componentsSeparatedByString:(tab)) |
| 165 | set numCntLineArray to ocidLineArray's |count|() |
| 166 | |
| 167 | repeat with numTabNO from 0 to (numCntLineArray - 1) by 1 |
| 168 | set ocidTabText to (ocidLineArray's objectAtIndex:(numTabNO)) |
| 169 | if numTabNO = 0 then |
| 170 | #空 |
| 171 | if (ocidTabText as text) is "" then |
| 172 | # (ocidOutString's appendString:("")) |
| 173 | (ocidOutString's appendString:(ocidZeroPaddString)) |
| 174 | else |
| 175 | #最初の項目を半角にして |
| 176 | set ocidHalfWidth to (ocidTabText's stringByApplyingTransform:(refMe's NSStringTransformFullwidthToHalfwidth) |reverse|:(false)) |
| 177 | #元の半角にする前の値と同じなら 半角 |
| 178 | set boolEqual to (ocidTabText's isEqualToString:(ocidHalfWidth)) |
| 179 | #違うなら項番が全角数字だったわけなので |
| 180 | if boolEqual is false then |
| 181 | #数字が全角だったわけなので 項番を全角にして |
| 182 | set ocidFullWidth to (ocidZeroPaddString's stringByApplyingTransform:(refMe's NSStringTransformFullwidthToHalfwidth) |reverse|:(true)) |
| 183 | #戻す |
| 184 | (ocidOutString's appendString:(ocidFullWidth)) |
| 185 | else if boolEqual is true then |
| 186 | #同じ値だったなら半角なので そのまま戻す |
| 187 | (ocidOutString's appendString:(ocidZeroPaddString)) |
| 188 | end if |
| 189 | end if |
| 190 | (ocidOutString's appendString:(tab)) |
| 191 | else if numTabNO < (numCntLineArray - 1) then |
| 192 | (ocidOutString's appendString:(ocidTabText)) |
| 193 | (ocidOutString's appendString:(tab)) |
| 194 | else if numTabNO = (numCntLineArray - 1) then |
| 195 | (ocidOutString's appendString:(ocidTabText)) |
| 196 | end if |
| 197 | end repeat |
| 198 | (ocidOutString's appendString:(strNewlineChar)) |
| 199 | end repeat |
| 200 | set strText to ocidOutString as text |
| 201 |
|
| 202 | ######################## |
| 203 | # |
| 204 | tell application "Jedit" |
| 205 | tell front document |
| 206 | set its selection to strText |
| 207 | end tell |
| 208 | end tell |
| 209 |
|
| 210 | ######################## |
| 211 | # |
| 212 |
|
| 213 | set strMsg to ("置換しました" & return & "この内容でよろしいですか") as text |
| 214 | set strOKButton to ("OK確定") as text |
| 215 | set strCancelButton to ("キャンセル元に戻す") as text |
| 216 | set strSelectButton to ("新規ドキュメントに置換") as text |
| 217 | set listButton to {strOKButton, strCancelButton, strSelectButton} as list |
| 218 | tell application "SystemUIServer" |
| 219 | activate |
| 220 | try |
| 221 | set recordResponse to (display alert strMsg buttons listButton default button strOKButton cancel button strCancelButton as critical giving up after 360) as record |
| 222 | on error strErrMsg number numErrNo |
| 223 | log strErrMsg & numErrNo |
| 224 | set strSerchText to (strText) as text |
| 225 | set strReplaceText to (strOrgText) as text |
| 226 | tell application "Jedit" |
| 227 | tell front document |
| 228 | replace for strSerchText by strReplaceText without case sensitive, replacing all and using regular expression |
| 229 | end tell |
| 230 | end tell |
| 231 | return |
| 232 | end try |
| 233 | end tell |
| 234 | if (gave up of recordResponse) is true then |
| 235 | set strSerchText to (strText) as text |
| 236 | set strReplaceText to (strOrgText) as text |
| 237 | tell application "Jedit" |
| 238 | tell front document |
| 239 | replace for strSerchText by strReplaceText without case sensitive, replacing all and using regular expression |
| 240 | end tell |
| 241 | end tell |
| 242 | log "時間切れですやりなおしてください" |
| 243 | return false |
| 244 | else if strOKButton is equal to (button returned of recordResponse) then |
| 245 | set strResponse to (button returned of recordResponse) as text |
| 246 | else if strSelectButton is equal to (button returned of recordResponse) then |
| 247 | |
| 248 | set strSerchText to (strText) as text |
| 249 | set strReplaceText to (strOrgText) as text |
| 250 | tell application "Jedit" |
| 251 | tell front document |
| 252 | replace for strSerchText by strReplaceText without case sensitive, replacing all and using regular expression |
| 253 | end tell |
| 254 | end tell |
| 255 | |
| 256 | tell application "Jedit" |
| 257 | set refNewDoc to make new document with properties {rich text:strText} |
| 258 | end tell |
| 259 | |
| 260 | #文字数数えて |
| 261 | set numCntChar to (count of every character of strText) as integer |
| 262 | |
| 263 | tell application "Jedit" |
| 264 | tell refNewDoc |
| 265 | tell characters 1 thru numCntChar |
| 266 | properties |
| 267 | set (its font) to strPsFontName |
| 268 | set (its size) to numFontSize |
| 269 | set (its color) to listCharColor |
| 270 | end tell |
| 271 | |
| 272 | (* |
| 273 | repeat with itemNO from numCntChar to 1 by -1 |
| 274 | tell character itemNO |
| 275 | set (its color) to listCharColor |
| 276 | end tell |
| 277 | end repeat |
| 278 | *) |
| 279 | |
| 280 | |
| 281 | end tell |
| 282 | end tell |
| 283 | end if |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 | return |
| 288 | ######################## |
| 289 | #新規ドキュメントを作ってテキストを戻す(使わなかった) |
| 290 | tell application "Jedit" |
| 291 | if boolNewDoc is true then |
| 292 | make new document with properties {rich text:strText} |
| 293 | else |
| 294 | tell front document |
| 295 | set its selection to strText |
| 296 | end tell |
| 297 | end if |
| 298 | |
| 299 | end tell |