20260430

[Jedit]項番を再度採番しなおす(タブ区切り用)AppleScripts

[Jedit]項番を再度採番しなおす(タブ区切り)

ダウンロードはこちらから
NOTE記事一覧ですnote.com

【スクリプトエディタで開く】 |

[Jedit Open]項番を再度採番しなおす(タブ区切り).applescript.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+----3----+----4----+----5----+----6----+----7
004(*
005Jedit
006選択中テキストを項番再採番して
007戻す
008項番は、ゼロサプレスする
009
010重要 ヘッダーやフッター部分を選択しない
011採番される範囲を選択するように作ってあります
012
013
014v1初回作成
015v1.1 全角 半角対応 項番ゼロサプレス対応
016v1.2 Jedit Open 用に変更した版
017
018com.cocolog-nifty.quicktimer.icefloe *)
019----+----1----+----2----+----3----+----4----+----5----+----6----+----7
020use AppleScript version "2.8"
021use framework "Foundation"
022use framework "AppKit"
023use scripting additions
024property refMe : a reference to current application
025
026########################
027#
028tell application "Jedit"
029   set numCntWindow to (count of every window) as integer
030end tell
031if numCntWindow = 0 then
032   display alert "Windowがありません" as informational giving up after 2
033   return false
034end if
035tell application "Jedit"
036   set numCntDoc to (count of every document) as integer
037end tell
038if numCntDoc = 0 then
039   display alert "開いている書類がありません" as informational giving up after 2
040   return false
041end if
042
043########################
044#テキスト取得
045#前面ドキュメントのテキストを取得
046tell 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
058end tell
059
060
061if strOrgText is "" then
062   tell application "SystemUIServer"
063      activate
064      display alert "採番する部分を本文選択してください"
065   end tell
066   return false
067   
068else if numLength = 0 then
069   tell application "SystemUIServer"
070      activate
071      display alert "採番する部分を本文選択してください"
072   end tell
073   return false
074   
075end if
076
077if strOrgText does not contain tab then
078   tell application "SystemUIServer"
079      activate
080      display alert "タブ区切りテキスト専用です"
081   end tell
082   return false
083end if
084
085
086
087####################################
088#改行コード判定 簡易版
089set ocidOrgText to refMe's NSString's stringWithString:(strOrgText)
090#改行文字の数を数えて
091set ocidCRLFArray to ocidOrgText's componentsSeparatedByString:(return & linefeed)
092set ocidCRArray to ocidOrgText's componentsSeparatedByString:(return)
093set ocidLFArray to ocidOrgText's componentsSeparatedByString:(linefeed)
094set numCntCRLFArray to ocidCRLFArray's |count|() as integer
095set numCntCRArray to ocidCRArray's |count|() as integer
096set numCntLFArray to ocidLFArray's |count|() as integer
097#改行文字の数で判定する
098if (numCntCRArray = 1) and (numCntCRArray = 1) and (numCntLFArray = 1) then
099   #改行無しなので SingleArrayに
100   set strNewlineChar to ("") as text
101   
102else if numCntCRArray = 1 then
103   #改行 UNIX
104   set strNewlineChar to (linefeed) as text
105   
106else if ocidCRArray = ocidLFArray then
107   #改行 Win
108   set strNewlineChar to (return & linefeed) as text
109   
110else if numCntLFArray = 1 then
111   #改行 Mac
112   set strNewlineChar to (return) as text
113   
114end if
115####################################
116#最後が改行終わりだと空の項目が出来るので除去
117set boolHasSuffix to ocidOrgText's hasSuffix:(strNewlineChar)
118if 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
124end if
125####################################
126#改行毎でArrayにする
127set ocidTempArray to ocidOrgText's componentsSeparatedByString:(strNewlineChar)
128
129####################################
130#空の項目を削除
131set appPredicate to refMe's NSPredicate's predicateWithFormat:("self.length > 0")
132set ocidOrgTextArray to ocidTempArray's filteredArrayUsingPredicate:(appPredicate)
133
134#Array=行数
135set numCntLine to ocidOrgTextArray's |count|()
136####################################
137#ゼロパディングの桁数
138if numCntLine < 100 then
139   set ocidZeroPdd to refMe's NSMutableString's stringWithString:("00")
140else if numCntLine < 1000 then
141   set ocidZeroPdd to refMe's NSMutableString's stringWithString:("000")
142else
143   set ocidZeroPdd to refMe's NSMutableString's stringWithString:("0000")
144end if
145#出力用のテキスト
146set ocidOutString to refMe's NSMutableString's alloc()'s init()
147
148repeat 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))
199end repeat
200set strText to ocidOutString as text
201
202########################
203#
204tell application "Jedit"
205   tell front document
206      set its selection to strText
207   end tell
208end tell
209
210########################
211#
212
213set strMsg to ("置換しました" & return & "この内容でよろしいですか") as text
214set strOKButton to ("OK確定") as text
215set strCancelButton to ("キャンセル元に戻す") as text
216set strSelectButton to ("新規ドキュメントに置換") as text
217set listButton to {strOKButton, strCancelButton, strSelectButton} as list
218tell 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
233end tell
234if (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
244else if strOKButton is equal to (button returned of recordResponse) then
245   set strResponse to (button returned of recordResponse) as text
246else 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
283end if
284
285
286
287return
288########################
289#新規ドキュメントを作ってテキストを戻す(使わなかった)
290tell 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   
299end tell
AppleScriptで生成しました