【问题标题】:How can I convert the input __NSArrayM into an AppleScript list in an Automator Action project?如何将输入 __NSArrayM 转换为 Automator Action 项目中的 AppleScript 列表?
【发布时间】:2014-05-31 12:58:34
【问题描述】:

自动化操作

我正在制作一个自定义 Automator 操作来操作文本输入。我测试了输入以查看它是什么类,结果是__NSArrayM。这意味着我需要以某种方式将此输入转换为 AppleScript 可以理解的列表,并最终转换为字符串。我只需要隔离字符串,然后将其转换回相同的对象用于输出。

总结:

  • 将 __NSArrayM 输入 转换为 AppleScript 列表对象
  • 将 AppleScript 列表对象转换回 输出 的 __NSArrayM

我希望我的自动机看起来像这样:

XCode 中的编码尝试

我的编码尝试如下:

script Change_Case
    property parent : class "AMBundleAction"
    property menuChoices : {"Title Case","UPPER CASE","lower case","tOGGLE cASE"}
    property menuSelection : 0
    on runWithInput_fromAction_error_(input, anAction, errorRef)
        set inputClass to class of input -- just for debugging

        set menuSel to menuSelection
        if menuSel is 0 -- Title Case
            display dialog menuSel as string
        end if

        tell class "NSArray" of current application to set inputValues to arrayWithObjects_(input)
        log inputValues -- just for debugging
    end runWithInput_fromAction_error_
end script

最终代码更新

为了完整起见,我想我会将最终代码发布到我的自动化操作中。关键步骤是输入输出com.apple.cocoa.string如下图所示。

script Change_Case
    property parent : class "AMBundleAction"
    property menuChoices : {"Title Case","UPPER CASE","lower case","tOGGLE cASE"}
    property menuSelection : missing value
    property array : class "NSArray"
    
    on runWithInput_fromAction_error_(input, anAction, errorRef)
        set inputValues to input as list
        set theString to item 1 of inputValues

        if (menuSelection as string) is "missing value"
            set menuSelection to 0
        end if
        set menuSelection to menuSelection as integer
        
        if menuSelection is 0 -- Title Case
            --display dialog "Title Case, Menu Selection ID: " & menuSelection as string
            set mycode to "import sys; print sys.argv[1].title()"
            set myscript to "/usr/bin/python -c " & mycode's quoted form & " " & quoted form of theString
            set output to (do shell script myscript) as string
        end if
        if menuSelection is 1 -- UPPER CASE
            --display dialog "Upper Case, Menu Selection ID: " & menuSelection as string
            set mycode to "import sys; print sys.argv[1].upper()"
            set myscript to "/usr/bin/python -c " & mycode's quoted form & " " & quoted form of theString
            set output to (do shell script myscript) as string
        end if
        if menuSelection is 2 -- lower case
            --display dialog "Lower Case, Menu Selection ID: " & menuSelection as string
            set mycode to "import sys; print sys.argv[1].lower()"
            set myscript to "/usr/bin/python -c " & mycode's quoted form & " " & quoted form of theString
            set output to (do shell script myscript) as string
        end if
        if menuSelection is 3 -- tOGGLE cASE
            --display dialog "Swap Case, Menu Selection ID: " & menuSelection as string
            set mycode to "import sys; print sys.argv[1].swapcase()"
            set myscript to "/usr/bin/python -c " & mycode's quoted form & " " & quoted form of theString
            set output to (do shell script myscript) as string
        end if
        return output
    end runWithInput_fromAction_error_
end script

【问题讨论】:

    标签: applescript automator applescript-objc


    【解决方案1】:

    脚本的目标是获取文本字符串数组,并根据 menuSelection 更改它们的大小写吗?

    您可以将传递的数组强制转换为 AS 列表:

    set inputValues to input as list
    

    主要是计划接收单个文本输入吗?然后转到目标/常规,输入和输出选项: com.apple.applescript.text-object 或者 com.apple.cocoa.string 这些将是传递的数组项的类。

    【讨论】:

    • 是的,你理解正确,是的,它很可能只接收一个文本输入,但如果它可以处理多个输入以实现灵活性,那也很酷。我想我试过了,但没有用。我想我需要一个 Cocoa 方法。
    • 缺少的成分是将 com.apple.applescript.text-object 或 com.apple.cocoa.string 放入“Automator Action 接受的输入类型”区域(使用 XCode 5.1.1)一般下。谢谢!
    • 很高兴它为您工作。很难找到有关创建自动化操作的帮助,很高兴将这一点知识传递给另一个人。 (我在堆栈上仍有一个未回答的活跃 Automator Action 问题。)
    • 是的,我知道几乎没有什么是最新的!甚至 Shane Stanley 也没有创​​建 Automator 动作。我可以建议下次链接到您未回答的问题。这次我会为你做的:stackoverflow.com/questions/23920160/…
    猜你喜欢
    • 2011-12-28
    • 2021-10-28
    • 1970-01-01
    • 2017-04-16
    • 2016-09-08
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 2015-01-12
    相关资源
    最近更新 更多