AppleScript to delete the whole line if ..

Im using the following code to find and replace with "target word" but I would like to DELETE the whole line instead of replacing the actual word.


so it would be


if the line contains www.network -> Delete the whole line.


Thanks in advance!



set theTargetWords to {"www.network"}


tell application "Pages" to activate


tell application "System Events" to tell process "Pages"



-- Open the "Find & Replace" window:


keystroke "f" usingcommand down

repeat until exists window "Find & Replace"

end repeat


tell tab group 1 of window "Find & Replace"



-- Adjust the settings:


clickradio button 2 -- Advanced

if value of checkbox 2 is 1 then click checkbox 2 -- Don't match case

if value of checkbox 3 is 1 then click checkbox 3 -- Whole words

if value of checkbox 1 is 0 then clickcheckbox 1 -- Search previous text (loop)


clickpop up button 1 -- find in


clickmenu item 1 of menu 1 of pop up button 1 -- Entire document


clickpop up button 2 -- replace style


clickmenu item 1 of menu 1 of pop up button 2 -- Any



-- Delete every target word:

set value of text area 1 of scroll area 2 to ""

repeat with thisWord in theTargetWords

set value of text area 1 of scroll area 1 to thisWord -- the target word

click button "Replace All"

end repeat


end tell

end tell

AppleScript

Posted on Sep 22, 2013 12:41 AM

Reply
1 reply

Sep 22, 2013 8:20 AM in response to thebluedrg

if you want to script an application, read and use the application scripting dictionary. using System Events to script the GUI is something you should only do as a last resort. It creates clunky, slow, overly complex, and breakable scripts.


try this to start:


tell application "Pages"

delete (every paragraph of body text of document 1 whose contents contains "some key phrase")

end tell

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

AppleScript to delete the whole line if ..

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.