首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >创建AppleScript以在给定文档中的替换中进行查找

创建AppleScript以在给定文档中的替换中进行查找
EN

Stack Overflow用户
提问于 2010-01-11 18:51:31
回答 4查看 4.3K关注 0票数 1

我希望在运行时创建aAppleScript:

  1. 在文档中搜索给定的字符串
  2. 用另一个给定的字符串替换该字符串

字符串总是一样的

搜索

这将在textmate - 我想在textmate做这件事中使用

我知道我可以使用textmate的查找和替换功能--我只是尝试一下自动化。

这只应在当前页上进行更改。

这个是可能的吗?

UPDATE:所以我找到了一些代码让我开始.

代码语言:javascript
运行
复制
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

但我得到了以下错误:

代码语言:javascript
运行
复制
error "System Events got an error: Can’t get button \"Replace All\" of process \"TextMate\"." number -1728 from button "Replace All" of process "TextMate"

在Textmate的“查找和替换”对话框中,按钮被标记为“替换所有”,我在这里遗漏了什么吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-01-12 13:08:03

你得把击键发送到合适的窗口。比如tell window "find dialog" (或者其他什么)。你必须是完全明确的,所以可能是

代码语言:javascript
运行
复制
tell tab 1 of pane 1 of window "find and replace" of app textmate... 

用户界面脚本太麻烦了,你只能作为最后的手段来做。

看来你需要sed

在命令行上,或使用do shell script

代码语言:javascript
运行
复制
cat /path/to/your/file.php|sed "s_<?_<?php_g">/path/to/your/newfile.php

或者整个文件夹的价值

代码语言:javascript
运行
复制
cd /path/to/your/folder
for file in *.php; do  cat "$file"|sed "s_<?_<?php_g">"${file/.php/-new.php}"; done
票数 1
EN

Stack Overflow用户

发布于 2010-01-11 19:23:19

最好还是看一下MacScripter;有很多示例和解决方案可以使用Applescripts分隔符:MacScripter /搜索结果来查找和替换是否使用文本分隔符,如下所示:

代码语言:javascript
运行
复制
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
票数 1
EN

Stack Overflow用户

发布于 2011-11-15 22:16:44

在AppleScript字符串中进行搜索/替换,或者将它们发送到Mac的底层Unix工具(如sed和Perl ),这一切都是非常好的,但它们通常并不能真正替代直接搜索/替换目标应用程序中的文本。

我也遇到了同样的问题,尽管我用的是龙条,而不是TextMade。谷歌带领我来到这里,我对没有找到直接的解决方案感到沮丧。所以让我分享一下我想出的那个:

代码语言:javascript
运行
复制
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

关键的区别是寻址窗口,它知道“替换所有”按钮。当然,你还必须将“龙字典”目标应用程序更改为"TextMate“。(AppleScript似乎需要确切地知道脚本所针对的应用程序,除非您想要返回到某个真正丑陋的低级消息发送中。当与AppleScript打交道时,这只是一天中的第337次叹息!)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2044094

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档