Need Help With Apple Script

Discussion in 'Mac Programming' started by dididan2000, Mar 10, 2014.

  1. dididan2000 macrumors newbie

    Joined:
    Mar 10, 2014
    #1
    I am trying to make a script so when you run it, it asks you if you would like to go on Twitter. If you say yes, a new message pops up. The problem I am having is the next message that asks you what you will tweet, so you put in the information. Then you press done. The problem is when you press done, I am getting an error that says "Can't get buttoned returned of {whatever message I put into the text box.} The next thing is that, if somebody helps me with this part, how do I get the text entered to be put into the actual "Compose New Tweet" box on Twitter. This works great if I do not have the second message, but you have to manually put in your message in the script on the bottom, which I do not want. Please, somebody help me because I really want this to work and this will be the first pretty cool thing I have made with an Apple Script. Thanks!

    Here is my code:
    Code:
    say "Would you like to go on Twitter and say hello?"
    display dialog "Would you like to go on Twitter and say hello?" buttons {"Yes", "No"}
    set button_r to the button returned of the result
    
    if the button_r is "yes" then
    	say "What would you like to tweet?"
    	set theName to the text returned of (display dialog "What would you like to tweet?" default answer "" buttons {"Done", "Nevermind"})
    	set button_w to the button returned of the result
    	
    	if the button_w is "Done" then
    		tell application "Safari"
    			activate
    		end tell
    		delay 1
    		tell application "Safari"
    			open location "https://www.twitter.com"
    		end tell
    		delay 1.5
    		tell application "System Events"
    			tell process "Safari"
    				tell window 1
    					tell group 2
    						tell group 1
    							tell group 1
    								tell scroll area 1
    									tell UI element 1
    										tell group 3
    											tell group 2
    												tell list 1
    													tell group 3
    														click button 1
    													end tell
    												end tell
    											end tell
    										end tell
    									end tell
    								end tell
    							end tell
    						end tell
    					end tell
    				end tell
    			end tell
    		end tell
    		tell application "System Events"
    			display dialog theName
    			keystroke "(Your Message)"
    		end tell
    		delay 1
    		tell application "System Events"
    			tell process "Safari"
    				tell window 1
    					tell group 2
    						tell group 1
    							tell group 1
    								tell scroll area 1
    									tell UI element 1
    										tell group 20
    											tell group 1
    												tell toolbar 1
    													tell group 2
    														click button 1
    													end tell
    												end tell
    											end tell
    										end tell
    									end tell
    								end tell
    							end tell
    						end tell
    					end tell
    				end tell
    			end tell
    		end tell
    	end if
    	
    	if the button_r is "No" then
    	end if
    	
    	if the button_w is "Nevermind" then
    	end if
    	
    end if
     
  2. kryten2, Mar 10, 2014
    Last edited: Mar 10, 2014

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    You fell into the trap of the special result property. Can't help you with the Twitter part cause I don't use it. A simplified version of your script :

    Code:
    set button_r to the button returned of (display dialog "Would you like to go on Twitter and say hello?" buttons {"Yes", "No"})
    
    if the button_r is "yes" then
    	set {theName, button_w} to the {text returned, button returned} of (display dialog "What would you like to tweet?" default answer "" buttons {"Done", "Nevermind"})
    	if the button_w is "Done" then
    		-- Do your stuff here with Safari
    		
    	end if
    end if
    
    You can also do something like :

    Code:
    click button 1 of group 3 of list 1 of group 2 ...
    From the AppleScript Language Guide

     
  3. DennisBlah macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #3
    I also can't help you much with twitter, also I don't use it.
    But anyways, you are trying to 'automize' an website.
    The easiest way to do this is by injecting javascript and/or jquery script into it.

    Here I have a small example:

    Code:
    --set this jquery file to an local jquery file. (You can also grab one from the google code site, but that will take some url fetching and stuff..
    --set this jquery file to an local jquery file. (You can also grab one from the google code site, but that will take some url fetching and stuff..
    set jqueryFile to ("/Users/Dennis/Desktop/jquery-2.0.3.js")
    set jqueryContents to (read jqueryFile)
    
    global theWindow, theSource, theTab
    
    tell application "Safari"
    	activate
    	open location "https://twitter.com"
    	
    	(*Wait till the page is done loading...
    	You can also pass some html code in here, to see if the page is loaded and contains an element. (Like for an login check ;D)
    	But don't use an repeat until in the case you are checking for certain elements *)
    	repeat until waitTillSafe("") of me is true
    	end repeat
    	
    	--do an login check here first! This below is just for logging in...
    	
    	do JavaScript jqueryContents
    	do JavaScript "$('#signin-email').value = 'yourEmail@here.com'; 
    $('#signin-password').value = 'yourPassword';
    $('.submit.btn.primary-btn.flex-table-btn.js-submit').click();" in front document
    end tell
    
    (* waitTillSafe()
    	makes the script wait untill the page is finished loading :-) *)
    on waitTillSafe(aditionalCode)
    	set returnValue to false
    	
    	--Keep breathing
    	delay 0.2
    	tell application "Safari"
    		set theWindow to front window
    		set theTab to current tab of theWindow
    		set theSource to source of theTab
    		
    		try
    			if (theSource contains "</html>") then
    				if (aditionalCode is not "") then
    					if (theSource contains aditionalCode) then
    						set returnValue to true
    					end if
    				else
    					set returnValue to true
    				end if
    			end if
    		end try
    	end tell
    	return returnValue
    end waitTillSafe
    
    I hope it's gonna help you and inspires you a little bit.
    Ow btw, the easiest way to get the element id's and classes is using firefox with element inspector ;-)
     
  4. dididan2000 thread starter macrumors newbie

    Joined:
    Mar 10, 2014
    #4
    Ok thanks, I will try it!

    How do I get my local jquery file?
     

Share This Page