Mac Need Help With Apple Script

dididan2000

macrumors newbie
Original poster
Mar 10, 2014
2
0
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
 

kryten2

macrumors 6502a
Mar 17, 2012
996
59
Belgium
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.
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

Results

The result of a statement is the value generated, if any, when the statement is executed. For example, executing the statement 3 + 4 results in the value 7. The result of the statement set myText to "keyboard" is the text object "keyboard". A result can be of any class. AppleScript stores the result in the globally available property result, described in “AppleScript Constant.”

result

When a statement is executed, AppleScript stores the resulting value, if any, in the predefined property result. The value remains there until another statement is executed that generates a value. Until a statement that yields a result is executed, the value of result is undefined. You can examine the result in Script Editor by looking in the Result pane of the script window.
 
Last edited:

DennisBlah

macrumors 6502
Dec 5, 2013
485
2
The Netherlands
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 ;-)
 

dididan2000

macrumors newbie
Original poster
Mar 10, 2014
2
0
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
Ok thanks, I will try it!

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 ;-)
How do I get my local jquery file?