Applescript handling of buttons in dialog boxes

Discussion in 'Mac Programming' started by petron, Oct 25, 2010.

  1. macrumors member

    Joined:
    May 22, 2009
    Location:
    Malmo, Sweden
    #1
    Hi folks,

    I started to write a script that imports Vcards into the Address Book.

    The problem I have right now is how to handle the dialog box's buttons.
    As long as I want to simulate the default button all works fine.


    Code:
    #open vcf file
    tell application "Address Book"
    	activate
    	open "Users:minpc:Desktop:ha.vcf"
    	tell application "System Events"
    		keystroke return
    	end tell
    end tell
    
    How can I specify to activate different button then the default one ?

    b.r.
    /petron
     
  2. macrumors 65816

    Joined:
    Mar 13, 2006
    #2
    there are two solutions I see instantly.

    One would be to press number the correct number of times and the other would be to use the keyboard shortcut for the desired button
     
  3. macrumors member

    ShinyDren

    Joined:
    Jun 25, 2010
    Location:
    Ambitoysterous, USA
    #3
    You can use code such as this (it also works with buttons):
    "tell radio group 1 of scroll area 2 of group 1 of splitter group 3 of window 1"

    The easiest way to get the exact info you need is by using a program called "UI Browser". It will create the exact code for you, and the installer includes some sample code scripts.
    http://pfiddlesoft.com/uibrowser/
    It comes with a 30 day free trial, so you can test it to see if it helps you out.

    I think you have to have "Enable access for assistive devices" enabled in the Universal Access System Preference, but it has been a while so it might not be necessary. :eek:

    Good luck
     
  4. thread starter macrumors member

    Joined:
    May 22, 2009
    Location:
    Malmo, Sweden
    #4
    Thanks for input,
    I tried to use a short cut command and it works fine.
    /petron
     
  5. macrumors 6502a

    Joined:
    Oct 13, 2007
    #5
    GUI scripting should only be used as a last resort (unless you are specifically doing Q/A on the software... and I assume you are not doing that for AddressBook).

    AddressBook provides a nice dictionary that you can use to access and manipulate the contents of the data store very nicely. To view it open the AppleScript Editor and select "Open Dictionary" from the File menu.

    There is even a MacTech article on it (just a google away):

    http://www.mactech.com/articles/mactech/Vol.21/21.10/ScriptingAddressBook/index.html
     

Share This Page