applescript 使用周围的 "static" 文本搜索多个变量文本的文本文件
applescript search text file for multiple variable texts using surrounding "static" text
我正在尝试创建一个 applescript
来搜索文本文件中的某些文本,不幸的是这些文本是可变的(因此我提供的每个文件都可能具有不同的语言,例如需要的文本查找德语、法语等)。
然后我需要用用户定义的值替换此文本。然而,围绕它的文字是一个常数,
例如,文本文件将包含 order="GERMAN"
,其中 GERMAN 需要替换为用户文本。
我写了下面的代码,其中 stringtofind
被定义为 GERMAN 但我真正需要的是将 stringtofind
定义为 order=" and "
之间出现的任何文本的值。
另一个问题,我在这之后是文件理论上可以包含这个值的倍数以及另一个值(例如,一个文件可以包含 order="GERMAN"
的实例以及 order="FRENCH"
。在这些情况下,我需要,用户应该能够为这些定义不同的值(因此 GERMAN 的所有实例都可以设置为一个替换值,而 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
通常,search/replace最强大的功能是shell命令"sed",但在您的情况下,您并不真正知道您在搜索什么。其他专家可能知道使用 sed 进行此类搜索的特殊语法,但我采用了另一种方法,完全通过 Applescript。
下面的脚本可以满足您的需求:搜索 order=" 和 " 之间的任何表达式并替换该表达式。
每次找到新表达式时,脚本都会要求用户输入替换值。如果找到的表达式之前已经找到,则脚本只采用已经输入的替换表达式。
我添加了很多评论,以便您根据需要进行调整(例如保存文本文件或另存为..)。
您还必须用自己的方法替换 select 文件的第一行。
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
我正在尝试创建一个 applescript
来搜索文本文件中的某些文本,不幸的是这些文本是可变的(因此我提供的每个文件都可能具有不同的语言,例如需要的文本查找德语、法语等)。
然后我需要用用户定义的值替换此文本。然而,围绕它的文字是一个常数,
例如,文本文件将包含 order="GERMAN"
,其中 GERMAN 需要替换为用户文本。
我写了下面的代码,其中 stringtofind
被定义为 GERMAN 但我真正需要的是将 stringtofind
定义为 order=" and "
之间出现的任何文本的值。
另一个问题,我在这之后是文件理论上可以包含这个值的倍数以及另一个值(例如,一个文件可以包含 order="GERMAN"
的实例以及 order="FRENCH"
。在这些情况下,我需要,用户应该能够为这些定义不同的值(因此 GERMAN 的所有实例都可以设置为一个替换值,而 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
通常,search/replace最强大的功能是shell命令"sed",但在您的情况下,您并不真正知道您在搜索什么。其他专家可能知道使用 sed 进行此类搜索的特殊语法,但我采用了另一种方法,完全通过 Applescript。
下面的脚本可以满足您的需求:搜索 order=" 和 " 之间的任何表达式并替换该表达式。
每次找到新表达式时,脚本都会要求用户输入替换值。如果找到的表达式之前已经找到,则脚本只采用已经输入的替换表达式。
我添加了很多评论,以便您根据需要进行调整(例如保存文本文件或另存为..)。
您还必须用自己的方法替换 select 文件的第一行。
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