Newbie Applescript question

I wonder if anyone can help with this Applescript question? I want to be able to click on an address in Contacts, copy the name and address (home, work, or other) from the context menu to the clipboard and then

  1. have Applescript open Pages
  2. insert the date flush right at the top of the file
  3. insert the name and address copied from Contacts flush left at least one line underneath the date
  4. reposition the cursor one the first blank line of the file flush left

I am using Pages 4.3

(A bonus would be to automtically name the file “lastname, firstname day month year” but that may be too tricky.)

Hi,

one solution for your request is a AddressBook plug-in

Quit Contacts.app
Save this code as normal script with name CreatePagesFile.scpt and put it into ~/Library/Address Book Plug-Ins


using terms from application "Contacts"
	on action property
		return "address"
	end action property
	
	on action title for this_person with this_entry
		return "Create Pages File"
	end action title
	
	on should enable action for this_person with this_entry
		return (this_entry is not missing value)
	end should enable action
	
	on perform action for this_person with this_entry
		tell me
			tell (current date)
				set currentDate to its date string
				set fileNameDateString to (its day as text) & (its month as integer) & year
			end tell
			set desktopFolder to path to desktop as text
		end tell
		
		set personName to name of this_person
		set addressStreet to street of this_entry
		set addressCity to zip of this_entry & space & city of this_entry
		
		set fileName to personName & "_" & fileNameDateString & ".pages"
		tell application "Pages"
			launch
			set currentDocument to make new document
			tell currentDocument
				set dateParagraph to make new paragraph
				set contents of dateParagraph to currentDate & return
				set alignment of dateParagraph to right
				set nameParagraph to make new paragraph at after last paragraph
				set alignment of nameParagraph to left
				set contents of nameParagraph to personName & return & addressStreet & return & addressCity & return
			end tell
			save currentDocument in file (desktopFolder & fileName)
		end tell
	end perform action
end using terms from


then relaunch Contacts.app, select an address of a person and choose Create Pages File from the contextual menu.
A Pages file will be created on desktop.

If you use different address format conventions you have to change the order in the line starting with


set contents of nameParagraph to .

Thanks so much! I am eager to try this when I get home.

What if one wanted to save this to iCloud? Is that also possible?

I haven’t tested it, but probably you have to save the files into ~/Library/Mobile Documents/com~apple~Pages/Documents

Also, if one wanted Pages to launch with a particular template, would one just include the template name after “launch”?

AppleScript looks like an amazingly powerful scripting language.

specify the template name in the make new document line


set currentDocument to make new document with properties {template name:"Woodland Envelope"}

If this actually works it will be a godsend. I wonder if it will also work in the new,crippled version of Pages.

Hmm. It does not seem to work. If Pages is not running, the Pages icon bounce up and down but never opens If Pages is already running nothing happens at all (though Contacts seems to unresponsive for several seconds). Any thoughts?

I tested it successfully on 10.8 with Pages 4.3

Interesting, I am running 10.9. When you ran it, Pages actually launched, had the date and address, and had focus? None of that here. I wonder if it could be mavericks?

Yes, everything works as expected on Mountain Lion. No focus is intended.
To move Pages to the front, insert a line

activate

after

launch

No joy in mudville. Maybe it is Mavericks. Here’s the script I’m using: yours with the addition of template info and “activate” after launch.

using terms from application “Contacts”
on action property
return “address”
end action property

on action title for this_person with this_entry
	return "Create Pages File"
end action title

on should enable action for this_person with this_entry
	return (this_entry is not missing value)
end should enable action

on perform action for this_person with this_entry
	tell me
		tell (current date)
			set currentDate to its date string
			set fileNameDateString to (its day as text) & (its month as integer) & year
		end tell
		set desktopFolder to path to desktop as text
	end tell
	
	set personName to name of this_person
	set addressStreet to street of this_entry
	set addressCity to zip of this_entry & space & city of this_entry
	
	set fileName to personName & "_" & fileNameDateString & ".pages"
	tell application "Pages"
		launch
		activate
		set currentDocument to make new document with properties {template name:"TNC small"}
		tell currentDocument
			set dateParagraph to make new paragraph
			set contents of dateParagraph to currentDate & return
			set alignment of dateParagraph to right
			set nameParagraph to make new paragraph at after last paragraph
			set alignment of nameParagraph to left
			set contents of nameParagraph to personName & return & addressStreet & return & addressCity & return
		end tell
		save currentDocument in file (desktopFolder & fileName)
	end tell
end perform action

end using terms from

After some research I figured out that Address Book Plug-Ins which send Apple Events to other applications don’t work anymore because Contacts.app is now sandboxed

Rats. I assume there is no way around that.

this is a standalone version, which uses the selection in Contacts.app
If multiple contacts are selected, only the first item is considered.
If nothing is selected or a group is selected or the selected contact has no addresses the script quits.

If the selected contact has one address, this address is taken.
If the selected contact has multiple addresses, you can choose an address from a list based on the label.

tell (current date)
	set currentDate to its date string
	set fileNameDateString to (its day as text) & (its month as integer) & year
end tell
set desktopFolder to path to desktop as text

tell application "Contacts"
	set theSelection to selection
	if theSelection is {} or class of item 1 of theSelection is not person then return
	set thisPerson to item 1 of theSelection
	set personName to name of thisPerson
	set theAddresses to addresses of thisPerson
	if theAddresses is {} then return
	
	if (count theAddresses) is 1 then
		set currentAddress to item 1 of theAddresses
	else
		set addressList to {}
		repeat with i from 1 to count theAddresses
			set end of addressList to (i as text) & space & label of item i of theAddresses
		end repeat
		set chosenItem to choose from list addressList
		if chosenItem is false then return
		set addressIndex to (word 1 of item 1 of chosenItem) as integer
		set currentAddress to item addressIndex of theAddresses
	end if
	set addressStreet to street of currentAddress
	set addressCity to zip of currentAddress & space & city of currentAddress
end tell

set fileName to personName & "_" & fileNameDateString & ".pages"

tell application "Pages"
	launch
	set currentDocument to make new document
	tell currentDocument
		set dateParagraph to make new paragraph
		set contents of dateParagraph to currentDate & return
		set alignment of dateParagraph to right
		set nameParagraph to make new paragraph at after last paragraph
		set alignment of nameParagraph to left
		set contents of nameParagraph to personName & return & addressStreet & return & addressCity & return
	end tell
	save currentDocument in file (desktopFolder & fileName)
end tell

This looks very impressive. I am eager to try it out later on. You are obviously a pro at this stuff. I greatly appreciate your taking the time to do this.

This works splendidly! I have tinkered and there are a couple of things I wonder if you can unravel. Here’s my modified script. There are two things that do not work. One is that fileNameDateString does not work inside the Pages file (although it works for making the filename). And, second, I am trying to test for the country name: if it is blank, or if it says United States of America", I don;t want that to be part of the address because what is entered is “missing value.” But if it IS part of the address, then I would like it entered. Any thoughts?

tell (current date)
set currentDate to its date string
set fileNameDateString to (its day as integer) & " " & (its month as text) & " " & year
end tell
set desktopFolder to path to desktop as text

tell application “Contacts”
set theSelection to selection
if theSelection is {} or class of item 1 of theSelection is not person then return
set thisPerson to item 1 of theSelection
set personName to name of thisPerson
set lastName to (last name) of thisPerson
set firstName to (first name) of thisPerson
set theAddresses to addresses of thisPerson
if theAddresses is {} then return

if (count theAddresses) is 1 then
	set currentAddress to item 1 of theAddresses
else
	set addressList to {}
	repeat with i from 1 to count theAddresses
		set end of addressList to (i as text) & space & label of item i of theAddresses
	end repeat
	set chosenItem to choose from list addressList
	if chosenItem is false then return
	set addressIndex to (word 1 of item 1 of chosenItem) as integer
	set currentAddress to item addressIndex of theAddresses
end if
set addressStreet to street of currentAddress
set addressCity to city of currentAddress & space & zip of currentAddress
set addressCountry to country of currentAddress

end tell

set fileName to lastName & ", " & firstName & " " & fileNameDateString & “.pages”

tell application “Pages”
launch
set currentDocument to make new document with properties {template name:“The New Criterion small”}
tell currentDocument
set dateParagraph to make new paragraph
set contents of dateParagraph to fileNameDateString & return
set alignment of dateParagraph to right
set salutParagraph to make new paragraph at after last paragraph
set alignment of salutParagraph to left
set contents of salutParagraph to "Dear " & firstName & “:” & return & return & return
set nameParagraph to make new paragraph at after last paragraph
set alignment of nameParagraph to left
if country of currentAddress is {} then
set contents of nameParagraph to personName & return & addressStreet & return & addressCity
else
set contents of nameParagraph to personName & return & addressStreet & return & addressCity & return & addressCountry

end if
end tell
save currentDocument in file (desktopFolder & fileName)
end tell

a pair of curly braces indicates an empty list. The country property is either missing value or a string

replace the bold lines regarding the country with


set addressString to personName & return & addressStreet & return & addressCity & return
if addressCountry is not missing value and addressCountry is not "United States of America" then
	set addressString to addressString & addressCountry & return
end if
set contents of nameParagraph to addressString

in the fileNameDateString line you have to coerce the day to text, otherwise you get a list


set fileNameDateString to (its day as text) & " " & (its month as text) & " " & year

Again, many thanks. I am full of admiration at your expertise as well as gratitude for the time you devoted to this random question!