开发者

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.


  1. argv is always initialized to a list.
  2. You cannot keystroke a list (you have to coerce each item to a string first).
  3. 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

  1. The script is implying that Sparrow is already activated.
  2. 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 (assuming AppleScript's text item delimiters is ""). If you set 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
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜