applescript使用“环绕”搜索多变量文本的文本文件;“静态”;文本

applescript使用“环绕”搜索多变量文本的文本文件;“静态”;文本,applescript,textedit,findandmodify,Applescript,Textedit,Findandmodify,我正在尝试创建一个applescript,它将搜索文本文件中不幸可变的某些文本(因此我提供的每个文件都可能有不同的语言,例如需要查找德语、法语等的文本) 然后我需要用一个用户定义的值替换这个文本。但是,围绕这个的文本是一个常量 例如,文本文件将包含order=“德语”,其中德语需要由用户文本替换 我写了下面的代码,其中stringtofind定义为德语,但我真正需要的是将stringtofind定义为order=”和“之间出现的任何文本的值 另一个问题是,理论上,文件可以包含该值的倍数和另一个值

我正在尝试创建一个
applescript
,它将搜索文本文件中不幸可变的某些文本(因此我提供的每个文件都可能有不同的语言,例如需要查找德语、法语等的文本)

然后我需要用一个用户定义的值替换这个文本。但是,围绕这个的文本是一个常量

例如,文本文件将包含
order=“德语”
,其中德语需要由用户文本替换

我写了下面的代码,其中
stringtofind
定义为德语,但我真正需要的是将
stringtofind
定义为
order=”和“
之间出现的任何文本的值

另一个问题是,理论上,文件可以包含该值的倍数和另一个值(例如,文件可以包含
order=“GERMAN”
的实例和
order=“FRENCH”
的实例)。在这些情况下,我需要用户能够为这些值定义不同的值(因此,德语的所有实例都可以设置为一个替换值,法语的所有实例都可以由用户设置为不同的值)

对此,我们将不胜感激

tell application "System Events" 
activate
set myFile to choose file with prompt ("Select a file to be processed")
end tell
set stringtofind to "GERMAN"
display dialog "Please enter a value for " & stringtofind default answer "1"
set x to text returned of the result as string
tell application "TextEdit" 
open myFile
-- Find and replace
set every word of front document where it = stringtofind to x
end tell

通常,搜索/替换最强大的功能是shell命令“sed”,但在您的情况下,您并不真正知道要搜索什么。其他专家可能知道sed搜索的特殊语法,但我采用了另一种方法,纯粹通过Applescript

下面的脚本实现了您想要的功能:搜索order=“and”之间的任何表达式,并替换该表达式

每次找到新表达式时,脚本都会要求用户输入替换值。如果找到的表达式以前已找到,则脚本只接受已输入的替换表达式

我添加了许多注释,以便在需要时进行调整(如保存文本文件或另存为…)

您还必须用自己的方法替换第一行以选择文件

set myFile to "Users:me:Desktop:tests.rtf" --for my tests, but could be change to choose file !
-- this script replaces any occurance of the same text between Mytarget and EndTarget with new value
-- value replaced depends of found text

set myQuote to (ASCII character 34)
set myTarget to "order=" & myQuote -- order="
set endTarget to myQuote -- the character "
set TargetList to {} -- list of expressions already found
set ReplaceList to {} -- list of replacement expression already defined

tell application "TextEdit"
open myFile
set Mytext to text of document 1
end tell
set {TempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, myTarget}
set tempList to the text items of Mytext
repeat with I from 1 to (count of tempList)
set PosEnd to (offset of endTarget in (item I of tempList))
if PosEnd > 0 then -- found the next "
    set Expressfound to text 1 thru (PosEnd - 1) of (item I of tempList) -- extraxt the text to be replaced
    set MyNum to NumberInList(Expressfound, TargetList)
    if MyNum > 0 then
        set ReplaceExpress to item MyNum of ReplaceList -- word already found, replace with same word
    else -- new expression never found, ask for new replacement value
        set MyEntry to display dialog "By what do you want to replace the expression " & myQuote & Expressfound & myQuote & " ?" default answer Expressfound
        set ReplaceExpress to text returned of MyEntry
        set end of TargetList to Expressfound
        set end of ReplaceList to ReplaceExpress
    end if
    set (item I of tempList) to ReplaceExpress & (text PosEnd thru -1 of (item I of tempList))
end if
end repeat
tell application "TextEdit"
set text of document 1 to (tempList as text)
-- if required, you can save the document here  (or saves as... !)
end tell
set AppleScript's text item delimiters to TempTID -- reset delimiters to default values
display dialog " the text has been modified for the " & (count of TargetList) & " expressions found."


on NumberInList(LocalTarget, LocalList) -- return the number of the item in the list. 0 if not found
set NItem to 0
repeat with N from 1 to count of LocalList
    if item N of LocalList is LocalTarget then set NItem to N
end repeat
return NItem
end NumberInList