Search code examples
applescripttexteditfindandmodify

applescript search text file for multiple variable texts using surrounding "static" text


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

Solution

  • Usually, the most powerful function for search/replace is the shell command "sed", but in your case, you do not really know what you are searching. Others experts may know special syntax for such search with sed, but I took an other approach, purely via Applescript.

    The script bellow does what you want : search for any expression between order=" and " and it replaces that expression.

    Each time new expression is found, the script asks user to enter replacement value. If the expression found has been already found before, script just takes already entered replacement expression.

    I add many comments to let you adjust if required (like saving the text file or save as..).

    You also have to replace the 1st line to select file with your own method.

    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