Applescript没有用户输入执行

时间:2017-09-26 23:41:51

标签: applescript

我有一个简单的Applescript,它输入一个电话号码和一条标准信息。当我硬编码电话号码时,脚本按预期运行。我试图扩展它以接受用户输入,它现在提供了一个工作流程,但消息不是按键。 它在Automator窗口中运行时按预期工作,但这是它工作的唯一地方。

结束目标:能够在任何应用程序中运行此Applescript,并且消息是按键。

set phoneNumber to "error"
set contactName to (choose from list {"John", "Bob", "Jill"} with prompt "Who do you want to contact?") as string

if contactName is "John" then
    set phoneNumber to "(310) 213-1234"
else if contactName is "Bob" then
    set phoneNumber to "(213) 123-1234"
else if contactName is "Jill" then
    set phoneNumber to "(424) 456-7890"
end if

set theMessage to "Hi, please contact " & contactName & " at: " & phoneNumber & return & "Thank you."
tell application "System Events"
    keystroke theMessage
end tell

Screenshot of workflow

1 个答案:

答案 0 :(得分:0)

以下是我可以在运行AppleScript 操作中对其进行编码的示例,作为 Automator服务

on run {input, parameters}

    tell application "System Events"
        --  # Get the name of the App that has focus when this Service is triggered.
        set activeApp to name of first application process whose frontmost is true
        delay 0.5 
    end tell

    tell current application
        activate -- # Set focus back to me so the list box is frontmost.
        set contactName to (choose from list {"John", "Bob", "Jill"} with prompt "Who do you want to contact?") as string
        if return is false then return --   # User clicked Cancel or pressed Esc, exit the run handler.
        if contactName is "John" then
            set phoneNumber to "(310) 213-1234"
        else if contactName is "Bob" then
            set phoneNumber to "(213) 123-1234"
        else if contactName is "Jill" then
            set phoneNumber to "(424) 456-7890"
        end if
        set the clipboard to "Hi, please contact " & contactName & " at: " & phoneNumber & linefeed & "Thank you."
    end tell

    --  # Set focus back to the App that was frontmost when the Service was triggered.
    tell application activeApp
        activate
        delay 1 --  # Make sure the App has come frontmost before pasting what's on the clipboard.
        tell application "System Events"
            try
                -- # Insure the front window is indeed active.
                perform action "AXRaise" of window 1 of application process activeApp
                delay 0.2
            end try
            try
                --  # Paste from the clipboard.
                keystroke "v" using command down
                delay 0.2
            end try
        end tell
    end tell

    --  # Clear the clipboard.
    tell current application to set the clipboard to ""

end run

注意: delay 命令可能是必需的,也可能不是,或者可能需要为您的系统调整。您可以随时删除那些您不需要的东西,但我觉得它们位于战略位置并将其留在。