Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Trying to use AppleScript to find and replace text in TextEdit...

Hi all...


AppleScript newb here....



Trying to setup an applescript that will replace a few lines of code from and .htm file


This is ugly, but works:


tell application "Finder"


activate

tell application "System Events"


key down {command}


keystroke "a"


key up {command}

end tell

end tell


tell application "Finder"


activate

set fileCount to count files in front window

set fileCountAlert to fileCount

end tell

tell application "System Events"

tell process "Finder"

tell menu bar 1

tell menu bar item "File"

tell menu "File"

tell menu item "Open With"

tell menu "Open With"

click menu item "TextEdit"

end tell

end tell

end tell

end tell

end tell


end tell


delay 3

end tell


repeat until fileCount = 0


tell application "TextEdit"


activate

tell application "System Events"



key down {command}


keystroke "f"


key up {command}


--delay 1

keystroke "BORDER=1"

delay 0.5


keystroketab


keystroketab


keystroke "BORDER=0"

delay 0.5


key down {command}


keystroke "w"


key up {command}

delay 0.5

end tell


end tell


set fileCount to (fileCount - 1)

end repeat


tell application "Finder"

display dialog "Completed Adjusting " & fileCountAlert & " files."

end tell



I also tried using something like this.... but this thing didn't work at all....


set the search_document to (choose file)

replaceText("BORDER=1", "BORDER=0", search_document)


on replaceText(search_string, replacement_text, this_document)

tell application "TextEdit"


openthis_document

set AppleScript's text item delimiters to the search_string

set this_text to the text of the front document as list

set AppleScript's text item delimiters to the replacement_text

set the text of the front document to (this_text as string)


closethis_documentsavingyes

end tell

end replaceText



Lastly... I tried figuring out how to click buttons in applescript using something like this:


tell application "System Events"

tell process "TextEdit"


clickbutton "All" of scroll area of window "3.htm"

end tell

end tell


BUT - I keep getting errors here. I'm using a UIElementInspector app but no luck.





Wondering if anyone has anny quick help suggestions?


Posted on Jan 18, 2013 6:31 PM

Reply
13 replies

Jan 18, 2013 7:45 PM in response to Gregory Kane

You definitely don;t want to be GUI scripting this. First it's a real pain and second there is no reason to.


Something like this will get you started



tell application "TextEdit"

set wordlist to every character of document 1

end tell

set wordlist to wordlist as text


set finished to findAndReplace(wordlist, "BORDER=1", "BORDER=0")


tell application "TextEdit"


makenewdocumentwith properties {name:"ChangedText", text:finished}

end tell


on findAndReplace(theText, searchText, replText)

set {TempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, searchText}

set tempList to the text items of theText

set AppleScript's text item delimiters to replText

set finishedText to tempList as text

set AppleScript's text item delimiters to TempTID

return finishedText

end findAndReplace



It assumes TextEdit is open on the document you want to change. It ends with the changed text written to a new document but not written to disk. You can do that in TextEdit or make the script do it. I purposely made the changes to a new document. You really don;t want to make the changes in place, to many chances for something to go wrong.


As irt stands now the old text and the new text are hard coded in. BORDER=1 is changed to BORDER=0


Here is an example of a run


User uploaded file


If you;re loking to get into AppleScripting there ar many good sites out there with tutorials. Of course there is Apple's site with a lot of good information and a few books that I would recommend. Foremost on my list is Learn AppleScript by Apress where the bulk of the above code came from.


good luck.

Jan 19, 2013 3:33 AM in response to Frank Caggiano

Frank, thank you so much for your reply. My sense was that I DID NOT want to be GUI scripting... so, right on there... felt like I was trying to use QuickKeys....


Your script worked beautifully and I understand MOST of what's going on in there.


Also, thank you for the book suggestion, that looks like a monster reference, Amazon used, check. :-)



Best,

GK

Jan 19, 2013 5:31 AM in response to adayzdone

Alright, so here is the whole situation....


Using filemaker to export to html table.


Can't format html as far as I can see.


want to dump border off table - easy - you solved that and i had a GUI script that also worked, but awkward.


some of the exported data includes hyperlinks... when filemaker exports this data, it uses the html character codes instead of the actual character codes.


filemaker, i know i know....


anyhow, I want to create a script that basically scrubs the html.


Basically, I need to swap out:


>

<

"


for their actual characters.


I just integrated your shell script in with my other GUI stuff and this sucker almost works.


This is being used for what I hope will be a DAILY TEXT dump of html to a webserver.


Antiquated site that I built for my dad... http://www.ttender.com/partslist_pic.html


Basically, he sends me these horrible CSV files and I for many years have sliced them up and saved as in excel.


I created a set of filemaker scripts that gobbles up the data, splits it up, and exports it all out into html. It's incredible, to me.


Just need to scrub the html clean and I think i'll be good to go.


Here's the madness I have written as of now.


IT's working, but not closing windows correctly or something.


I know that there are way easier ways to do all of this... but, I'm just a school teacher. :-)



Thanks man...

Jan 19, 2013 5:32 AM in response to Gregory Kane

set myFiles to (choose file with multiple selections allowed)

set fileCount to countmyFiles

set reportFileCount to fileCount

set username to (short user name of (system info))


repeat with aFile in myFiles

set myData to do shell script "cat " & quoted form of (aFile's POSIX path)

set newData to do shell script "echo " & quoted form of myData & " | sed 's/BORDER=1/BORDER=0/g' > " & quoted form of (aFile's POSIX path)

end repeat


tell application "TextEdit"



openmyFiles


activate


repeat with aFile in myFiles


tell application "System Events"



key down {option}


key down {command}


keystroke "f"


key up {command}

key up {option}

delay 0.1


end tell


tell application "System Events"

tell application process "TextEdit"

tell window 1

tell scroll area 1

tell UI element "text field"

keystroke "\""

end tell

end tell

end tell

end tell

end tell


delay 0.1


tell application "System Events"


key down {shift}


keystroketab

key up {shift}

keystroke """

end tell


delay 0.1


tell application "System Events"

tell application process "TextEdit"

tell window 1

tell scroll area 1

tell group -1

click button "All"

end tell

end tell

end tell

end tell

end tell


delay 0.1



-- REPLACE <


tell application "System Events"


keystroketab


keystroke "<"


key down {shift}


keystroketab

key up {shift}

keystroke "&lt;"

end tell


delay 0.1


tell application "System Events"

tell application process "TextEdit"

tell window 1

tell scroll area 1

tell group -1

click button "All"

end tell

end tell

end tell

end tell

end tell



-- REPLACE >


tell application "System Events"


keystroketab


keystroke ">"


key down {shift}


keystroketab

key up {shift}

keystroke "&gt;"

end tell


delay 0.1


tell application "System Events"

tell application process "TextEdit"

tell window 1

tell scroll area 1

tell group -1

click button "All"

end tell

end tell

end tell

end tell

end tell


tell application "TextEdit"

set openWindow to the name of the front window

delay 0.1


closewindowopenWindow

end tell


set fileCount to (fileCount - 1)


end repeat


display dialog "Hello " & username & ", I have completed adjusting " & reportFileCount & " files."


end tell

Jan 19, 2013 6:02 AM in response to Gregory Kane

You can try something like this:


set myFiles to (choose file with multiple selections allowed)
repeat with aFile in myFiles
    set myData to do shell script "cat " & quoted form of (aFile's POSIX path)
    set newData to do shell script "echo " & quoted form of myData & " | sed -e 's/BORDER=1/BORDER=0/g' -e 's/&gt;/>/g' -e 's/&lt;/</g' -e 's/&quot;/\"/g'  > " & quoted form of (aFile's POSIX path)
end repeat


The basic form of the sed command is : sed 's/find/replace/'

Here is a great resource if you want to learn more: http://www.grymoire.com/Unix/Sed.html#toc-uh-13

Mar 17, 2013 7:28 AM in response to adayzdone

Dear Adayzdone

thanks a lot for your script

I am very new to AS and tried your script simple text and indeed worked fine on a text doc


but when i tried on a big CSV file I found some issues :

I tried with CSV but got error "255"


set myFiles to (choose file with multiple selections allowed)

repeat with aFile in myFiles

set myData to do shell script "cat " & quoted form of (aFile's POSIX path)

set newData to do shell script "echo " & quoted form of myData & " | sed -e 's:< p:<p:g' -e 's:< S:<s:g' -e 's:< P:<p:g' -e 's:< /:</:g' > " & quoted form of (aFile's POSIX path)

end repeat


Do you know why it works on text and NOT on csv ??

Trying to use AppleScript to find and replace text in TextEdit...

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