Hi,
This is just to share some code that Christian Grunenberg helped me with. So not really a question.
In March this year Christian was kind enough to share the code below (Code 1.) for generating x number of random words from a source text in DevonThink Pro that I had written. The source text was usually about 500 words long, so in order to avoid repeats I was deleting the random words generated by the code in the original text. A time consuming process.
Code 2. is Christian’s code for both generating the random words and deleting them from the original source text.
Thank you again Christian.
Best regards,
Chris.
Code 1.
Here’s a simple example. Just select the documents and the script will
return n random words (see property pWords):
property pWords : 10
tell application “DEVONthink Pro”
set theRecords to the selection
set numRecords to count of theRecords
set num to random number from 1 to numRecords
set theText to plain text of (item num of theRecords)
set numWords to count of words of theText
set theResult to “”
repeat with i from 1 to pWords
if i > 1 then set theResult to theResult & " "
set theResult to theResult & (word (random number from 1 to numWords)
of theText)
end repeat
return theResult
end tell
Code 2.
The new code that deletes the random words from the original text:
property pWords : 25
tell application “DEVONthink Pro”
set theResult to “”
try
set theRecords to the selection
set numRecords to count of theRecords
set num to random number from 1 to numRecords
set theText to plain text of (item num of theRecords)
set numWords to count of words of theText
set theWords to {}
repeat with i from 1 to pWords
if i > 1 then set theResult to theResult & " "
set theWord to word (random number from 1 to numWords) of theText
set theResult to theResult & theWord
set theWords to theWords & theWord
end repeat
set theWindow to missing value
show progress indicator "Deleting Words" steps (count of theRecords)
repeat with theRecord in theRecords
set theText to plain text of theRecord
if theWindow is missing value then
set theWindow to open window for record theRecord
else
set record of theWindow to theRecord
end if
repeat with theWord in theWords
if theText contains theWord then tell text of theWindow to set (every word where it is theWord) to ""
end repeat
tell theWindow to save
step progress indicator (name of theRecord as string)
end repeat
hide progress indicator
tell theWindow to close
on error
hide progress indicator
return ""
end try
return theResult
end tell