property myText : "6000[F]This computer is fast.
1[F]This computer is fast.
6000[F]This computer is fast.
3[F]This computer is fast.
6000[F]This computer is fast."
property divider : "[F]"
property alphabet : "abcdefghijklmnopqrstuvwxyz"
property databasePath : "/tmp/Identifiers.sqlite"
property sqlite3Path : "/usr/bin/sqlite3"
property columnSeparator : character id 3
property rowSeparator : character id 2
on run
set myParagraphs to paragraphs in myText
SQLExecute(databasePath, "
create table if not exists Identifiers( Identifier integer primary key, Counter );
delete from Identifiers;")
repeat with myParagraphRef in myParagraphs
set sides to TextToList of (contents of myParagraphRef) between divider
if number of sides is 2 then
set {myIdentifier, myComment} to sides
SQLExecute(databasePath, "
insert or replace into Identifiers select " & myIdentifier & ", coalesce((select Counter + 1 from Identifiers where Identifier = " & myIdentifier & "), 0);")
SQLSelect(databasePath, "select Counter from Identifiers where Identifier = " & myIdentifier)
set myCounter to item 1 in item 1 in result
if myCounter > 0 then
set myAlpha to character myCounter in the alphabet
set newParagraph to myIdentifier & myAlpha & divider & myComment
set contents of myParagraphRef to newParagraph
end if
end if
end repeat
return ListToText of myParagraphs between return
end run
-- Handlers:
on TextToList of containerText between delimiter
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to delimiter
set textItems to text items of containerText
set AppleScript's text item delimiters to oldDelims
return textItems
end TextToList
on ListToText of theList between delimiter
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to delimiter
set joinedText to theList as text
set AppleScript's text item delimiters to oldDelims
return joinedText
end ListToText
on SQLExecute(databasePath, sqlCommand)
set shellCommand to "echo " & (quoted form of sqlCommand) & " | " & sqlite3Path & space & quoted form of databasePath
set resultText to do shell script shellCommand
return resultText
end SQLExecute
on SQLSelect(databasePath, sqlCommand)
set sqlCommandWithSeparators to "select *, '" & rowSeparator & "' from (" & sqlCommand & ");"
set shellCommand to "echo " & (quoted form of sqlCommandWithSeparators) & " | " & sqlite3Path & space & "-separator " & (quoted form of columnSeparator) & space & quoted form of databasePath
set recordsText to do shell script shellCommand
script speedyObject
property recordList : {}
end script
if recordsText is not "" then
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to (columnSeparator & rowSeparator & return)
set speedyObject's recordList to text items in recordsText
set last item in speedyObject's recordList to text 1 thru -(1 + (length of (columnSeparator & rowSeparator))) in last item in speedyObject's recordList
set AppleScript's text item delimiters to columnSeparator
set recordCount to (count speedyObject's recordList)
repeat with recordN from 1 to recordCount
set item recordN in speedyObject's recordList to text items in item recordN in speedyObject's recordList
end repeat
set AppleScript's text item delimiters to oldDelimiters
end if
return speedyObject's recordList
end SQLSelect