如何将输入__NSArrayM转换为Automator Action项目中的AppleScript列表?

时间:2014-05-31 12:58:34

标签: applescript automator applescript-objc

Automator Action

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

要点:

  • 将__NSArrayM 输入转换为AppleScript列表对象
  • 将AppleScript列表对象转换回__NSArrayM以获取输出

我希望我的自动取款机看起来像这样: enter image description here

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

最终代码更新

我以为我会将最终代码发布到我的automator动作以获得完整性。关键步骤是输入和输出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

inputautomator

1 个答案:

答案 0 :(得分:1)

脚本的目标是采用一组文本字符串,并根据menuSelection更改其大小写?

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

set inputValues to input as list

主要是计划接收单个文本输入吗?然后转到Target / General,Input和Output选项: com.apple.applescript.text对象 要么 com.apple.cocoa.string 这些将是传递的数组项的类。