-1

I am trying to create an applescript that will search for certain texts in a text file that are unfortunately variable (so every file I am supplied could have for instance of a different languages as the text that need finding GERMAN, FRENCH etc).

Then I need to replace this text with a user defined value. The text surrounding this is however a constant so,

For example the text file will contain order="GERMAN" where GERMAN needs to be replace by the users text.

I have written the code below where stringtofind is defined as GERMAN but what I really need is to define stringtofind as the value of whatever text appears between order=" and " .

The other issue, I have after this is that the file can in theory contain multiples of this value along with another value (so for instance a file could contain instances of order="GERMAN" along with instances of order="FRENCH" . In these cases I need that, user should be able to define different values for these (so all instance of GERMAN can be set to one replacement value and all instances of FRENCH can be set to a different value by the user)

Any assistance with this would be greatly appreciated.

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
4

1 回答 1

0

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

下面的脚本会执行您想要的操作:搜索 order=" 和 " 之间的任何表达式并替换该表达式。

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

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

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

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
于 2015-12-14T13:13:14.383 回答