Replace 在AppleScript中查找并替换

Replace 在AppleScript中查找并替换,replace,applescript,Replace,Applescript,我目前正在编写一个超级简单的脚本,我需要查找并替换变量中的文本(特别是在applescript上的项目路径中)。以下是我目前拥有的内容: 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

我目前正在编写一个超级简单的脚本,我需要查找并替换变量中的文本(特别是在applescript上的项目路径中)。以下是我目前拥有的内容:

    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
(不包括不相关的代码,并添加一个对话框以验证其是否有效)

我在堆栈溢出中搜索这篇文章的标题时得到的“on replaceText…”块。我的问题是,当我试图编译它时,它告诉我它期望一个“结束”,但找到了一个“打开”。我假设它希望我在“打开替换文本”之前结束我的打开,但我不想这样做。我能做些什么让它工作呢?很抱歉,如果这很简单,我对AppleScript还很陌生


我知道我可以删掉前12个字符,然后在字符串开头添加“/ifs/disk1”,但我想知道,如果再次发生这种情况,为什么这样做不起作用。

您不能将一个处理程序放在另一个(显式)处理程序中。还做了一些其他更正

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
你可以照这个做

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