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
Related
I have the AppleScript below that worked great in Big Sur. But after upgrading to Monterey, it stopped working. Can anyone help me fix it?
Thanks!
changeKeyboardLayout("Squirrel")
on changeKeyboardLayout(layoutName)
tell application "System Events"
tell process "TextInputMenuAgent"
click menu item layoutName of menu 1 of menu bar item 1 of menu bar 2
click menu bar item 1 of menu bar 2
end tell
end tell
end changeKeyboardLayout
If I change layoutName to a number, such as 1
click menu item 1 of menu 1 of menu bar item 1 of menu bar 2
ScriptDebugger shows
menu item "U.S." of menu 1 of menu bar item 1 of menu bar 2 of application process "TextInputMenuAgent"
So it is able to get U.S., but the click simulation does not have any effects, i.e., it does not switch the keyboard layouts to U.S. (from another keyboard).
The example AppleScript code, shown below, was tested in Script Editor under macOS Monterey with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.
1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.
Example AppleScript code:
my changeKeyboardLayoutTo("U.S.")
to changeKeyboardLayoutTo(layoutName)
ignoring application responses
tell application "System Events" to ¬
click menu bar item 1 of menu bar 2 of ¬
application process "TextInputMenuAgent"
end ignoring
delay 0.1
do shell script "killall 'System Events'"
delay 0.2
tell application "System Events"
launch
click menu item layoutName of menu 1 of ¬
menu bar item 1 of menu bar 2 of ¬
application process "TextInputMenuAgent"
end tell
end changeKeyboardLayoutTo
Notes:
The example AppleScript code assumes one has checked Show Input menu in menu bar in: System Preferences > Keyboard Input Sources
The example AppleScript code is also coded to workaround the ~5 second delay that is a know issue between actuating the primary menu and the the target menu item on the menu.
See my answer A: AppleScript - Can't get rid of delay after click for the reference to mentioned ~5 second delay, which still persists in macOS Big Sur and macOS Monterey. It also includes other methods to click the target menu items that can be adapted as/if needed. Although I have not tested the alternate solutions in macOS Monterey yet.
Note: The example AppleScript code is just that and sans any included error handling 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.
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.
I'm trying to write an AppleScript that will use the Save as Pictures... function of PowerPoint, but I'm wrestling with AppleScript. This is what I have:
set p to "file.pptx"
tell application "Microsoft PowerPoint"
launch
open p
delay 2
click menu item "Save as Pictures..." of menu bar item "File" of menu bar 1
end tell
and it isn't doing what I want. The specific error I get is:
script error: Expected end of line, etc. but found class name. (-2741)
And I don't know what to do. I've tried all sorts of things but I can't seem to get the right menu item to click. I'm using OSX 10.9 and PowerPoint 2011 (Version 14.3.2)
UPDATE:
This is now the script I have:
set p to "file.pptx"
tell application "Microsoft PowerPoint"
launch
open p
end tell
delay 2
tell application "System Events"
tell process "PowerPoint"
click menu item "Save as Pictures..." of menu 1 of menu bar item "File" of menu bar 1
end tell
end tell
And I'm getting an execution error: Microsoft PowerPoint got an error: Parameter error. (-50)
Gui automation is generally done through the "System Events" app.
Also you need to provide a full path to a file before trying to open it.
This is how you would begin your attack on PowerPoint correctly.
set p to POSIX file "/Users/drew/Desktop/file.pptx"
tell application "Microsoft PowerPoint"
launch
open p
end tell
delay 2
tell application "System Events"
tell process "PowerPoint"
click menu item "Save as Pictures..." of menu 1 of menu bar item "File" of menu bar 1
end tell
end tell
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
I'm using Applescript to automate deployment of applications from Xcode 4.
I use System Events to click the menu items in the menu bar for that. I have the whole thing working, but with one quirk. If the user clicks elsewhere while my script is running, that is the XCode 4 window goes out of foreground, my entire script fails. Is there a way to force Xcode to be in the foreground from my script?
Now, if only Xcode 4 was at least as scriptable as Xcode 3, I wouldn't have to resort to GUI automation.
You can just use the activate command for every click call to ensure that the application is in the foreground. It's not ideal. Really if you're going to use System Events for scripting input like this you have to just accept that the user can't really use the computer whilst the script is running!
If you can break the script down into parts that require user input and parts that don't, you could present a dialog to the user saying something like, "Are you ready to continue with the script? You'll have to leave your computer for a while!"
... and then when it's finished, "Feel free to use your computer again now!"
This might make the script a little less obtrusive. Just a suggestion.
you should put up some of loading image or something while it is running also anytime you call a gui you should run a loop with a timeout that activates the app then checks for the existence of the items you want to click then when it exists click it and get out of the loop
EDIT
helpful link
A combination of mcgrailm and James Bedford's answers worked.
I put the menu click for "Edit Schemes…" in a loop until the edit scheme sheet becomes existent.
I also had to activate the application just before I clicked "Run without building".
The code:
tell application id "com.apple.dt.Xcode"
activate
end tell
tell application "System Events"
tell process "Xcode"
repeat until sheet 1 of window 2 exists
click menu item "Edit Scheme…" of menu "Product" of menu bar item "Product" of menu bar 1
tell application "Xcode"
activate
beep
end tell
end repeat
tell sheet 1 of window 2
set destination to pop up button 2 of group 1
click destination
set target to "iPad 4.3 Simulator"
click menu item target of destination's menu 1
set buildConfig to pop up button 3 of group 2
click buildConfig
click menu item "Debug" of menu 1 of buildConfig
click button "OK"
end tell
tell application "Xcode"
activate
beep
end tell
tell application id "com.apple.dt.Xcode"
activate
end tell
click menu item "Run Without Building" of menu 1 of menu item "Perform Action" of menu "Product" of menu bar item "Product" of menu bar 1
end tell
end tell