Scenario description

This side needs to go to a ftp regularly to download the data down and change their fields. The process is:

  1. Download from a place to get a file name called "202301081014XXXX.REQ" data format xml
  2. change the data of one of the fields of S001 which content is "1234" Change it to "abcd"
  3. If the change is completed, create a file with the same name and name it as "202301081014XXXX.REQ.OK" 4.Upload the two created data to another ftp path

Currently I have tried the action

  1. via Mac app script tools
  2. The operation of recording the screen is as follows
tell application "Finder"
    open document file "202301081014N1234.REQ" of folder "Downloads" of folder "blue2" of folder "Users" of startup disk using application file "BBEdit.app" of folder "Applications" of startup disk
end tell
tell application "BBEdit"
    open find window
    find "<S001>1234</S001>" searching in text 1 of text document id 829 with selecting match
    set characters 406 thru 428 of text document id 829 to "<S001>abcd</S001>"
    select insertion point after characters 406 thru 428 of text document id 829
    save text document id 829
end tell

Problems encountered

Because the names of the data are different, I have read the official documents but I don't know how to define the scope. And the <S001> field is in a different position, so I can't execute the recorded AppleScript.

  • BBEdit has an extensive scripting dictionary - for example, the find command will return a record containing a reference to the character range of a match.
    – red_menace
    Jan 8 at 16:28
  • AppleScript is not the ideal tool to use here. You would probably do much better with a Bash script (command line) using sed or awk to fine/replace text elements quickly and efficiently.
    – Allan
    Jan 8 at 17:42
  • Also, don’t paste a screen shot of your code. Post the actual text and format accordingly. Anyone who wants to test your code won’t be able to unless they retype it manually. It’s unlikely people will want to do that.
    – Allan
    Jan 8 at 17:43
  • @Allan thank you for your advise will try sed / awk to use
    – blue2lynn
    Jan 9 at 13:39
  • @red_menace yes have use the find but it still have the document id that if i open anoother file want to use sripts that it can not work
    – blue2lynn
    Jan 9 at 13:41

1 Answer 1


Trying to record the UI is problematic at best, and can include extraneous statements and varying types of references. A better option is to look at an application's scripting dictionary (if it has one) to view the functionality it exposes - BBEdit has support for most of its functions.

In addition to an ID, windows and documents also have an index, which is their display order. The front object is always 1, so that can be used instead of trying to figure out a unique ID.

The find command will return a record containing a reference to the character range of a match, and also has an option to select it - those can be used instead of hard coded character offsets. The replace command will just replace all occurrences.

A more general purpose version of your script would be something like:

# Script settings - change values as desired:
property autoSave : false -- automatically save and close the document?
property replaceAll : false -- replace all occurances of searchText?
property searchText : "<S001>1234</S001>"
property replaceText : "<S001>abcd</S001>"

on run -- example
   doStuff for (choose file with multiple selections allowed)
end run

to doStuff for someFiles -- process file items
   tell application "BBEdit" to repeat with aFile in someFiles
      open aFile
      tell document 1 to tell its text
         if replaceAll then
            replace searchText using replaceText options {starting at top:true}
         else -- replace first match
            set findResult to find searchText options {starting at top:true} with selecting match
            if found of findResult then set contents of selection to replaceText
         end if
      end tell
      if autoSave then
         close document 1 with saving
      else -- review before closing, etc
      end if
   end repeat
end doStuff
  • The script worked better than I could have imagined! Thank you so much for your help! The problem was solved in another way (with the help of my colleague) for some reasons in the middle. So I forgot to come back and follow up on this side of the issue.
    – blue2lynn
    Jan 15 at 14:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .