6

I'm currently writing a super simple script and I need to find and replace text in a variable (specifically in the path of the items dropped onto the applescript.) Here is what I have currently:

    on open {dropped_items}
    tell application "Finder" to set filePathLong to the POSIX path of dropped_items as text


on replaceText(find, replace, subject)
    set prevTIDs to text item delimiters of AppleScript
    set text item delimiters of AppleScript to find
    set subject to text items of subject

    set text item delimiters of AppleScript to replace
    set subject to "" & subject
    set text item delimiters of AppleScript to prevTIDs

    return subject
end replaceText

get replaceText("/mpc/mayors1", "/ifs/disk1", filePathLong)




display dialog subject

end open

(excluding irrelevant code and adding a dialog to verify it worked)

That "on replaceText..." block I got from searching Stack Overflow for the title of this post. My problem is that when I try to compile it tells me it expected an "end" but found an "on." I'm assuming it wants me to end my open before I can "on replaceText" but I don't want to do that. Any ideas as to what I could do to get it to work? Sorry if this is very simple, I'm pretty new to AppleScript.

I understand I could just chop off the first twelve characters and then add "/ifs/disk1" to the beginning of the string but I want to know why this isn't working in case this happens again.

JohnShafto
  • 83
  • 1
  • 1
  • 4

3 Answers3

7

You can't place a handler within another (explicit) handler. Made some other corrections as well.

on open dropped_items
    repeat with anItem in dropped_items
        set filePathLong to anItem's POSIX path
        set mySubject to replaceText("/mpc/mayors1", "/ifs/disk1", filePathLong)
        display dialog mySubject
    end repeat
end open


on replaceText(find, replace, subject)
    set prevTIDs to text item delimiters of AppleScript
    set text item delimiters of AppleScript to find
    set subject to text items of subject

    set text item delimiters of AppleScript to replace
    set subject to subject as text
    set text item delimiters of AppleScript to prevTIDs

    return subject
end replaceText
adayzdone
  • 11,120
  • 2
  • 20
  • 37
1

You can follow this https://github.com/abbeycode/AppleScripts/blob/master/Scripts/Libraries/Strings.applescript

on replace_text(this_text, search_string, replacement_string)
    set prevTIDs to AppleScript's text item delimiters
    set AppleScript's text item delimiters to the search_string
    set the item_list to every text item of this_text
    set AppleScript's text item delimiters to the replacement_string
    set this_text to the item_list as string
    set AppleScript's text item delimiters to prevTIDs
    return this_text
end replace_text
onmyway133
  • 45,645
  • 31
  • 257
  • 263
1

AppleScript also has a built in handler for this. https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ManipulateText.html

Finding and Replacing Text in a String

The handler in Listing 19-9 can be used to find and replace text in a string. To use it, provide some source text, a string to find, and a replacement string. This handler replaces any found instances of the specified search string.

APPLESCRIPT

Open in Script Editor Listing 19-9AppleScript: Handler that finds and replaces text in a string

    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

Listing 19-10 shows how to call the handler in Listing 19-9.

APPLESCRIPT

Open in Script Editor Listing 19-10AppleScript: Calling a handler to find and replace text in a string

    set theText to "On Tuesday, I told you to have the report ready by next Tuesday."
    set theText to findAndReplaceInText(theText, "Tuesday", "Friday")
    --> Result: "On Friday, I told you to have the report ready by next Friday."
vau
  • 11
  • 1