How to open a new UNSAVED contact in Outlook Mac with AppleScript? - macos

I can create a new contact and open it for display in Mac Outlook 2011 using AppleScript:
tell application "Microsoft Outlook"
set newContact to make new contact with properties {first name:"Fred", last name:"Flintstone"}
open newContact
end tell
But this contact is already saved. Is there a way I can open a new and unsaved Outlook contact, fill in the properties, and allow the user to decide whether or not to save it?
I've tinkered around with "make new window" but I can't get anywhere there. I consistently get the error:
error "Microsoft Outlook got an error: AppleEvent handler failed." number -10000
I think I need to go about this a different way, but nothing in the Outlook AppleScript dictionary looks promising.

You can do this by scripting the UI elements to open a new contact:
tell application "System Events"
click menu item "Contact" of menu 1 of menu item "New" of menu 1 of menu bar item "File" of menu bar 1 of application process "Outlook"
end tell
Update:
But the caveat is that the new contact is not a AppleScriptable object until it is added to the Outlook database, i.e. it is saved. If you add these lines to the above script, you can see this:
tell application "Microsoft Outlook"
set contactWindow to item 1 of (windows whose index is 1)
get object of contactWindow
end tell
object of contactWindow is a missing value.
So if you want to use the Outlook Applescript dictionary APIs to edit the fields of a new contact, then that contact must have first been saved.
2nd Update:
The following, when placed in the 'tell application "System Events"' block after creating a contact will set the lastname, firstname and email address for that contact using UI element scripting:
set lastName to "Einstein"
set firstName to "Albert"
set emailAddress to "a.einstein#relativity.com"
set value of text field 1 of splitter group 1 of window 1 of application process "Outlook" to lastName
set value of text field 2 of splitter group 1 of window 1 of application process "Outlook" to firstName
set value of text field 6 of scroll area 1 of window 1 of application process "Outlook" to emailAddress
As mentioned in the comments, "Enable access for assistive devices" needs to be enabled for this to work. This can also be done programmatically from AppleScript:
-- turn on UI automation - may throw a permissions dialog
if UI elements enabled is false then
set UI elements enabled to true
end if

Related

How do I tell if the frontmost window is a NSOpenPanel / file open dialog in MacOS using Applescript?

I'm trying to automatically change the directory of the frontmost "file open dialog" or NSOpenPanel dialog with AppleScript, whether that window is part of any application. The idea is that I hit a hotkey, and it will control that dialog to switch to a particular folder.
I can't seem to find out how to find the attributes of a window that would filter it for a "file open dialog". Using the Accessibility Inspector I can find that the "class" is NSOpenPanel. How can I get the class of a window using Applescript?
If you run the following AppleScript, you can see the properties of the foremost window:
tell application "anApp" to activate
delay 1
tell application "System Events"
tell process "anApp"
properties of window 1
end tell
end tell
The app has to be active to see the properties of the windows; You will not get consistent results if the app is in the background.
The NOOpenPanel ought to be recognizable by testing for some combination of the following properties:
role description:"dialog"
title:"Open"
subrole:"AXDialog"
name:"Open"
description:"dialog"
Personally, I'd probably rely on name and role description, which should be the same anytime an app throughs up a standard 'Open' dialog. 'Save' dialogs will be the same, except that title and name will be 'save' rather than 'open'.
If you have an app that presents a open or save sheet (a sub window attached to the titlebar), not a separate dialog, then you'll shift things a little. The AppleScript to get the properties looks like this:
tell application "anApp" to activate
delay 1
tell application "System Events"
tell process "anApp"
tell window 1
properties of sheet 1
end tell
end tell
end tell
and the relevant testable properties are as follows:
accessibility description:"save"
role description:"sheet"
role:"AXSheet"
description:"save"
You'll probably have to add logic to test whether the front window has a sheet, which should distinguish between dialogs and sheets.
Some apps use non-standard open/save dialogs, and you'll have to account for them on a case-by-case basis. There's no magic bullet for that.

Using AppleScript automation how can I get currently opened Adobe reader file name

I tried below code
activate application "Adobe Reader"
tell application "System Events"
tell process "Adobe Reader"
set currentFile to active Document
end tell
end tell
But I couldn't get active document name, I tried this code by already with opened document. I don't even find any dictionary for adobe reader in script Editor. Any suggestions will be much appreciated
The Adobe Reader app does not have an AppleScript dictionary file within it's application bundle and as such is not AppleScript scriptable beyond a very limited number of Standard Suite commands and UI Scripting its menu bar with System Events.
If you just want to get the name of the document that has focus, then the following example AppleScript code can do that:
if running of application id "com.adobe.Reader" then
try
tell application "System Events" to ¬
set docName to ¬
(get name of every menu item of menu 1 of menu bar item ¬
"Window" of menu bar 1 of application process ¬
"Acrobat Reader" whose value of ¬
attribute "AXMenuItemMarkChar" = "✓") as string
return docName
end try
end if
The use of return docName was for testing purposes and can be removed as appropriate. Additionally, it was not necessary for Adobe Reader to have focus and its window could even be minimized, the script still retrieved the name as shown on its Window menu.
Note: This was tested on macOS High Sierra using US English and Adobe Acrobat Reader DC (Continuous Release | Version 2019.021.20058.) and works as is. Adjustments may be needed for other languages and or different versions of Adobe Reader.
Note: The example AppleScript code is just that and does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.
Here are a few AppleScript options that may work for you. I don’t have Adobe Reader so I am not able to test the code.
tell application "Adobe Reader" to set currentFile to name of document 1
If that doesn’t work for you, you can try this…
tell application "Adobe Reader" to activate
repeat until application ""Adobe Reader" is frontmost
delay 0.1
end repeat
tell application (path to frontmost application as text) to set currentFile to name of document 1

Using an applescript/automator to attach multiple items to a new Outlook message

I've worked out (found online) how to attach a single item from Finder to a new Outlook message. I've played with this format a good amount - changed 'selecteditem' and selection and some other more major changes - but I can't work out how to get more than one item to attach to a new outlook message at a time.
Only a single item attaches to each new outlook message. There are no Outlook Automator options in Automator - I think Office 365 did away with them.
My current script is as follows:
tell application "Finder" to set selectedItem to item 1 of (get selection)
set theAttachment to selectedItem as alias
set fileName to name of selectedItem
tell application "Microsoft Outlook"
set newMessage to make new outgoing message with properties {subject:fileName}
tell newMessage
make new attachment with properties {file:theAttachment}
end tell
open newMessage
get newMessage
end tell
I'm currently trying use this script in Automator as a service so I have a right click option to send files directly to a new Outlook message. It's currently setup like this.
Your Automator Service gets "Files or Folders" from Finder. This part is OK. Just the content of the applescript must be changed.
To read these inputs (the selected files from Finder), you must use the "on run" handler where "input" will be the selected files.
The script bellow must replace your current script. I assumed that the subject of your email should be the name of the first selected file in case of multiple selection:
on run {input, parameters}
set SelectedItems to input
tell application "Finder" to set fileName to name of first item of SelectedItems
tell application "Microsoft Outlook"
set newMessage to make new outgoing message with properties {subject:fileName}
tell newMessage
repeat with aFile in SelectedItems -- the loop through all selected items
make new attachment with properties {file:aFile}
end repeat
end tell
open newMessage
get newMessage
end tell
return input
end run
On top of that, I have 2 comments :
1) this script does not filter the case when you select a folder. Inside the loop, you may want to add a "if" test to check file or folder and take action in case selection is a folder.
2) I do not understand the overall process you want to perform: Select files, go to menu Service to run this service, which creates a new Outlook message with these files as attachment...You can do all this by just selecting the files and drag/drop them on the Outlook icon in the dock. it will immediately create a new blank message with all the attached files.
Old question, but I ran into this today and used the "Shortcuts" app on MacOS to get this working, or at least something similar.
Start to finish:
Opened Shortcuts app on MacOS (Monterey)
Select (+) to create new shortcut
Searched for and select "Run AppleScript"
Highlight Text in script and replace with below:
on run {input, parameters}
set SelectedItems to input
tell application "Finder" to set fileName to name of first item of SelectedItems
tell application "Microsoft Outlook"
set newMessage to make new outgoing message with properties {subject:fileName}
tell newMessage
repeat with aFile in SelectedItems -- the loop through all selected items
make new attachment with properties {file:aFile}
end repeat
end tell
open newMessage
get newMessage
end tell
return input
end run
On the top right of the Shortcuts window select the Shortcut Details (Adjustable Bars Icon)
Select "Pin in Menu Bar", "Use as Quick Action", "Finder" & "Services Menu".
Under the "Run AppleScript with" select "Shortcut Input".
It should look like this:
And this is how you use it within the Finder:
If outlook is open, it will attach it like shown:

Applescript - How to tell application to open a specific menu item

This is my first attempt to create an applescript.
What i'd like to do is create an apple script and then assing a custom shortcut to it so that it is launched when i need it.
I need it to quickly expand Thunderbird's insert -> HTML... menu.
My current applescript is nested into an automator workflow, and the code is the following:
on run {input, parameters}
tell application "Thunderbird"
activate
tell application "System Events"
tell process "Thunderbird"
click menu item 6 of menu 1 of menu bar item "Insert" of menu bar 1
end tell
end tell
end tell
return input
end run
And when i launch it (after grant permissions to automator), this is the error i get:
Syntax error: System Events got an error; Can't get menu bar item
"Insert" of menu bar 1 of process "Thunderbird"
I think that the proble is because the "Insert" menu is only available when the "Write new message" panel is open in thunderbird...
So my question is, how can i change the script above so that instead of only checking for "Thunderbird" app, it checks that both "thunderbird" and it's own "write new message" panel are open?
And also, is it liklely to be for this reason that i get this error, or i made some mistake in my applescript creation?
Thanks a lot and feel free to ask for more details.

Set the default Microsoft Outlook for Mac signature with Applescript

I've written a script that grabs information from Active Directory and creates a new Signature in Microsoft Outlook for Mac.
I use the following code to create the signature(I will leave the other code out, as it isn't really relevant):
tell application "Microsoft Outlook"
make new signature with properties {name:strName, content:contentHTML, plain text content:"", include in random:false}
end tell
Where strName is the name of the signature I get from elsewhere and contentHTML is the actual signature in HTML that I build elsewhere.
Adding this signature to Microsoft Outlook is working perfectly, but I can't see how to set the signature that I created to the default signature for the current account. I have done quite a lot of research that hasn't helped at all, and I've poked around the dictionary as well.
This can be done with AppleScript. There is nothing in the Outlook 2011 dictionary to specifically do this, so instead this can be done by scripting the UI elements, which admittedly is rather clunky.
Signatures are set on a per-account basis, so you need to provide the name of the account to this script as well as the name of the signature you want to set for that account.
setDefaultSignature to "strName" for "Gmail"
on setDefaultSignature to mySignature for accountName
tell application "Microsoft Outlook" to activate
tell application "System Events"
-- turn on UI automation - may throw a permissions dialog
if UI elements enabled is false then set UI elements enabled to true
click menu item "Preferences..." of menu 1 of menu bar item "Outlook" of menu bar 1 of application process "Outlook"
click item 1 of (buttons of window "Outlook Preferences" of application process "Outlook" whose description is "Signatures")
click button "Default Signatures..." of window "Signatures" of application process "Outlook"
repeat with thisRow in rows of table 1 of scroll area 1 of sheet 1 of window "Signatures" of application process "Outlook"
if value of text field of thisRow as string is accountName then
click pop up button 1 of thisRow
click menu item mySignature of menu 1 of pop up button 1 of thisRow
click button "OK" of sheet 1 of window "Signatures" of application process "Outlook"
click item 1 of (buttons of window "Signatures" of application process "Outlook" whose description is "close button")
exit repeat
end if
end repeat
end tell
end setDefaultSignature

Resources