jaysonking

Q: Text Edit Script Function, does it exist?

Thank you for taking the time to look into this.

 

At work I constantly have to go through textEdit using the find and replace to remove all items in between brackets. So Find [1] and replace it with nothing which I guess I am not replacing but removing.

 

I wondered if there was something to put in find that would find all instances of the brackets but also whats inside them? I was thinking with all the functions in Office to represent items maybe there is something that would find... [anything in these brackets].

 

I appreciate your time and help very much! 

Mac Pro, OS X Mavericks (10.9.4)

Posted on Jan 23, 2015 8:36 PM

Close

Q: Text Edit Script Function, does it exist?

  • All replies
  • Helpful answers

Page 1 Next
  • by Drew Reece,Helpful

    Drew Reece Drew Reece Jan 23, 2015 8:51 PM in response to jaysonking
    Level 5 (7,485 points)
    Notebooks
    Jan 23, 2015 8:51 PM in response to jaysonking

    In TextEdit's search bar click the 'magnifying glass'

    Select 'insert pattern'

     

    That gives you a popup with options for matching different character types. Select one that suits your needs. Wrap it in the brackets to match the desired text…

     

    TextEdit regex.png

    I'm not sure how you can use these matched items in the replacement text, but it should allow you to do the 'removing' that you asked about

  • by rccharles,Helpful

    rccharles rccharles Jan 24, 2015 11:19 AM in response to jaysonking
    Level 6 (8,464 points)
    Classic Mac OS
    Jan 24, 2015 11:19 AM in response to jaysonking

    I suggest you look at TextWranger which supports regular expressions.  It will allow you to search for "[" any text "]" and replace or delete the string.  If you files are plan text this is the way to go. The actual search string could be something like

    \[.*\]

     

    I like TextWranglerr for editing bash scripts.
    http://www.barebones.com/products/textwrangler/

  • by t quinn,

    t quinn t quinn Jan 24, 2015 1:41 PM in response to jaysonking
    Level 5 (4,995 points)
    Mac OS X
    Jan 24, 2015 1:41 PM in response to jaysonking

    Hi all,

     

    I am trying to address this question also. I want to build a script that will automatically do a find and replace in textedit with predefined values for find and replace.

     

    quinn

  • by Drew Reece,

    Drew Reece Drew Reece Jan 24, 2015 2:14 PM in response to t quinn
    Level 5 (7,485 points)
    Notebooks
    Jan 24, 2015 2:14 PM in response to t quinn

    t quinn wrote:

     

    Hi all,

     

    I am trying to address this question also. I want to build a script that will automatically do a find and replace in textedit with predefined values for find and replace.

     

    quinn

    What type of file is it that you need to replace the text?

     

    TextEdit may not always the best answer, if the file is a plaintext file you can use command line tools (sed, awk, grep, perl…).

    If the file is a Word doc, Applescript, RTF or other binary/ non-plaintext format you may need to use other tools.

  • by t quinn,

    t quinn t quinn Jan 24, 2015 2:33 PM in response to Drew Reece
    Level 5 (4,995 points)
    Mac OS X
    Jan 24, 2015 2:33 PM in response to Drew Reece

    Hi Drew,

     

    In an effort to expand my scripting abilities I wanted to write a little script that would gather the HTML from the reply box on these apple forums and replace the indent that gets added when you paste an applescript into the window. Basically I want to replace "text-indent: -" with "text-indent: " and paste it back into the reply window. I have pages and text edit but no other text editors right now.

     

    Most of my scripting has been for Numbers so I thought a little inter-app script would be fun.

     

    quinn

  • by t quinn,

    t quinn t quinn Jan 24, 2015 5:19 PM in response to t quinn
    Level 5 (4,995 points)
    Mac OS X
    Jan 24, 2015 5:19 PM in response to t quinn

    Hi Drew,

     

    In an effort to expand my scripting abilities I wanted to write a little script that would gather the HTML from the reply box on these apple forums and replace the indent that gets added when you paste an applescript into the window. Basically I want to replace "text-indent: " with "text-indent: " and paste it back into the reply window.

     

    Most of my scripting has been for Numbers so I thought a little inter-app script would be fun.

     

    This is what I have so far. I still need to figure out how to find and click the HTML button of this window and the "show full editor" button to get back. If you have any applescript chops to share I am receptive.

     

    quinn

     

    --script follows-

     

    tell application "Safari"

      activate application "Safari"

      tell application "System Events" to keystroke "a" using command down

      delay 0.15

      tell application "System Events" to keystroke "c" using command down

    end tell

     

    activate application "TextEdit"

    tell application "TextEdit"

      make document

      set text of front document to the clipboard

      set fdText to "text-indent: "

      set rplText to "text-indent: "

     

     

      activate application "TextEdit"

      set the clipboard to fdText

      tell application "System Events" to keystroke "f" using command down

      tell application "System Events" to keystroke "v" using command down

      delay 0.15

      set the clipboard to rplText

      tell application "System Events" to keystroke "f" using [command down, option down]

      tell application "System Events" to keystroke "v" using command down

      delay 0.15

      activate application "TextEdit"

      tell application "System Events" to keystroke tab

      delay 0.15

      tell application "System Events" to keystroke space

      delay 0.15

      set sr to front document's text

      set the clipboard to sr

    end tell

     

    activate application "Safari"

    tell application "Safari"

      tell application "System Events" to keystroke "v" using command down

    end tell

  • by Drew Reece,

    Drew Reece Drew Reece Jan 24, 2015 5:31 PM in response to t quinn
    Level 5 (7,485 points)
    Notebooks
    Jan 24, 2015 5:31 PM in response to t quinn

    Sounds more like a job for a javascript bookmarklet to me.

    You may be able to do it all in the browser…

    https://en.wikipedia.org/wiki/Bookmarklet

    I have no idea how you would go about it though

     

    TextEdit isn't really designed to be used how you intend as far as I can see. Also Applescript between two apps is 'kludgy' prone to breaking, cut TextEdit out of the loop & use 'do shell script' to run 'sed', 'awk' or other tools to change the html. That seems like overkill though.

     

     

    Wouldn't it be easier to submit a bug report to the host or create a post asking who to tell to get it fixed? That would benefit everyone here, not just you

  • by t quinn,

    t quinn t quinn Jan 24, 2015 5:36 PM in response to Drew Reece
    Level 5 (4,995 points)
    Mac OS X
    Jan 24, 2015 5:36 PM in response to Drew Reece

    Hi Drew,

     

    Bug report was done. Just wanted to see if I could do this simple thing. I kept adding delays when it seemed that the keystrokes weren't taking. Script works as it is.

     

    quinn

  • by Drew Reece,

    Drew Reece Drew Reece Jan 24, 2015 5:44 PM in response to t quinn
    Level 5 (7,485 points)
    Notebooks
    Jan 24, 2015 5:44 PM in response to t quinn

    Sorry I started creating that post a while ago & missed the script you posted

    I'll look at what you have & post back if anything can be made better.

  • by Drew Reece,

    Drew Reece Drew Reece Jan 25, 2015 9:38 AM in response to Drew Reece
    Level 5 (7,485 points)
    Notebooks
    Jan 25, 2015 9:38 AM in response to Drew Reece

    Isn't this meant to replace something…

     

      set fdText to "text-indent: "

      set rplText to "text-indent: "

     

    Seems like it is replacing 'text-indent: ' with the same value. It would help to set rplText to "text-indent: -"

     

    The space bar & tab keystroke thing also fails to do anything in TextEdit for me too (on 10.9). Instead you would need to find the UI buttons & click them to make this work reliably. We used to use 'UIElementInspector' to do this, as this old article explains it …

    http://www.mactech.com/articles/mactech/Vol.21/21.06/UserInterfaceScripting/inde x.html

    It looks like it is renamed & is part of the developer tools…

    where to find  UI Element Inspector

     

    I gave up with UI scripting years ago - it would break easily, perform badly & was generally not worth the time it took to fix, maybe it got better?

     

    You can skip all of this TextEdit nonsense with a simple call to 'do shell script' that uses 'sed' to find & replace instead.

    Here is an example (it is independent, not part of your version)…

     

    set inputstring to "Text with text-indent: 85px"

     

    -- use 'sed' to replace the '-' after the text-indent:

    set cmd to "echo " & inputstring & " | sed -e 's/text-indent: -/text-indent: /g' "

    set outputstring to (do shell script cmd)

     

    --see what we got

    display dialog "inputstring: " & return & inputstring & return & return & "outputstring:" & return & outputstring

     

    See if you can adapt that - cutting TextEdit out saves you the hassle of all those unsaved documents too

     

    As ever the manuals help…

    In Script Editor File > Open Dictionary > find Standard Additions in the list. That has info on 'do shell script'.

    https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/ man1/sed.1.html

  • by t quinn,

    t quinn t quinn Jan 25, 2015 9:51 AM in response to Drew Reece
    Level 5 (4,995 points)
    Mac OS X
    Jan 25, 2015 9:51 AM in response to Drew Reece

    Thanks, Drew,

     

    set fdText to "text-indent: " was originally set fdText to "text-indent: -" but I used my script to adjust the margins....


    I am running 10.9.5 and textedit 1.9. Tab got me to the replace button and space clicked it. I do think this script is a kludge- just looking at all the keystroke commands.


    Looks like I will be exploring shell scripts.

    The hint about finding the buttons will be useful when I try to click the HTML button on this window.


    quinn

  • by Drew Reece,

    Drew Reece Drew Reece Jan 25, 2015 10:32 AM in response to t quinn
    Level 5 (7,485 points)
    Notebooks
    Jan 25, 2015 10:32 AM in response to t quinn

    I'm not sure the 'Toggle html mode' button in the webpage is findable via Accessibility Inspector. It isn't a 'standard UI control', it's parts of the webpage, you will need to dig with the tool to see if it is detected. The old way to resolve issues like this was to 'click at x,y coordinates' (very fragile - depends on window size & location etc).

     

    This is why I think javascript would be far better - it has access to the raw page, you can make page elements click via JS, or change the content etc.

  • by t quinn,Solvedanswer

    t quinn t quinn Jan 25, 2015 12:41 PM in response to Drew Reece
    Level 5 (4,995 points)
    Mac OS X
    Jan 25, 2015 12:41 PM in response to Drew Reece

    Hi Drew,

     

    I get your recomendation for javascript. Makes perfect sense dealing with browsers. My primary interest is to improve my apple scripting chops and a foray into a shell script makes sense. Let me know if you get tired of my questions or want me to start a new thread- we are somewhat off topic by now.

     

    Something is going off as I start to modify the script you suggested. I thought I would pass it a test from the clipboard:

    Screen Shot 2015-01-25 at 1.26.25 PM.png

    I didn't see what could go wrong. When I ran the script I got this error:

    Screen Shot 2015-01-25 at 1.23.24 PM.png

    It goes on with the entire contents of the clipboard, running off my screen at the bottom so I can't tell if there is any needed info at the bottom. I can't figure out how to make the error message leave the window. This one editor window is frozen, and the only way pst has been to quit the editor.

     

    Is there something obvious I can change?

     

    quinn

  • by Drew Reece,

    Drew Reece Drew Reece Jan 25, 2015 12:55 PM in response to t quinn
    Level 5 (7,485 points)
    Notebooks
    Jan 25, 2015 12:55 PM in response to t quinn

    Apologies that is my fault… I forgot to wrap the echo in quotes. The shell is fussy about certain characters & raw html contains a lot to break it.

     

    Here is the same thing with the input string wrapped in double quotes…

     

    set inputstring to "Text with text-indent: 85px <> & "

     

    -- use 'sed' to replace the '-' after the text-indent:

    set cmd to "echo \"" & inputstring & "\" | sed -e 's/text-indent: -/text-indent: /g' "

    set outputstring to (do shell script cmd)

     

    --see what we got

    display dialog "inputstring: " & return & inputstring & return & return & "outputstring:" & return & outputstring

     

     

    The \" is passed as a double quote ". The backslash is an escape character to allow us to send the double quote inside a string already wrapped in double quotes (you getting this, it's hard to parse sometimes ).

     

    We are wildly off topic, so yep I think a new thread is in order, add a note to the top of the new one linking here & post a link from here too & I'll follow you there.

     

    I'm OK hacking about to learn if thats what you want

Page 1 Next