自动化操作
我正在制作用于操作文本输入的自定义 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