Simple automator string search and replace

Discussion in 'Mac Programming' started by kjmatthews, Nov 13, 2009.

  1. kjmatthews macrumors newbie

    Nov 13, 2009
    Not being well-versed in programming languages of any sort, I hoped someone here might be able to help me do the following. When copying and pasting text from PDFs into other documents, there are two tedious operations I currently do using find-and-replace, and would like to automate them by turning them into a Service through Automator. I'm running Snow Leopard, so this should be easy. What I can't do for myself is write the script.

    I would like the service first to find the string " - " (space-hyphen-space) and replace it with "—" (an em-dash with no spaces on either side). Then I would like it to find the string "- " (hyphen-space) and delete it (i.e., replace it with nothing).

    I think Automator can accomplish this using AppleScript or a shell script. Can anyone help me?
  2. mysterytramp macrumors 65816


    Jul 17, 2008
    I had a similar issue last month, and the thread might give you some ideas.

    If you want to go the Service route, I think your best bet is to use TextWrangler, which is a free download. Open a file, then hit the Record button in the AppleScript Editor. You copy/paste the script into the Run AppleScript Action in Automator, and from there, create the service.

    Command line tools might be more straightforward, but I'm guessing you'd have to change your work habits.

  3. kjmatthews thread starter macrumors newbie

    Nov 13, 2009
    Thanks for the suggestion, myserytramp. The TextWrangler method is not really working for me — I can generate the script well enough and all that, but it would require that I copy the text from Skim (where I do my PDF annotation) to TextWrangler, invoke the service, copy it back to Skim (so the corrected text will be searchable), and also to whatever document I want the text for.

    If I could generate a similar script that worked in Skim, it would be ideal, but commands given to Skim don't seem to register in the AppleScript editor.

    The awesome, free WordService service from Devon Technologies does something I imagine to be similar to what I'm asking for (i.e. searching for and replacing certain strings), but I don't know how they manage do it.

Share This Page