GUI Scripting

Ric

Joined
May 14, 2004
Messages
4,260
Reaction score
5
Once we have finished the AppleScript Studio and the AppleScript Tutorials we will have a look at GUI Scripting.

This enables us to 'Script' any Application, even if it is not AppleScript aware !!!!

First script we will be working on is a script to take a folder full of Digital Images ---> copy them to a new folder ---> open each one sample it to 600 pixels wide max ---> then open a new mail message ---> add some recipients ---> add a subject ---> add some body copy ---> then add just one picture to the email ---> this must be a Windows Friendly Attachment ---> then repeat sending the emails till the whole batch has been sent !

This cannot be done in 'Vanilla" AppleScript and it can't be done in Automator. The only way to do it all is in AppleScript using some GUI Scripting techniques...

...this will be part of our script.

menu bar 1
menu bar item "Edit" (4)
menu 1
menu item "Attachments" (17)
menu 1
menu item "Always Send Windows Friendly Attachments" (2)
 

Ric

Joined
May 14, 2004
Messages
4,260
Reaction score
5
We will need to do a few things before getting started...

Go to the System prefs pane, and select Universal Access, then turn on 'Enable access for assistive devices'.

Next download, either the Apple GUI Browser or/and PreFab UI Browser, the Apple one is free the PreFab one is worth getting and paying for if you do alot of this type of stuff, makes life easier.

Have a read of both sets of read me's and then we will do some GUI Scripting...

Links here PreFab UI Browser Apple GUI Scripting Page Apple GUI Browser
 

Attachments

  • assistive.jpg
    assistive.jpg
    125.3 KB · Views: 475

Ric

Joined
May 14, 2004
Messages
4,260
Reaction score
5
Here is a small script to give you a taster of what can be achieved...

Log out of www.mac-help.com and checked that you are being shown as a Guest, then quit out of Safari.

Open up Script Editor and paste in the script below, if you change the username and password to yours, then when you run the script, Safari should open the URL www.mac-help.com is opened and you are logged in via the AppleScript !

Saves a bit of work...

Remember though, if someone else can access your computer and can access this script, they will be able to log in as you.

Script below:

--GUI Script example


tell application "Safari"
activate
open location "https://www.mac-help.com"
end tell
tell application "System Events"
delay 18
keystroke tab
delay 1
keystroke "myUsername"
keystroke tab
delay 1
keystroke "myPassword"
delay 1
keystroke return
end tell


--if you clear your cookies regularly this script can help...change the mysername and myPassword and web address to suit your needs. Depending on the speed of your Mac you may need to make the delays a bit larger.
 

Ric

Joined
May 14, 2004
Messages
4,260
Reaction score
5
Depending on the speed of your Mac and Internet connection, you can change the delay figure to what suits.

Mine is currently set at 3 and then 1 and then 1.
 

Ric

Joined
May 14, 2004
Messages
4,260
Reaction score
5
Another query and answer from Apple Discussions:

How to set the Screen Brightness to 50%, set mail to offline and turn off your wireless card...

Well after a little trial and error here are the scripts. I'm sure given the time they coild be a lot neater, and more efficient, but they do the job !!!

Some GUI Scripting and some not !

Script 1
Code:
--first we will turn on the Wireless Card

activate application "Internet Connect"
tell application "System Events"
	tell process "Internet Connect"
		
		-- GUI Scripting statements: the try is a cheats way of not checking to see if it can be done !
		try
			click button "AirPort" of tool bar 1 of window "Summary"
		end try
		
		try
			click button "AirPort" of tool bar 1 of window "Bluetooth"
		end try
		
		try
			click button "AirPort" of tool bar 1 of window "Internal Modem"
		end try
		
		
		delay 2
		
		try
			click button "Turn AirPort On" of window "AirPort"
		end try
		
		try
			click button 1 of window "AirPort"
		end try
		
	end tell
end tell

tell application "Internet Connect"
	quit
end tell

activate application "System Preferences"
tell application "System Events"
	tell process "System Preferences"
		-- GUI Scripting statements:
		
		delay 2
		
		click button "Displays" of scroll area 1 of window "System Preferences"
		
		delay 2
		
		try
			set value of slider 1 of group 2 of tab group 1 of window "Color LCD" to 1
		end try
		
	end tell
end tell

delay 1

tell application "System Preferences"
	quit
end tell

--last we will set Mail to be online

activate application "Mail"
tell application "System Events"
	tell process "Mail"
		-- GUI Scripting statements:
		
		delay 2
		try
			click menu item "Go Online" of menu 1 of menu bar item "Mailbox" of menu bar 1
		end try
		
	end tell
end tell

---Ric @ www.mac-help.com
 

Ric

Joined
May 14, 2004
Messages
4,260
Reaction score
5
And the second script:

Code:
--first we will set Mail to be offline

activate application "Mail"
tell application "System Events"
	tell process "Mail"
		-- GUI Scripting statements:
		
		delay 2
		
		click menu item "Go Offline" of menu 1 of menu bar item "Mailbox" of menu bar 1
		
	end tell
end tell

--next we will turn off the Wireless Card

activate application "Internet Connect"
tell application "System Events"
	tell process "Internet Connect"
		
		-- GUI Scripting statements: the try is a cheats way of not checking to see if it can be done !
		try
			click button "AirPort" of tool bar 1 of window "Summary"
		end try
		
		try
			click button "AirPort" of tool bar 1 of window "Bluetooth"
		end try
		
		try
			click button "AirPort" of tool bar 1 of window "Internal Modem"
		end try
		
		
		delay 2
		
		try
			click button "Turn AirPort Off" of window "AirPort"
		end try
		
		try
			click button 1 of window "AirPort"
		end try
		
	end tell
end tell

tell application "Internet Connect"
	quit
end tell

activate application "System Preferences"
tell application "System Events"
	tell process "System Preferences"
		-- GUI Scripting statements:
		
		delay 2
		
		click button "Displays" of scroll area 1 of window "System Preferences"
		
		delay 2
		
		try
			set value of slider 1 of group 2 of tab group 1 of window "Color LCD" to 0.5
		end try
		
	end tell
end tell

delay 1

tell application "System Preferences"
	quit
end tell

---Ric @ www.mac-help.com


Scripts are attached below...

regards

Ric
 

Attachments

  • Airport off-Brightness half-Mail offline 2.zip
    4.6 KB · Views: 328
  • Airport off-Brightness half-Mail offline.zip
    24.8 KB · Views: 336
  • Airport on-Brightness full-Mail online 2.zip
    4.7 KB · Views: 329
  • Airport on-Brightness full-Mail online.zip
    24.7 KB · Views: 335

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top