I am making an attempt to jot down an AppleScript that works inside CotEditor to insert numbers inside strains of textual content, specifying the place so as to add it, from what quantity, the increment, prefixes/suffixes and selecting so as to add main zeros, nevertheless it would not work.
-- Handler so as to add main zeros to a quantity
on addLeadingZeros(numberText, maxDigits)
set numLength to size of numberText
set zerosToAdd to maxDigits - numLength
if zerosToAdd > 0 then
set leadingZeros to ""
repeat zerosToAdd instances
set leadingZeros to leadingZeros & "0"
finish repeat
return leadingZeros & numberText
else
return numberText
finish if
finish addLeadingZeros
strive
inform utility "CotEditor"
if exists entrance doc then
set frontDoc to entrance doc
set docText to contents of frontDoc
set lineList to paragraphs of docText
set totalLines to rely of lineList
-- Immediate the person to enter the beginning line, finish line, column place, beginning quantity, and step increment
set startLineDialog to show dialog "Begin on line:" default reply "1"
if button returned of startLineDialog is "Cancel" then return
set startLine to textual content returned of startLineDialog
set endLineDialog to show dialog "Finish on line:" default reply totalLines as wealthy textual content
if button returned of endLineDialog is "Cancel" then return
set endLine to textual content returned of endLineDialog
set columnPositionDialog to show dialog "Column place:" default reply "1"
if button returned of columnPositionDialog is "Cancel" then return
set columnPosition to textual content returned of columnPositionDialog
set startingNumberDialog to show dialog "Beginning quantity:" default reply "1"
if button returned of startingNumberDialog is "Cancel" then return
set startingNumber to textual content returned of startingNumberDialog
set stepIncrementDialog to show dialog "Step increment:" default reply "1"
if button returned of stepIncrementDialog is "Cancel" then return
set stepIncrement to textual content returned of stepIncrementDialog
strive
set {startLine, endLine, columnPosition, startingNumber, stepIncrement} to {startLine as integer, endLine as integer, columnPosition as integer, startingNumber as integer, stepIncrement as integer}
on error
show dialog "Please enter legitimate enter values." buttons {"OK"} default button "OK"
return
finish strive
-- Immediate the person for a prefix
set prefixDialog to show dialog "Enter the prefix for line numbers:" default reply ""
if button returned of prefixDialog is "Cancel" then return
set prefixText to textual content returned of prefixDialog
-- Immediate the person for a suffix
set suffixDialog to show dialog "Enter the suffix for line numbers:" default reply ""
if button returned of suffixDialog is "Cancel" then return
set suffixText to textual content returned of suffixDialog
-- Ask if the person desires main zeros
set leadingZerosDialog to show dialog "Would you like main zeros for line numbers?" buttons {"Sure", "No"} default button "No"
set leadingZeros to button returned of leadingZerosDialog
set newContent to ""
set currentLineNumber to startingNumber as integer
repeat with i from 1 to totalLines
log "Processing line: " & i -- Log message to trace progress
if i ≥ startLine and that i ≤ endLine then
log "Including line quantity to line: " & i
-- Calculate the suitable prefix based mostly on the road quantity
set lineNumberText to currentLineNumber as wealthy textual content
log "Line quantity textual content earlier than main zeros: " & lineNumberText
if leadingZeros is "Sure" then
set lineNumberText to my addLeadingZeros(lineNumberText, 5) -- assuming a most of 5 digits for the road quantity
log "Line quantity textual content after main zeros: " & lineNumberText
finish if
set linePrefix to prefixText & lineNumberText & suffixText
log "Line prefix: " & linePrefix
-- Insert line quantity at specified place
set lineContent to merchandise i of lineList
log "Unique line content material: " & lineContent
if class of lineContent is wealthy textual content and lineContent is just not "" then
-- Insert line quantity at specified column place
set linePrefixLength to size of linePrefix
if columnPosition > linePrefixLength then -- Verify if column place is after the prefix
set newLineContent to (wealthy textual content 1 through (columnPosition - linePrefixLength) of lineContent) & linePrefix & (wealthy textual content (columnPosition - linePrefixLength + 1) through -1 of lineContent)
else
set newLineContent to linePrefix & lineContent
finish if
else
-- If the road is empty or not textual content, simply insert the road quantity
set newLineContent to linePrefix
finish if
log "New line content material: " & newLineContent
-- Replace the road content material within the record
set merchandise i of lineList to newLineContent
set currentLineNumber to currentLineNumber + stepIncrement
finish if
finish repeat
-- Reassemble the doc's contents
set newContent to ""
repeat with i from 1 to totalLines
set newContent to newContent & merchandise i of lineList & return
finish repeat
set contents of frontDoc to newContent
else
show dialog "No doc is at present open."
finish if
finish inform
finish strive