开发者

Creating an AppleScript to do a find in a replace in a given document

I'm looking to create aAppleScript that when run will:

  1. Search the document for a given string
  2. Replace that string with another given string

The strings will always be the same

Search for

This will be used in textmate - I was trying to do this in textmate

I know I can use textmate's find and replace functionality - I'm just trying to automate a little.

This should only make changes on the current page.

Is this 开发者_StackOverflow社区possible?

UPDATE: So I've found some code that has got me started...

tell application "TextMate" to activate
tell application "System Events"
    keystroke "f" using {command down}
        tell process "TextMate"
            keystroke "<?"
            keystroke tab
            keystroke "<?php"
            click button "Replace All"
        end tell
    keystroke "esc"
end tell

but I get the following error:

error "System Events got an error: Can’t get button \"Replace All\" of process \"TextMate\"." number -1728 from button "Replace All" of process "TextMate"

On the find and replace dialog of Textmate the button is labeled "Replace All" Am I missing something here?


You'll have to send the keystroke to the proper window. Something like tell window "find dialog" (or whatever). You have to be completely specific, so it might be

tell tab 1 of pane 1 of window "find and replace" of app textmate... 

User interface scripting is so hackalicious you should only do it as a last resort.

Looks like you need sed.

on a command line, or with do shell script:

cat /path/to/your/file.php|sed "s_<?_<?php_g">/path/to/your/newfile.php

or for a whole folder's worth

cd /path/to/your/folder
for file in *.php; do  cat "$file"|sed "s_<?_<?php_g">"${file/.php/-new.php}"; done


You're better off looking at MacScripter; there are lots of examples and solutions for find and replacing with or without a texteditor using Applescripts delimiters: MacScripter / Search results, like this:

on replaceText(find, replace, someText)
   set prevTIDs to text item delimiters of AppleScript
   set text item delimiters of AppleScript to find
   set someText to text items of someText
   set text item delimiters of AppleScript to replace
   set someText to "" & someText
   set text item delimiters of AppleScript to prevTIDs
   return someText
end replaceText


It's all very well sending folks to do search/replace within AppleScript strings, or sending them to Mac OS X's underlying Unix tools, like sed and Perl, but those are often no real substitue for searching/replacing text directly in the target application.

I had the exact same problem, albeit in Dragon Dicate rather than TextMade. Google led me here, where I was dismayed to find no direct solution. So let me share the one I came up with:

set find to "the"
set replace to "THE"
tell application "Dragon Dictate"
    activate
    tell application "System Events"
        tell process "Dragon Dictate"
            keystroke "f" using {command down}
            keystroke find
            keystroke tab
            keystroke replace
            tell window "Find"
                click button "Replace All"
            end tell
        end tell
    end tell
end tell

The key difference is addressing the Find window, which knows about the "Replace All" button. You will also have to change the "Dragon Dicate" target app to "TextMate" of course. (AppleScript seems to require knowing EXACTLY what app a script is being fired against, unless you want to fall back into some truly ugly low-level message sending. When dealing with AppleScript, that's just the 337th sigh of the day!)


If you want to write an AppleScript to manipulate text, there is nothing better than to use an AppleScriptable text editor. That is the right tool for the job. Then you can write just a few lines of code and get the job done.

For example, in TextMate, go Edit > Select All and Edit > Copy to copy the contents of your document to the clipboard. Then run this AppleScript:

tell application "TextWrangler"
    activate
    set theSearchString to the clipboard
    set theResultString to replace "old term" using "new term" searchingString theSearchString
    set the clipboard to theResultString
end tell

Then go back into TextMate and go Edit > Paste.

TextWrangler is available for free in Mac App Store.

You may be able to expand this script so that the TextMate work is automated with GUI scripting, but since it is just selecting all and copying and pasting, that is a fairly small task.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜