Applescript 在脚本编辑器中有效,但在 Automator 中无效

问题描述 投票:0回答:1

我对 Applescript 很陌生,但多年来一直在电视上扮演程序员。下面的运行处理程序工作正常,但是当我尝试使用 replaceText() 或 findAndReplaceInText() 处理程序将“#”替换为“%23”时,它没有。如果我在处理程序的第一行添加诸如“return 'xxxx'”之类的内容,那么该值将被返回,并且一切都按预期工作,所以问题似乎出在处理程序中,但我对它们的执行方式只有一个稀疏的想法字符串替换。我预计处理程序中的某些功能可能在 Automator 中不可用。

在此先感谢您的帮助, -帕特

on run {input, parameters}
    set x to input
    set searchString to findAndReplaceInText(x, "#", "%23")
    set myLink to "https://a.b.c?search_string=" & searchString
    tell application "Google Chrome"
        activate
        open location myLink
    end tell
    return input
end run

on replaceText(find, replace, someText)
    set prevTIDs to text item delimiters of AppleScript
    set text item delimiters of AppleScript to find
    set someText to text items of someText
    set text item delimiters of AppleScript to replace
    set someText to "" & someText
    set text item delimiters of AppleScript to prevTIDs
    return someText
end replaceText

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

我原以为“#”会像在脚本编辑器中那样与“%23”一起出现。

macos applescript automator
1个回答
0
投票

假设您的工作流程将该字符串传递给

run applescript
操作,您可以尝试将其用作脚本。正如@red_menace 所建议的那样,input 是一个单项列表,因此您需要获取它的项目才能将其作为字符串使用。当返回 myLink 时,它也是一个列表。

on run {input, parameters}
    set x to item 1 of input
    display dialog x
    --> some.web.site.com/#Home/12345678
    set parameterString to findReplaceInText(x, "#", "%23")
    set myLink to "https://a.b.c?search_string=" & parameterString
    return myLink
end run

on findReplaceInText(sourceText, findString, replaceString)
    set AppleScript's text item delimiters to findString
    set sourceTextItems to sourceText's text items
    
    set AppleScript's text item delimiters to replaceString
    set sourceText to sourceTextItems as text
    display dialog sourceText
    --> some.web.site.com/%23Home/12345678
    
    set AppleScript's text item delimiters to ""
    return sourceText
end findReplaceInText

您可以删除或注释掉显示对话框命令。

© www.soinside.com 2019 - 2024. All rights reserved.