【问题标题】:Problem with pasting粘贴问题
【发布时间】:2011-09-27 18:39:03
【问题描述】:

我正在尝试编写一个苹果脚本来搜索 Sparrow(Mac 的邮件客户端)

这是脚本:

 on run argv

    tell application "Sparrow"
        activate
    end tell

    tell application "System Events"
        key code 3 using {option down, command down}
        keystroke argv
    end tell
end run

问题是我希望脚本在运行时接受一个参数,以便我可以向它提供要搜索的内容,但我无法将其粘贴出来。

【问题讨论】:

    标签: applescript


    【解决方案1】:
    1. argv 始终初始化为一个列表。
    2. 您不能击键列表(您必须先将每个项目强制转换为字符串)。
    3. 您永远无法知道将发送到脚本的参数的确切数量,因此更好的方法是遍历列表并做任何需要做的事情,如下所示:

      tell application "System Events"
          tell process "Sparrow"
              key code 3 using {command down, option down}
              repeat with this_item in argv
                  keystroke (this_item as string)
              end repeat
          end tell
      end tell
      

    @Runar

    1. 脚本暗示 Sparrow 已被激活。
    2. 你不能像写的那样做(every text item of argv 的结果仍然是一个列表)。但是,如果您将结果强制转换为字符串,这将起作用,但它会将所有内容压缩在一起(假设 AppleScript's text item delimiters"")。如果你set AppleScript's text item delimiters to space,那么这实际上会比之前的脚本更好......

      on run argv
          tell application "Sparrow" to activate
          tell application "System Events"
              tell process "Sparrow" --implying Sparrow is already activated
                  set prevTIDs to AppleScript's text item delimiters
                  key code 3 using {command down, option down}
                  set AppleScript's text item delimiters to space
                  keystroke (every text item of argv) as string
                  set AppleScript's text item delimiters to prevTIDs
              end tell
          end tell
      end run
      

    【讨论】:

    • 好的。您的脚本没有激活 sparrow,因此密钥代码不会发送到正确的应用程序。我可以做“击键(每个项目都变坏)吗?
    • 谢谢。现在看起来不错。但是脚本需要 s 来激活 Sparrow。脚本的重点是从 Alfred 启动它。
    • 非常感谢!奇迹般有效。疯狂适合扩展并认为您是编码器。希望你不要介意。
    猜你喜欢
    • 1970-01-01
    • 2013-12-27
    • 1970-01-01
    • 2023-01-07
    • 1970-01-01
    • 1970-01-01
    • 2010-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多