content: on run arguments
    set cwd to item 1 of arguments
    set saveas to item 2 of arguments
    set profile_name to item 3 of arguments
    set pkey_password to item 4 of arguments
  
  	do shell script ("rm -f " & cwd & saveas & ".p12")
  	do shell script ("rm -f " & cwd & saveas & ".cer")
  
    do shell script "open -a /Applications/Utilities/Keychain\\ Access.app $1"
  
    delay 1
  
    tell application "Keychain Access" to activate
  
    delay 1
  
    tell application "System Events"
    	tell process "Keychain Access"
    		select row 1 of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window "Keychain Access"
  
    		delay 1
  
    		select row 4 of outline 1 of scroll area 2 of splitter group 1 of splitter group 1 of window "Keychain Access"
  
    		delay 1
  
    		set hehe to outline 1 of scroll area 1 of splitter group 1 of window "Keychain Access"
  
    		-- Create private key
    		repeat with aRow in row of hehe
  
    			set hoho to value of text field 1 of first UI element of aRow
    			--set hoho to hoho2's first item
    			if hoho contains profile_name then
    				tell text field 1 of first UI element of aRow to perform action "AXShowMenu"
    				delay 1
  
    				repeat 4 times
    					key code 125 (* down arrow *)
    				end repeat
    				keystroke return (* confirm choice of menu item *)
    			end if
    		end repeat
  
    		delay 1
  
    		set shh to sheet 1 of window "Keychain Access"
    		set value of text field 1 of shh to "~"
        delay 2
        set value of text field 1 of sheet 1 of shh to ("" & cwd & saveas)
        delay 2
        click button "Go" of sheet 1 of shh
        delay 2
    		click pop up button 1 of group 1 of shh
    		delay 0.5
    		click (menu item 1 where its name starts with "Person") of menu 1 of pop up button 1 of group 1 of shh
    		delay 0.5
    		click button "Save" of shh
    		delay 1
  
    		tell application "System Events" to tell process "SecurityAgent"
    			set value of text field 1 of window 1 to pkey_password
    			set value of text field 2 of window 1 to pkey_password
    			delay 1
    			click button "OK" of window 1
    		end tell
  
    		-- Create cert
    		repeat with aRow in row of hehe
  
    			set hoho to value of text field 1 of first UI element of aRow
    			--set hoho to hoho2's first item
    			if hoho contains profile_name then
    				tell text field 1 of first UI element of aRow to perform action "AXShowMenu"
    				delay 1
  
    				repeat 4 times
    					key code 125 (* down arrow *)
    				end repeat
    				keystroke return (* confirm choice of menu item *)
    			end if
    		end repeat
  
    		delay 1
  
    		set shh to sheet 1 of window "Keychain Access"
    		set value of text field 1 of shh to saveas
    		click pop up button 1 of group 1 of shh
    		delay 0.5
    		click (menu item 1 where its name starts with "Cert") of menu 1 of pop up button 1 of group 1 of shh
    		delay 0.5
    		click button "Save" of shh
  
    	end tell
    end tell
  end run
  