Moeen
Moeen

Reputation: 61

Expect an end but find on

I have a code like this

tell application "app"
    set 1 to something
    repeat with some in 1
        set refpdf to field "File Attachments" of record 1
        on findAndReplaceInText(theText, theSearchString, theReplacementString)
    set AppleScript's text item delimiters to theSearchString
    set theTextItems to every text item of theText
    set AppleScript's text item delimiters to theReplacementString
    set theText to theTextItems as string
    set AppleScript's text item delimiters to ""
    return theText
end findAndReplaceInText
        set refpdf1 to findAndReplaceInText(refpdf, "some", "some2")
        set libh to findAndReplaceInText(libraryname, "test", "test2")
        
    end repeat
end tell

It gives me an error, But I should change text with the on findAndReplaceInText command, what I have done wrong?

Upvotes: 0

Views: 37

Answers (1)

vadian
vadian

Reputation: 285074

Three issues:

  1. The handler must be on the top level of the script
  2. some is a reserved word, use something else
  3. A handler inside an application tell block must be called with my

tell application "app"
    set 1 to something
    repeat with somethingElse in 1
        set refpdf to field "File Attachments" of record 1
        set refpdf1 to my findAndReplaceInText(refpdf, "some", "some2")
        set libh to my findAndReplaceInText(libraryname, "test", "test2")
    end repeat
end tell

on findAndReplaceInText(theText, theSearchString, theReplacementString)
    set AppleScript's text item delimiters to theSearchString
    set theTextItems to every text item of theText
    set AppleScript's text item delimiters to theReplacementString
    set theText to theTextItems as string
    set AppleScript's text item delimiters to ""
    return theText
end findAndReplaceInText

Upvotes: 2

Related Questions