Auto-fill FedEx forms in Safari via AppleScript

Feb 14, '08 07:30:02AM

Contributed by: ClarkGoble

If you do a lot of shipping with FedEx, you know how annoying it is to cut and paste all the address information into Safari. There are expensive programs that will extract information, but most don't work with OS X or require Filemaker. Here's a quick hint I use quite often to autofill the FedEx forms.

tell application "Safari"
  activate
  tell (make new document) to set URL to "https://www.fedex.com/ship/shipEntryAction.do?method=doInitialEntry&origincountry=us&locallang=en&urlparams=us&sType=&programIndicator=0"
  delay 2
  
  set doc to document "FedEx | Ship Manager | Shipping"
  log (doc's name)
  do JavaScript "document.forms['domesticShipmentActionForm']['recipient.companyName'].value = 'add'" in doc
  do JavaScript "submitForm('doSelectRecipientCompanyName')" in doc
  delay 2
  do JavaScript "document.forms['domesticShipmentActionForm']['recipient.companyName'].value = 'New Company'" in doc
  do JavaScript "document.forms['domesticShipmentActionForm']['recipient.firstName'].value = 'First'" in doc
  do JavaScript "document.forms['domesticShipmentActionForm']['recipient.lastName'].value = 'Last'" in doc
  do JavaScript "document.forms['domesticShipmentActionForm']['recipient.addressLine1'].value = 'Address1'" in doc
  do JavaScript "document.forms['domesticShipmentActionForm']['recipient.addressLine2'].value = 'Address2'" in doc
  do JavaScript "document.forms['domesticShipmentActionForm']['recipient.city'].value = 'City Name'" in doc
  do JavaScript "document.forms['domesticShipmentActionForm']['recipient.stateProvinceCode'].value = 'UT'" in doc
  do JavaScript "document.forms['domesticShipmentActionForm']['recipient.zipPostalCode'].value = '84111'" in doc
  do JavaScript "document.forms['domesticShipmentActionForm']['recipient.phoneNumber'].value = '801-655-1996'" in doc
  do JavaScript "document.forms['domesticShipmentActionForm']['recipientGroundCommercialFlag'].checked = true" in doc
  do JavaScript "recipientGroundCommercialFlag = false" in doc
  do JavaScript "document.forms['domesticShipmentActionForm']['shipmentServiceType'].value = 'FedEx Home Delivery'" in doc
  do JavaScript "document.forms['domesticShipmentActionForm']['shipmentPackagingType'].value = 'Your Packaging'" in doc
  do JavaScript "document.forms['domesticShipmentActionForm']['packageWeightForPieceZero'].value = '1.5'" in doc
  do JavaScript "document.forms['domesticShipmentActionForm']['packageWeightForPieceZero'].value = '1.5'" in doc
  do JavaScript "document.forms['domesticShipmentActionForm']['shipmentDimension'].value = '1006956'" in doc
  do JavaScript "document.forms['domesticShipmentActionForm']['packageDeclaredValueForPieceZero'].value = '70'" in doc
  do JavaScript "document.forms['domesticShipmentActionForm']['shipmentAlert.senderExceptionNotificationFlag'].checked = true" in doc
end tell
You can easily modify this to do more programmically. For instance, I use Python and appscript to parse the emails my e-commerce solution sends, and then fill in the FedEx page with Safari. I'll leave that up to you. Even if you don't need to do that, simply autofilling most of the common fields may be a huge timesaver for you. This also demonstrates an easy way to handle forms in general.

The only bit that needs explanation is the filling in of the field shipmentDimension, which seems a tad confusing. FedEx adds a unique number for each custom box you've created. Go to Safari's View » View Source menu, search for shipmentDimension, and you'll see your custom box sizes and the numbers for each.

Also note the two delay statements which wait two seconds each. That is necessary so that FedEx's custom Javascript code runs. If you have a slow connection, you may need to increase that delay time.

Comments (6)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20080205190647941