AppleScript and the Internet

iMac
iMac | Source

If you have searched the internet for AppleScripts that relate to interaction with web browsers or email sites, you have probably come up empty, as I did. Here I will feature AppleScript in this distinct niche. If, however, you have an interest in other AppleScripts, please visit: AppleScript for Beginners

Firefox: Open Image as Web Page, 3 Scripts

As the title suggests, this is article contains 3 scripts for opening an image in Firefox. The scripts are all very similar, but they are variants of each other for processing images in different ways.

In the first, I show you how to choose a file and open open it by means of a dialog. The second and third, are droplet-style scripts and should be saved as applications.

Choosing an Image from a 'choose from list':

on run
	set saveDelimiters to text item delimiters of AppleScript
	set desktopLocation to path to desktop as alias
	tell application "Finder"
		activate
		open desktopLocation
		set targetImage to (choose file "Select an image file:" default location desktopLocation)
		set imageInfo to (get properties of targetImage)
		set imageURL to (URL of imageInfo)
		tell application "Firefox"
			activate
			open location imageURL
		end tell
	end tell
	set text item delimiters of AppleScript to saveDelimiters
end run

Droplet to Open a Single Image:

on open targetImage
	set saveDelimiters to text item delimiters of AppleScript
	tell application "Finder"
		activate
		set targetImage to (targetImage as alias)
		set imageInfo to (get properties of targetImage)
		set imageURL to (URL of imageInfo)
		tell application "Firefox"
			activate
			open location imageURL
		end tell
	end tell
	set text item delimiters of AppleScript to saveDelimiters
end open

Droplet to Open a Two or More Images:

on open (imageList)
	set saveDelimiters to text item delimiters of AppleScript
	tell application "Finder"
		activate
		repeat with targetImage in imageList
			set targetImage to (targetImage as alias)
			set imageInfo to (get properties of targetImage)
			set imageURL to (URL of imageInfo)
			tell application "Firefox"
				activate
				open location imageURL
			end tell
		end repeat
	end tell
	set text item delimiters of AppleScript to saveDelimiters
end open
Google Chrome
Google Chrome | Source

Get Page Source with Google Chrome

Save Page Source to AppleWorks 6

A useful script for saving your blog html and blog page title to a text document for safe keeping and closes the opened html tab. This one saves the data to AppleWorks but it could be adapted for use with just about any text application that is scriptable, including TextEdit and Microsoft Word:

tell application "Google Chrome"
	set windowName to (title of window 1) as string
	view source of active tab of window 1
	delay 3
	repeat while loading of active tab of window 1
		delay 3
	end repeat
	select all of active tab of window 1
	copy selection of active tab of window 1
	delay 10 --need this delay to copy to clipboard
	delete tab (active tab index of window 1) of window 1
end tell
tell application "AppleWorks 6"
	activate
	try
		set saveDelimiters to AppleScript's text item delimiters
		set AppleScript's text item delimiters to ":"
		set windowName to text item 2 of windowName
		make new document with properties {name:windowName}
		paste text
		tell document 1
			select every text
			set properties of selection to {font:"Verdana", size:14}
		end tell
		set AppleScript's text item delimiters to saveDelimiters
	on error errorMsg
		display dialog errorMsg
	end try
end tell

This script is pretty easy, especially the block using AppleWorks which can be found elsewhere. You may notice that scripting Chrome is structured much like scripting Word 2008, with a lot references referring back to the 'active document'.

Source

Using Google Maps to Find an Address from FileMaker Pro

A Handy Script that Automates Google Maps It can be enhanced with a 'choose from list' dialog for frequent address searches or (as you'll see below) you can enhance the script with a database program such as FileMaker Pro, to search for an address from a specific record.

Some notes below, but first the script:

try
	set findAddress to text returned of (display dialog "Enter address to find:" default answer "Your address here")
	exists application "Firefox"
	tell application "Firefox"
		activate
		delay 5
		open location "http://www.google.com/maps"
		delay 10
		tell application "System Events"
			keystroke findAddress
			keystroke return
		end tell
	end tell
on error
	display alert "No address has been entered." message "Enter required info and try again." as warning
end try

Remember, that for this to work correctly, GUI Scripting must be enabled.

Take note of the statement 'exists application "Firefox"'. I use this because it will activate Firefox and helps with the timing of the delays that follow, which still may need to be adjusted for slower internet access and the speed of your processor.

Between the exists statement and activate there should be enough time for Firefox to be ready to go before the rest of the script continues.

After the Google map page opens, System Events, which requires GUI Scripting, takes over and enters the address in the address field and sends the return command to send the request for the map. It's that simple!

This code below can be used with FileMaker Pro in place of the 'display dialog' text entry code at the beginning of the script.

tell application "FileMaker Pro"
	set findAddress to (get cell "Address" of current record) & " " & (get cell "City" of current record) & ", " & (get cell "State" of current record) & " " & (get cell "Zip Code" of current record)
	copy findAddress to (cell "googleAddress" of current record)
end tell
Apple Mail
Apple Mail | Source

Mail to Multiple Recipients with Attachment

A good start to getting acquainted with the syntax for scripting Mail. This is a script to send mail through Apple's Mail App and optionally to multiple recipients and append an attachment to the email.

Copy this script and paste it into Script Editor:

tell application "Mail"
activate
set mailSubject to text returned of (display dialog "Subject line for new message" default answer "")
set theMessage to make new outgoing message with properties {visible:true, subject:mailSubject, content:"Check this out !"}
-->outgoing message id 3 of application "Mail"
set attachFile to (choose file "Select an attachment file:" of type {"jpg", "gif", "png"})
-->"MacintoshHD:Users:userAccount:Desktop:Airport-Extreme.png"
tell theMessage
repeat
set messageName to text returned of (display dialog "Enter a name for new message" default answer "")
set messageRecipient to text returned of (display dialog "Enter an email address for new message" default answer "")
if messageRecipient = "" then exit repeat
make new to recipient at end of to recipients with properties {name:messageName, address:messageRecipient}
end repeat
end tell
tell content of theMessage
make new attachment with properties {file name:attachFile} at after last paragraph
send theMessage
end tell
end tell

This is pretty straightforward and doesn't require further explanation. A few Script editor results are commented above. The Mail window looks something like this:

Please leave questions or comments at the bottom of this page in 'Comments on AppleScript and the Internet'

Convert AppleScript from Markup Code to Text

A structured script to Extract Script Text

If you follow my notes inserted within the script, you'll see that this is a script to eliminate markup code and replace it with the actual characters it is supposed to represent...

tell application "AppleWorks 6"
	set saveDelimiters to AppleScript's text item delimiters
	tell document 1
		select every text
		set editText to the selection
		--These first two instances eliminate most of the non-AppleScript text
		set AppleScript's text item delimiters to ">"
		set editText to text item 1 of editText
		set AppleScript's text item delimiters to "="
		set editText to text items 2 thru 4 of editText
		set AppleScript's text item delimiters to ","
		set editText to text item 3 of editText
		--This replaces %20 with spaces
		set AppleScript's text item delimiters to "%20"
		set textEditLength to (count text items in editText) as string
		set textEditString to text item 1 of editText
		repeat with x from 2 to textEditLength
			set textEditString to textEditString & space & (text item x of editText)
		end repeat
		--This replaces %22 with quotes
		set AppleScript's text item delimiters to "%22"
		set textEditLength to (count text items in textEditString) as string
		set textEditString2 to text item 1 of textEditString
		repeat with x from 2 to textEditLength
			set textEditString2 to textEditString2 & quote & (text item x of textEditString)
		end repeat
		--This replaces %3A with a colon
		set AppleScript's text item delimiters to "%3A"
		set textEditLength to (count text items in textEditString2) as string
		set textEditString3 to text item 1 of textEditString2
		repeat with x from 2 to textEditLength
			set textEditString3 to textEditString3 & ":" & (text item x of textEditString2)
		end repeat
		--This replaces %2F with forward slashes
		set AppleScript's text item delimiters to "%2F"
		set textEditLength to (count text items in textEditString3) as string
		set textEditString4 to text item 1 of textEditString3
		repeat with x from 2 to textEditLength
			set textEditString4 to textEditString4 & "/" & (text item x of textEditString3)
		end repeat
		--This replaces % 0D with returns
		set AppleScript's text item delimiters to "%0D"
		set textEditLength to (count text items in textEditString4) as string
		set textEditString5 to text item 1 of textEditString4
		repeat with x from 2 to textEditLength
			set textEditString5 to textEditString5 & return & (text item x of textEditString4)
		end repeat
		set AppleScript's text item delimiters to "%09"
		set textEditLength to (count text items in textEditString5) as string
		set textEditString6 to text item 1 of textEditString5
		repeat with x from 2 to textEditLength
			set textEditString6 to textEditString6 & space & (text item x of textEditString5)
		end repeat
		set textEditString6 to text 1 thru -2 of textEditString6
		display dialog textEditString6
	end tell
	set AppleScript's text item delimiters to saveDelimiters
end tell
tell application "Safari"
	activate
	set URLName to name of current tab of window 1 --YouTube - Broadcast Yourself.
	set theURL to URL of current tab of window 1 --http://www.youtube.com/
	set theHTML to source of current tab of window 1
end tell

tell application "TextEdit"
	activate
	set text of document 1 to URLName & return & return & theURL & return & return & theHTML
end tell

A script to send data to Apple's Mail application

There isn't really anything difficult here. It should suffice to note the AppleScript terms and expressions used here that are unique to the Mail application. You'll often find that an application has, along with the standard terms that are used by all applications, some that are specific to the application and therefore must be enclosed in a tell block that directs them to the app you're using.

display dialog "Enter recipient:" with icon 1 default answer "yourEmail@domain.com" buttons {"Cancel", "OK"} default button 2
set theRecipient to text returned of the result
display dialog "Enter subject of email:" with icon 1 default answer "Re: New Subject" buttons {"Cancel", "OK"} default button 2
set theSubject to text returned of the result
display dialog "Enter your email message here:" with icon 1 default answer "This is the text of your email message" buttons {"Cancel", "Send"} default button 2
set emailText to text returned of the result

tell application "Mail"
	activate
	set theMessage to make new outgoing message
	tell theMessage
		set the subject to theSubject
		set the content to emailText
		make new to recipient at end of to recipients with properties {address:theRecipient}
	end tell
	save theMessage
end tell

The statement: 'set theMessage to make new outgoing message' sets things up so that the following commands in the tell block can be directed to the variable theMessage as an object.

The next two statements set the values of the subject and content fields to the variables theSubject and emailText respectively.

Next, the value of the Mail property address is set to the value of the variable theRecipient.

Finally, the save command is directed to the Mail application, since it cannot be handled by theMessage as it is, in effect, a document.

Save Page Source to AppleWorks 6

A useful script for saving your blog html and blog page title to a text document for safe keeping and closes the opened html tab. This one saves the data to AppleWorks but it could be adapted for use with just about any text application that is scriptable, including TextEdit and Microsoft Word.

Copy this code into the AppleScript Editor:

tell application "Google Chrome"
set windowName to (title of window 1) as string
	view source of active tab of window 1
	delay 3
	repeat while loading of active tab of window 1
			delay 3
		end repeat
	select all of active tab of window 1
	copy selection of active tab of window 1
	delay 10 --need this delay to copy to clipboard
	delete tab (active tab index of window 1) of window 1
end tell

tell application "AppleWorks 6"
	activate
	try
			set saveDelimiters to AppleScript's text item delimiters
			set AppleScript's text item delimiters to ":"
			set windowName to text item 2 of windowName
			make new document with properties {name:windowName}
			paste text
			tell document 1
					select every text
					set properties of selection to {font:"Verdana", size:14}
				end tell
			set AppleScript's text item delimiters to saveDelimiters
		on error errorMsg
			display dialog errorMsg
		end try
end tell

This script is very straightforward, especially the block using AppleWorks which can be found elsewhere. You may notice that scripting Chrome is structured much like scripting Word 2008, with a lot references referring back to the 'active document'.

Firefox
Firefox | Source

Script to Open Gmail Inbox in Firefox

This is a quick and easy way to go to your Gmail Inbox

If already logged in, it goes straight to Gmail and opens your inbox. If you want instant login (even if you aren't already logged in), you can edit this script by placing your Gmail address and password into the appropriate dialog prompts of the script below.

This script assumes that you have an email account set up through Gmail:

try
	set isLoggedIn to button returned of (display dialog "Have you already logged in  to your email account for this session?" buttons {"Yes", "No"} default button 2 with icon 2)
	if isLoggedIn = "No" then
		set theEmail to text returned of (display dialog "Enter your email account  here:" default answer "yourEmail@gmail.com")
		set thePassword to text returned of (display dialog "Enter your password here:" default answer "" with hidden answer)
		if theEmail = "" or thePassword = "" then error
	end if
	exists application "Firefox"
	tell application "Firefox"
		activate
		delay 3
		if isLoggedIn = "No" then
			open location "https://www.google.com/accounts/ServiceLogin?"
			delay 10
			tell application "System Events"
				keystroke theEmail
				keystroke tab
				keystroke thePassword
				keystroke return
			end tell
		end if
		open location "http://mail.google.com/mail/#inbox"
	end tell
on error
	display alert "Either your email or password  have not been entered." message "Please enter  the required info and try again." as warning
end try

AppleScript and the Internet

4 stars for AppleScript and the Internet

© 2013 AppleScripter

More by this Author

  • More Advanced AppleScript Code
    0

    For those of you who still cannot get enough AppleScript to feed your fix, some more free applescript code,Finder,Microsoft Word,Pages,AppleWorks,PowerPoint,TextEdit,iTunes,POSIX paths,database events

  • Free AppleScript Code Examples
    1

    Write AppleScript Code,free AppleScripts of display dialog,display alert,choose from list,finder,choose file,choose file name,choose folder,choose application,choose color,Finder,TextEdit

  • AppleScript and FileMaker
    4

    Learning How to Write FileMaker Pro Scripts for Beginners. Free AppleScripts for those who are new. Many scripts are a mix of AppleScript and FileMaker.


Comments on AppleScript and the Internet

No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working