Apple Script:Find 和更换不工作

Apple Script:Find and replace not working

各位,我正在尝试让用户输入他们想要字幕的电影名称,然后让 apple 脚本自动在字幕网站上搜索他们输入的电影名称。 为此,电影名称中的所有空格都需要替换为 + 号,因为 url 会将空格转换为 + 号。代码不工作,我收到以下错误:

  1. 预期为“结束”但发现为“开启”。
  2. “(”不能跟在这个标识符后面。

这是我的代码;

 on run

    display dialog "What's the name of the movie?" default answer " " with title "What's the name of the movie?" buttons {"OK"} default button 1
    set moviename to text returned of the result
    set theText to moviename
    set theSearchString to " "
    set theReplacmentString to "+"

     end 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

 goToWebPage("https://rs.titlovi.com/prevodi/?prevod= & thetext")
 tell application "Safari"
 activate
 set URL of document 1 to theWebPage
 end tell
 end goToWebPage

end run

提前致谢。

  1. 函数(处理程序,在 AppleScript 中)不能嵌套在 AppleScript 中。因此,您需要将 findAndReplaceInText 和 goToWebPage 移到 on run 之外,或者将它们的功能合并到 on run 中而不使用处理程序。

  2. 处理程序以 on handlerName 开头并以 end handlerName 结尾;您的 findAndReplaceInText 以 end findAndReplaceInText.

  3. 开头和结尾

以下是分离处理程序后它可能如何工作:

on run
    display dialog "What's the name of the movie?" default answer " " with title "What's the name of the movie?" buttons {"OK"} default button 1
    set moviename to text returned of the result
    set moviename to findAndReplaceInText(moviename, " ", "+")
    goToWebPage("https://rs.titlovi.com/prevodi/?prevod=" & moviename)
end run

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

on goToWebPage(theWebPage)
    tell application "Safari"
        activate
        set URL of document 1 to theWebPage
    end tell
end goToWebPage

我已经在 Mac OS X 10.14.6.

的 Safari 中验证了这段代码