我正在尝试编写一个Apple脚本来搜索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
问题在于我希望脚本在运行时接受一个参数,这样我就可以为它提供搜索内容,但我无法将其粘贴出来。
答案 0 :(得分:2)
argv
始终初始化为列表。您永远无法确定将要发送到脚本的参数的确切数量,因此更好的方法是遍历列表并执行任何需要完成的操作,如下所示:
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
你不能这样做(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