代码之家  ›  专栏  ›  技术社区  ›  uncletreetrunk

Applescript未使用用户输入执行

  •  1
  • uncletreetrunk  · 技术社区  · 7 年前

    我有一个简单的Applescript,可以输入电话号码和一条标准消息。当我硬编码电话号码时,脚本按预期运行。我试图扩展它以接受用户输入,现在它提供了一个已完成的工作流,但消息没有被击键。

    最终目标:能够在任何应用程序中运行此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 回复  |  直到 7 年前
        1
  •  0
  •   user3439894    7 年前

    这是一个 实例 如何在 行动 自动机服务 :

    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 可能需要也可能不需要和/或其 可能需要根据您的系统进行调整。你总是可以删除那些你发现你不需要的,但我觉得他们的战略位置,并将留在他们。