Need Safari Script Help

It has been over 10 years since I last used AppleScript. I was pretty good at it then, but I am amazed at how much I have forgotten. I know it is like learning to ride a bicycle – you never really forget, you just have to be reminded about how it works. Can anyone point me to a sample script that does the following…

  1. Open Safari (I have already figured out how to do this)
  2. Go to a specific URL (by name)
  3. Click on a specific link on the resulting web page (the link is aways named the same)
  4. Increase the size of the text on the resulting page being viewed.

I do not want anyone to write the script for me, just point me to examples for steps 2, 3 and 4.

I need to create a number of scripts for my mother, who has Macular Degeneration and cannot see well, but insists, at 82, on using a Mac. With the above example, I can jump back into AppleScript and help her. Her usage will be relatively limited, but this script will let her view the obituaries in the online newspaper from the town she came from.

Model: iMac G5
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

For step 2, I suggest you look in the Dictionary. Choose file-open dictionary and choose standard additions. The item “Internet suit” will tell you how to acess safari web pages. Using the URL thing, you won’t need step one either. :smiley:

OK, thanks for the response.

I have compressed steps 1, 2 and 3 into:

open location “my URL”

But I cannot figure out how to tell Safari to make the text bigger.

Hi,

It’s hard not to write the script when showing examples. Here’s an example that should load htis site’s front page and open the link for this board.


property link_name : "AppleScript | OS X"
tell application "Safari"
	launch
	activate
	open location "http://macscripter.net"
	if not (my page_loaded(20)) then return -- could use repeat loop to try again
	set last_link to (do JavaScript "document.links.length" in document 1) - 1
	set link_found to false
	repeat with i from 0 to last_link
		set this_name to do JavaScript "document.links[" & i & "].text" in document 1
		if this_name is link_name then
			set link_found to true
			exit repeat
		end if
	end repeat
	if not link_found then return
	set the_url to do JavaScript "document.links[" & i & "].href" in document 1
	open location the_url
	if not (my page_loaded(20)) then return
end tell
tell application "System Events"
	tell process "Safari"
		repeat 2 times
			keystroke "+" with command down
		end repeat
	end tell
end tell
--
-- Apple's timeout handler
-- "http://www.apple.com/applescript/safari/jscript.01.html"
on page_loaded(timeout_value)
	delay 2
	repeat with i from 1 to the timeout_value
		tell application "Safari"
			if (do JavaScript "document.readyState" in document 1) is "complete" then
				return true
			else if i is the timeout_value then
				return false
			else
				delay 1
			end if
		end tell
	end repeat
	return false
end page_loaded

To find out more about javascripting Safari:

http://www.apple.com/applescript/safari/jscript.01.html

Edited: I forgot to change this line:

keystroke “+” with command down

to:

keystroke “+” using command down

gl,

I tried your example, and the size did not increase.

Here is the destination URL: http://www.advertiser-tribune.com/obituaries/sections.asp?section=Obituaries

What am I missing?

Bill

Hi

Try this shorter version of kel’s script. Does it work for you?


tell application "Safari"
	launch
	activate
	open location "http://bbs.applescript.net/viewtopic.php?pid=68119#p68119"
	if not (my page_loaded(20)) then return -- could use repeat loop to try again
end tell
tell application "System Events"
	tell process "Safari"
		repeat 2 times
			keystroke "+" using command down
		end repeat
	end tell
end tell
--
-- Apple's timeout handler
-- "http://www.apple.com/applescript/safari/jscript.01.html"
on page_loaded(timeout_value)
	delay 2
	repeat with i from 1 to the timeout_value
		tell application "Safari"
			if (do JavaScript "document.readyState" in document 1) is "complete" then
				return true
			else if i is the timeout_value then
				return false
			else
				delay 1
			end if
		end tell
	end repeat
	return false
end page_loaded

capitalj,

Your example worked. I took it one step further and did this:

open location "http://www.advertiser-tribune.com/obituaries/sections.asp?section=Obituaries"
tell application "Safari"
	if not (my page_loaded(20)) then return -- could use repeat loop to try again
end tell
tell application "System Events"
	tell process "Safari"
		repeat 3 times
			keystroke "+" using command down
		end repeat
	end tell
end tell
--
-- Apple's timeout handler
-- "http://www.apple.com/applescript/safari/jscript.01.html"
on page_loaded(timeout_value)
	delay 2
	repeat with i from 1 to the timeout_value
		tell application "Safari"
			if (do JavaScript "document.readyState" in document 1) is "complete" then
				return true
			else if i is the timeout_value then
				return false
			else
				delay 1
			end if
		end tell
	end repeat
	return false
end page_loaded

The above opens the URL without going to the Safari home page first.

Thanks very much!!!

Bill

The script works fine on my machine, but gets an error on Mom’s.

She has OS 10.2.8, Safari 1.0.3 and Applescript 1.9.1

The “Keystroke” command fails. What can I use in its place given her system?

I would prefer not to have to upgrade her system if possible.

Bill

Maybe a long-time scripter has the answer you’re looking for. I have no pre-Tiger experience.

That would be my suggestion (unless it’s completely impractical) because, beyond the obvious system improvements, it should make tech support calls/visits to Mom so much easier.

Hi,

In OS 10.2.8, you need to install System Events beta. You can install the beta here:

http://images.apple.com/applescript/GUI/UIscriptingbeta.sit

After installing the new System Events, you need to enable ui scirpting. The instructions for installing and enabling should be in the installation folder. Note that ui scirpting beta uses the ‘with’ parameter instead of the ‘using’ parameter in the ‘keystroke’ command. i.e.

keystroke “+” with command down

is used instead of:

keystroke “+” using command down

gl,