Hello,
I’m very new to applescript. I would like to either create or find a script that will allow me to do one of two things:
Either #1: delete “nth” word (a user defined number) in a given section of text (ie, deletes every 5th word in a paragraph);
Or #2: delete any word that starts with a given user-defined character (ie, any word in a paragraph that starts with “-” or “#”)
Let say I have the following sentences:
What is Critical Thinking? For some, the idea of Critical Thinking centers on the ability to use certain cognitive means to sort through some sort of information, to evaluate it, and to use the evaluated information to reach a conclusion or make a decision (Lawson 184).
If in example #1 I wanted to delete every 5th word, the script would return:
What is Critical Thinking? some, the idea of Thinking centers on the to use certain cognitive to sort through some of information, to evaluate, and to use the information to reach a or make a decision (184).
OR
Let say I have the following sentences:
What is @Critical Thinking? For some, the idea of @Critical Thinking centers on the ability to use @certain@cognitive means to sort through some sort of information, to evaluate it, and to use the evaluated information to reach a conclusion or make a decision (Lawson 184).
If in example #2 I wanted to delete every word that begins with “@”, the script would return:
What is Thinking? For some, the idea of Thinking centers on the ability to use means to sort through some sort of information, to evaluate it, and to use the evaluated information to reach a conclusion or make a decision (Lawson 184).
This would return the right words, but without punctuation for option #1:
-- Getting text and nth
set theString to ¬
"What is Critical Thinking? For some, the idea of Critical Thinking centers on the ability to use certain cognitive means to sort through some sort of information, to evaluate it, and to use the evaluated information to reach a conclusion or make a decision (Lawson 184)"
set nth to 5
-- Make list of words
set allWords to every word of theString
-- Loop and make new string
set newString to {}
set firstObjectInt to 1
set lastObjectInt to nth - 1
repeat while lastObjectInt < (count allWords)
repeat with i from firstObjectInt to lastObjectInt
set end of newString to (item i of allWords) as string
set end of newString to " "
end repeat
set firstObjectInt to firstObjectInt + nth
set lastObjectInt to lastObjectInt + nth
end repeat
set newString to newString as string
If you want to keep the punctuation, it’s going to be a lot harder for option #1, but possible for option #2:
global i
-- Getting text and character
set theString to ¬
"What is @Critical Thinking? For some, the idea of @Critical Thinking centers on the ability to use @certain @cognitive means to sort through some sort of information, to evaluate it, and to use the evaluated information to reach a conclusion or make a decision (Lawson 184)."
set theExcludeCharacter to "@"
-- loop and make new string
set newString to {}
set i to 1
repeat while (count every character of theString) is greater than or equal to i
log i
if character i of theString is not theExcludeCharacter then
set end of newString to character i of theString
else
repeat until character i of theString is " "
set i to i + 1
end repeat
end if
set i to i + 1
end repeat
set newString to newString as string
Thank You! Each of these does what I would need them to do. However, quick question:
Is there a way to get the script to ask for text? For example, Let’s say I have a 3 page paper and I want to delete every 5th word. How do I run script #1 to affect that 3 page paper? Or, do I need to manually cut and paste the text into the given area of the script?
Ideally, I would love to be able to run this script through Automator by using this workflow in Automator:
“Ask for Text” (TextEdit) then “Run AppleScript” (or skip the “Ask for Text” step if the script asks for the text) then “New Text File” (TextEdit).
Sorry for being so clueless. But, your help is really helping me to prepare for 3 comprehensive exams I have next week! So, thank you VERY much!
set myText to "What is Critical Thinking? For some, the idea of Critical Thinking centers on the ability to use certain cognitive means to sort through some sort of information, to evaluate it, and to use the evaluated information to reach a conclusion or make a decision (Lawson 184). "
RemoveNthWord(myText, 5)
-- "What is Critical Thinking? some, the idea of Thinking centers on the to use certain cognitive to sort through some of information, to evaluate ,and to use the information to reach a or make a decision (184)."
on RemoveNthWord(givenText, N)
set tid to AppleScript's text item delimiters
set resultString to ""
set AppleScript's text item delimiters to {" "}
set givenTextItems to (get text items of givenText)
set givenWords to (get words of givenText)
ignoring punctuation
repeat with i from 1 to count of givenWords
set testWord to item i of givenWords
repeat with oneItem in (get givenTextItems)
set givenTextItems to rest of givenTextItems
if (oneItem contains testWord) then
if i mod N = 0 then
set AppleScript's text item delimiters to {testWord}
set temp to text items of oneItem
set AppleScript's text item delimiters to {""}
set resultString to resultString & (temp as string)
else
set resultString to resultString & oneItem & " "
end if
exit repeat
else
set resultString to resultString & oneItem
end if
end repeat
end repeat
end ignoring
set AppleScript's text item delimiters to tid
return text 1 thru -2 of resultString
end RemoveNthWord
-- Getting text and nth
set theString to ¬
"What is @Critical Thinking? For some, the idea of @Critical Thinking centers on the ability to use @certain cognitive means to sort through some sort of information, to evaluate it, and to use the evaluated information to reach a conclusion or make a decision (Lawson 184)"
set tChar to "@" -- Could be more than 1
set OList to {}
set tStart to 1
set tstring to theString
-- Find Offsets of tChar
repeat -- Don't know how many
set tstring to text tStart thru -1 of theString
set OS to offset of tChar in tstring
if OS ≠0 then
set end of OList to OS + tStart
set tStart to (item -1 of OList)
else
exit repeat -- None left if the offset is 0
end if
end repeat
-- Make new string
set Leader to (item 1 of OList) - 2
set newString to (text 1 thru Leader of theString)
set tStart to Leader
repeat with Part in (items 2 thru -1 of OList)
set Piece to text (tStart + 2) thru (Part - 3) in theString
set O to offset of " " in Piece
set newString to newString & text (O + 1) thru -1 of Piece & space
set tStart to Part
end repeat
-- Get the end
set Tail to text tStart thru -1 of theString
set O to offset of space in Tail
set newString to newString & text (O + 1) thru -1 of Tail
--> "What is Thinking? For some, the idea of Thinking centers on the ability to use cognitive means to sort through some sort of information, to evaluate it, and to use the evaluated information to reach a conclusion or make a decision (Lawson 184)"
Save this as an application and drop you files to it or just open the application:
on open theObjects
if (count theObjects) < 2 then
set theExcludeCharacter to text returned of (display dialog "Please give the user-defined character:" default answer "@")
set fileToOpen to item 1 of theObjects
set OA to open for access fileToOpen
set theData to read OA
close access OA
set newString to GetEditedString(theData, theExcludeCharacter)
ReturnEditedString(newString)
else
display dialog "Please drop only one file" buttons "OK" default button 1
end if
end open
on run
set whatToDo to button returned of (display dialog "What do you want to do?" buttons {"Cancel", "Insert Text", "Open File"} cancel button 1 default button 3)
if whatToDo is "Insert Text" then
set textToEdit to text returned of (display dialog "Paste or insert the text to edit:" default answer "")
set theExcludeCharacter to text returned of (display dialog "Please give the user-defined character:" default answer "@")
set newString to GetEditedString(textToEdit, theExcludeCharacter)
log newString
ReturnEditedString(newString)
else if whatToDo is "Open File" then
set fileToOpen to (choose file with prompt "Choose the file to open")
set OA to open for access fileToOpen
set theData to read OA
close access OA
set theExcludeCharacter to text returned of (display dialog "Please give the user-defined character:" default answer "@")
set newString to GetEditedString(theData, theExcludeCharacter)
ReturnEditedString(newString)
end if
end run
on GetEditedString(theString, theExcludeCharacter)
try
-- loop and make new string
set newString to {}
set i to 1
repeat while (count every character of theString) is greater than or equal to i
log i
if character i of theString is not theExcludeCharacter then
set end of newString to character i of theString
else
repeat until character i of theString is " "
set i to i + 1
end repeat
end if
set i to i + 1
end repeat
end try
set newString to newString as string
log newString
return newString
end GetEditedString
on ReturnEditedString(theString)
log theString
repeat
try
set theChoice to choose from list {"Save in file", "Display Dialog", "Exit"} with prompt "What do you want to do with the result?"
set theChoice to theChoice as string
if theChoice is "Save in file" then
set theNewFileName to text returned of (display dialog "Give a name for the new file:" default answer "")
if theNewFileName does not end with ".txt" then set theNewFileName to (theNewFileName & ".txt") as string
set theNewFileFolder to (choose folder "Where do you want to save the file?") as alias
try
tell application "Finder" to make new file at theNewFileFolder with properties {name:theNewFileName}
tell application "Finder" to set theNewFile to (file theNewFileName of folder theNewFileFolder) as alias
set OA to open for access theNewFile with write permission
write theString to OA starting at 0
close access OA
display dialog "The data has been writen" buttons "OK" default button 1
on error theMsg
display dialog "An error occured, the script will try again." & return & return & theMsg
end try
else if theChoice is "Display Dialog" then
display dialog theString buttons "OK" default button 1
else
exit repeat
end if
end try
end repeat
end ReturnEditedString
DUDE! You’re amazing. This is almost perfect! Is there any way to make this script work with the remove the “nth” word option rather than the remove “@” option? For example:
Instead of “Please give the user-defined character:” default answer “@”)
It returns “Please give the user-defined variable:” default answer “5”)
Then, have the script do everything else you’ve got it doing. If so, it’d be exactly what I’m needing!
Another quick way to drop words. “Strings of tParts” will ignore the inserted “missing values” items.
set theString to ¬
"What is Critical Thinking? For some, the idea of Critical Thinking centers on the ability to use certain cognitive means to sort through some sort of information, to evaluate it, and to use the evaluated information to reach a conclusion or make a decision (Lawson 184)"
set n to 4
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to " " -- space
set tParts to text items of theString
repeat with k from 1 to count tParts
if k mod n = 0 then set item k of tParts to missing value
end repeat
set New to (strings of tParts) as string -- Reassemble around spaces
set AppleScript's text item delimiters to tid
New --> "What is Critical For some, the of Critical Thinking on the ability use certain cognitive to sort through sort of information, evaluate it, and use the evaluated to reach a or make a (Lawson 184)"
Here is the final script (save as application to work with dropped items)
on open theObjects
if (count theObjects) < 2 then
set nth to text returned of (display dialog "Give the nth" default answer "5")
set fileToOpen to item 1 of theObjects
set OA to open for access fileToOpen
set theData to read OA
close access OA
set newString to RemoveNthWord(theData, nth)
ReturnEditedString(newString)
else
display dialog "Please drop only one file" buttons "OK" default button 1
end if
end open
on run
set whatToDo to button returned of (display dialog "What do you want to do?" buttons {"Cancel", "Insert Text", "Open File"} cancel button 1 default button 3)
if whatToDo is "Insert Text" then
set textToEdit to text returned of (display dialog "Paste or insert the text to edit:" default answer "")
set nth to text returned of (display dialog "Give the nth" default answer "5")
set newString to RemoveNthWord(textToEdit, nth)
log newString
ReturnEditedString(newString)
else if whatToDo is "Open File" then
set fileToOpen to (choose file with prompt "Choose the file to open")
set OA to open for access fileToOpen
set theData to read OA
close access OA
set nth to text returned of (display dialog "Give the nth" default answer "5")
set newString to RemoveNthWord(theData, nth)
ReturnEditedString(newString)
end if
end run
on RemoveNthWord(givenText, N)
set tid to AppleScript's text item delimiters
set resultString to ""
set AppleScript's text item delimiters to {" "}
set givenTextItems to (get text items of givenText)
set givenWords to (get words of givenText)
ignoring punctuation
repeat with i from 1 to count of givenWords
set testWord to item i of givenWords
repeat with oneItem in (get givenTextItems)
set givenTextItems to rest of givenTextItems
if (oneItem contains testWord) then
if i mod N = 0 then
set AppleScript's text item delimiters to {testWord}
set temp to text items of oneItem
set AppleScript's text item delimiters to {""}
set resultString to resultString & (temp as string)
else
set resultString to resultString & oneItem & " "
end if
exit repeat
else
set resultString to resultString & oneItem
end if
end repeat
end repeat
end ignoring
set AppleScript's text item delimiters to tid
return text 1 thru -2 of resultString
end RemoveNthWord
on ReturnEditedString(theString)
log theString
repeat
try
set theChoice to choose from list {"Save in file", "Display Dialog", "Exit"} with prompt "What do you want to do with the result?"
set theChoice to theChoice as string
if theChoice is "Save in file" then
set theNewFileName to text returned of (display dialog "Give a name for the new file:" default answer "")
if theNewFileName does not end with ".txt" then set theNewFileName to (theNewFileName & ".txt") as string
set theNewFileFolder to (choose folder "Where do you want to save the file?") as alias
try
tell application "Finder" to make new file at theNewFileFolder with properties {name:theNewFileName}
tell application "Finder" to set theNewFile to (file theNewFileName of folder theNewFileFolder) as alias
set OA to open for access theNewFile with write permission
write theString to OA starting at 0
close access OA
display dialog "The data has been writen" buttons "OK" default button 1
on error theMsg
display dialog "An error occured, the script will try again." & return & return & theMsg
end try
else if theChoice is "Display Dialog" then
display dialog theString buttons "OK" default button 1
else
exit repeat
end if
end try
end repeat
end ReturnEditedString