Problem with pasting
I'm trying to write an apple scrip to search Sparrow (mail client for Mac)
Here is the script:
on run argv
tell application "Sparrow"
activate
end tell
tell application "Sys开发者_开发技巧tem Events"
key code 3 using {option down, command down}
keystroke argv
end tell
end run
The problem is that I want the script to take an argument on run so that I can supply it with what to search for, but I can't get it to pastet it out.
argv
is always initialized to a list.- You cannot keystroke a list (you have to coerce each item to a string first).
You can never tell the exact number of parameters that will be sent to the script, so a better route would be to iterate through the list and do whatever needs to be done, as shown below:
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
- The script is implying that Sparrow is already activated.
You can't do this as written (the result of
every text item of argv
is still a list). However, if you coerce the result into a string, this will work, but it will squash everything together (assumingAppleScript's text item delimiters
is""
). If youset AppleScript's text item delimiters to space
, then this would actually be better than the previous script...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
精彩评论