0

自動化アクション

テキスト入力を操作するためのカスタム 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

入力オートメーション

4

1 に答える 1