Create Outlook 2011 HTML Signature using AppleScript

Discussion in 'Mac Programming' started by jtbbrandt, May 8, 2012.

  1. macrumors newbie

    Joined:
    May 8, 2012
    #1
    What I Would Like to Do Using AppleScript:

    Create an email signature in Outlook 2011. Apparently with the most recent update to Microsoft Office 2011 they have included more compatibility with AppleScript.

    For example I can create a signature by doing the following:
    tell application "Microsoft Outlook"
    make new signature with properties {name:strName, content:contentHTML}​
    end tell

    Where strName of the signature in Outlook and contentHTML is the actual content of the HTML email signature. The hurdle I am trying to get over is to be able to have contentHTML be the actual content of the .html file that has my signature.

    I don't want to have the html code as the variable but rather what the html content would look like in a browser. For a reference of what I am trying to do using AppleScript see here.

    Any suggestions on how I may be able to accomplish this?
     
  2. macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #2
    You can just read the contents of your file, for example

    Code:
    set contentHTML to (read (choose file))
    The HTML code is what determines what it looks like in a browser (take a look at the signature template used in the article).
     
  3. thread starter macrumors newbie

    Joined:
    May 8, 2012
    #3
    Now when I run:

    set contentHTML to (read ("Users:username: Desktop:file.html"))

    tell application "Microsoft Outlook"
    make new signature with properties {name:strName, content:contentHTML}
    end tell


    I get an AppleScript Error: Can't make "Users:username: Desktop:file.html" into type file.

    This is the error log: error "Can’t make \"Users:username: Desktop:with-cell-num.html\" into type file." number -1700 from "Users:username: Desktop:with-cell-num.html" to file
     
  4. macrumors 603

    Joined:
    Aug 9, 2009
    #4
    The pathname is wrong.

    When using colon-separated directories, the first element must be the disk name:
    Code:
     "[U]Macintosh HD[/U]:Users:username:Desktop:file.html".
    
    And there shouldn't be a space before "Desktop".

    And "username" should be your actual user name, just as "Macintosh HD" should be your actual disk name.
     
  5. thread starter macrumors newbie

    Joined:
    May 8, 2012
    #5
    I added " Macintosh_HD " and received the same error as before. Can't make "Macintosh_HD:Users:username: Desktop:file.html" into type file.

    I put the space between the : and the D otherwise it looks like this = :Desktop
     
  6. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #6
    Code:
    set the_file to (path to desktop as text) & "file.html" as alias
    set contentHTML to (read the_file as «class utf8»)
     

    Attached Files:

  7. thread starter macrumors newbie

    Joined:
    May 8, 2012
    #7
    OK. Now my code looks like:

    set contentHTML to ("Macintosh_HD:Users:username: Desktop:with-cell-num.html")
    set contentHTML to (read {"Macintosh_HD:Users:username: Desktop:with-cell-num.html" as class, utf8})
    tell application "Microsoft Outlook"
    make new signature with properties {name:strName, content:contentHTML}
    end tell

    it compiles however, I receive this error when it runs: Can't make "Macintosh_HD:Users:username: Desktop:with-cell-num.html" into type class.
     
  8. macrumors 603

    Joined:
    Aug 9, 2009
    #8
    Below the post-editing box, below the "Submit Reply" and "Preview Post" buttons, is an area title "Additional Options".
    There should be a "Miscellaneous Options" box there with two checkboxes.
    One of the checkboxes is "Disable smilies in text".

    Or do as I did and use the CODE tags.
     
  9. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #9
    Code:
    set contentHTML to ((path to desktop as text) & "with-cell-num.html") as alias
    set contentHTML to (read contentHTML)
    tell application "Microsoft Outlook"
    	make new signature with properties {name:strName, content:contentHTML}
    end tell
    
     
  10. thread starter macrumors newbie

    Joined:
    May 8, 2012
    #10
    That works!!! However, and this is curious to me, if I use name:strName it tells me that the variable strName is not defined. I didn't think this was a variable but rather a property.

    Also, if I change name:strName to name:name for example the script runs and creates a signature in Outlook with the name Microsoft Outlook. If I use anything but name as the property I receive that variable not defined error. Any idea why it would do this?
     
  11. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #11
    From the System Events dictionary :
    You're telling "Microsoft Outlook" to do something but name is also a property of application and reserved. If you want to use the strName variable you should assign it a value. Like :

    Code:
    set strName to "John Doe"
    or without the strName variable :

    Code:
    make new signature with properties {name:"John Doe", content:contentHTML}
     
  12. thread starter macrumors newbie

    Joined:
    May 8, 2012
    #12
    Thanks for all of your help. This was the part I was most worried about; getting the signature into Outlook.

    Now that that is done I need to focus on actually creating the signature. I created a bash script to get that done but I am wondering that if I do it in applescript that I can get this whole process to be really smooth.

    Here is my bash script:

    This script basically asks the user for input, stores that input as a variable and then finds in the template email signature file for the certain places to put the variables. Then it creates a new file.

    Is there a way to do a similar thing using applescript? Especially the sed part.
     
  13. kryten2, May 9, 2012
    Last edited: May 9, 2012

    macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #13
    You can but there's no need to. Applescript can run shell scripts too.

    From the Terminal Suite dictionary :

    Code:
    set theScript to "/path/to/your/script"
    tell application "Terminal"
    	activate
    	do script theScript in window 1
    end tell
    
    But if you really want to. You can accomplish something similar for your echo/read lines with :

    Code:
    set thefullName to text returned of (display dialog "" default answer "" buttons {"OK"} default button 1)
    Sed line :

    Code:
    do shell script ""
    Last echo line :

    Code:
    display dialog "" buttons {"OK"} default button 1

    You might want to take a look at those dictionaries. All the info is there for you to use.
    Also the last echo line in your script says Your signature has been created as 'newSignature.html' file on your desktop. Must be confusing for the user if they search for a nonexisting file. Shouldn't that be domo.html?
     
  14. thread starter macrumors newbie

    Joined:
    May 8, 2012
    #14
    Any reason why it say Macintosh_HD/Users/jtb/Desktop/sig.sh: No such file or directory ? That file is definitely on the desktop.
     
  15. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #15
    Code:
    set theScript to "/Users/jtb/Desktop/sig.sh"
     
  16. thread starter macrumors newbie

    Joined:
    May 8, 2012
    #16
    Thanks! Is there anyway to have my script wait while the user finished the bash script before moving on to the next part?
    wait for user to finish script here. then move on to the next part.

     
  17. macrumors 603

    Joined:
    Aug 9, 2009
    #17
    You need to use the right delimiters (slash or colon), and the right naming convention. The pathname you show is a mismatched combination of Posix file convention (slash delimiters) and classical Mac OS file convention (start with disk name).

    Look at the Posix file AppleScript path, and Posix path property of an AppleScript alias object. I also recommend reading a decent AppleScript tutorial or reference doc on the difference between these two very different file naming conventions.
     
  18. kryten2, May 9, 2012
    Last edited: May 10, 2012

    macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #18
    Code:
    make new signature with properties {name:[COLOR="Red"][U]name[/U][/COLOR], content:contentHTML}
    I advised you not to use a name value for the name property of the properties record.

    Quote from the Applescript Terminology and Apple Event Codes :

    Code:
    set theScript to "/Users/jtb/Desktop/sig.sh"
    tell application "Terminal"
    	activate
    	do script theScript in window 1
    	repeat while busy of tab 1 of window 1 is true
    	end repeat
    end tell
    Please note the in window 1 and of tab 1 of window 1. The script should wait for the shell script in the Terminal window to finish so the busy property of tab returns false. Once again all of this can be found in the appropriate dictionaries.
     
  19. thread starter macrumors newbie

    Joined:
    May 8, 2012
    #19
    Thanks for all of your help. This is what I've got so far:

    Its really great. It waits while the user completes the bash script then it goes on to add the created signature into Outlook. I need to figure out how to set it as the default script.

    I tried adding to the make new signature line:

    But it just spits out an error:

    This is the html content of my signature. I am not sure what the error is trying to tell me.
    FYI: I replaced some of the things in my script with Google to hide a couple of things.
     
  20. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #20
    Unfortunately I don't have Outlook 2011. Those properties you tried to add are those real properties? Where did you get them? By looking in the dictionary? If so can you please post a screenshot of the relevant section of the dictionary so I can take a look at them. You've got the possibility of posting screenshots please use it. Thanks.
     
  21. macrumors 603

    Joined:
    Aug 9, 2009
    #21
    Remove both additional properties. Does it work now?

    Add the properties back one at a time. Which one works? Which one doesn't?

    Change the HTML content to something much smaller. Now add both new properties. What happens? (That is, try it, describe exactly what you tried, post all error messages).

    You wrote this:
    Code:
    make new signature with properties {name:name, content:contentHTML, [B]plaintextcontent:""[/B], [B]includeinrandom:false[/B]}
    
    but the error message says this (condensed):
    Code:
    Can’t make {name:"Sig1", content:"<html here>", [COLOR="Red"]string:""[/COLOR], include in random:false} into type properties of signature.
    
    The red-hilited one isn't what you showed at all. Does the scripting dictionary reference show the string property being the same as plaintextcontent? Does it say plaintextcontent and content are mutually exclusive (one or the other, but not both)?
     
  22. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #22
    This is what I found on this link : http://www.officeformachelp.com/2011/04/microsoft-updates-applescript-dictionary-for-outlook-2011-sp1/

     
  23. macrumors 603

    Joined:
    Aug 9, 2009
    #23
    Reading that suggests mutual exclusivity to me. That is, if one wanted a plain text signature, one would use plain text content. Conversely, if one wanted HTML in the signature, one would use content. I can't think of a logical way that one could have both at the same time.

    So my suggestion is to remove the plain text content property from the failing example, and see what happens.
     
  24. macrumors regular

    Joined:
    Aug 12, 2008
    Location:
    Cambridge, UK
    #24
    I've tested it in Outlook 2011 and as chown33 has pointed out the two properties are mutually exclusive. If you use the contentHTML property Outlook will automatically create a plain text version of the HTML signature.

    If you use the plain text content property Outlook will add a plain text signature and if sending a HTML email will format the signature to use the default HTML font.
     

Share This Page