Apple Script: найти и заменить не работает - PullRequest
0 голосов
/ 01 апреля 2020

все, я пытаюсь заставить пользователя ввести имя mov ie, для которого они хотят субтитры, и сценарий для apple, чтобы автоматически искать на сайте субтитров имя mov ie, которое они ввели. Для этого необходимо заменить все пробелы в имени mov ie на знак +, поскольку URL-адреса преобразуют пробелы в знак +. Код не работает, и я получаю следующие ошибки:

  1. Ожидается «конец», но найдено «on».
  2. A «(» не может go после этого идентификатора .

Вот мой код;

 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 Ответ

1 голос
/ 01 апреля 2020
  1. Функции (обработчики, на языке AppleScript) не могут быть вложены в AppleScript. Поэтому вам нужно либо переместить findAndReplaceInText и goToWebPage за пределы on run, либо объединить их функциональность в on run без использования обработчиков.

  2. Обработчики начинаются с on handlerName и заканчиваются end handlerName; у вас есть findAndReplaceInText, начинающийся и заканчивающийся end findAndReplaceInText.

Вот как это может работать после разделения обработчиков:

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

Я проверил этот код в Safari на Ма c OS X 10.14.6.

...