AppleScript - Fill in Outlook Subject via Keystroke Simulation - outlook

I am searching for an AppleScript which is able to simulate Key strokes in the below mentioned Script. The keystrokes should fill the "subject" and "content" field.
Please note: Unfortunately it is not enough to just fill in a complete strings since I need the keystrokes to activate an "Alfred-Text snipped" (Auto expansion function for text snippets).
I have already found some possibilities in the net like the example below but I'm not sure how this can be combinded with my script since "Subject" is expecting a string, and not keystrokes.
Is there an API for AppleScript in Outlook to insert keystrokes? Or is there another viable solution for this?
-- Keystroke example
tell application "System Events"
key code 124 using {shift down, command down}
end tell
on run {input, parameters}
set SelectedItems to input
tell application "Microsoft Outlook"
set newMessage to make new outgoing message
tell newMessage
repeat with aFile in SelectedItems
make new cc recipient at newMessage with properties {email address:{name:"Business Support", address:"business_suppport#xxxx"}}
make new attachment with properties {file:aFile}
set subject to "XXXXXXXX"
set content to "XXXXXXXX"
end repeat
end tell
open newMessage
get newMessage
activate
end tell

Related

Why won't my keystroke in AppleScript command turn an open email message into an "outgoing" message?

I am trying to implement an AppleScript to find emails in a mailbox that are saved drafts of outgoing emails. Each one found there should then be opened, , turned into an outgoing message and then sent. I have gotten really close, but in the following script The keystroke command does nothing. It should turn the message into an outgoing message by invoking "Send Again".
using terms from application "Mail"
tell application "Mail"
set theMailbox to mailbox "OutgoingEmail" of account "iCloud"
set foundMsgs to (every message in theMailbox)
set the messageCount to the count of foundMsgs
repeat with i from 1 to the messageCount
set newMsg to item i of foundMsgs
tell newMsg
open
tell application "System Events"
tell application process "Mail"
keystroke "D" using {shift down, command down}
end tell
end tell
end tell
end repeat
end tell
end using terms from
It does nothing though. The message is opened, but the keystroke has no effect. The next step would be to send the message, but that is not shown in the script.
I don't think you need to be using the "using terms from Mail" command, since you then have a tell block for mail on the very next line.
Try this code and see if it works.
tell application "Mail"
set theMailbox to mailbox "OutgoingEmail" of account "iCloud"
set foundMsgs to (every message in theMailbox)
set the messageCount to the count of foundMsgs
repeat with i from 1 to the messageCount
set newMsg to item i of foundMsgs
tell newMsg
open
end tell
tell application "System Events"
tell application process "Mail"
keystroke "D" using {shift down, command down}
end tell
end tell
end repeat
end tell
Also try a seperate script of just the system events commands to see if they work on their own...
tell application "System Events"
tell application process "Mail"
keystroke "D" using {shift down, command down}
end tell
end tell

How to set clipboard with value of selected cell in Numbers using AppleScript

I am trying to automate a repeating task between Numbers and Quicken 2017 using AppleScript.
I would like to take the contents of the currently selected cell in Numbers, set the clipboard with that numeric value, and paste that value into the search field in Quicken.
How can one go about doing that with AppleScript?
Example pseudo code to illustrate intent:
tell application "Numbers"
activate
set myCellsValue to value of currently selected cell
set the clipboard to myCellsValue
end tell
tell application "Quicken 2017"
activate
tell application "System Events"
keystroke "f" using {command down}
delay 0.1
keystroke "v" using {command down}
end tell
end tell
You got it 90% correct. I don't use Quicken, but this worked with Microsoft Word:
tell application "Numbers"
activate
tell active sheet to tell application "System Events" to keystroke "c" using command down
end tell
tell application "Quicken 2017"
activate
tell application "System Events"
keystroke "f" using command down
delay 0.1
keystroke "v" using command down
end tell
end tell
I generally like to minimise the amount by which I employ System Events to issue keystrokes or mouse clicks if there’s another way.
Numbers is very much AppleScript-able. I don’t use it personally, but with the help of this site, I’ve pieced together this example script that I wholly admit is untested (I’d appreciate your feedback if you try it out):
tell application "Numbers"
tell the front document to ¬
tell the active sheet to ¬
set CurrentTable to the first table whose class of selection range is range
tell the CurrentTable to get the value of the first cell in the selection range
if the result is not missing value then set the clipboard to the result
end tell
Quicken is also AppleScript-able, but I can’t find a downloadable copy of Quicken’s AppleScript dictionary in order to piece together an equivalent sample of code. However, your Quicken tell block is exactly right for employing System Events to issue a Cmd+V.
However, if you fancy uploading a PDF of the AppleScript dictionary, I can use it to try and draft something more robust.

Copying .rtf text into the body of an email with AppleScript

I have an AppleScript application which creates an email (in Mail.app) with attachments from the options I choose through dialogs. The text templates are stored in .rtf format so non-programers can alter the text templates to their wishes.
I am able to create an email from a .txt plain text file, but when I import an .rtf text file it imports the formatting commands, which I don’t want in the mail.
Here is an example of a .rtf import into an email:
{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf360
{\fonttbl\f0\fswiss\fcharset0 Helvetica-Light;}
{\colortbl;\red255\green255\blue255;}
\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\ql\qnatural\pardirnatural
\f0\fs24 \cf0 technology mail for english speakers\
Here is part of my script:
-- Import the .rtf file and store content in the mycontent variable
set mycontent to (read "/Users/kiara/Desktop/mailer/technology/tech-en-content.rtf")
…
…
-- create mail from values stored in variables
tell application "Mail"
set theMessage to make new outgoing message with properties {visible:true, subject:mysubject, content:mycontent}
tell content of theMessage
make new attachment with properties {file name:this_file} at after last paragraph
end tell
end tell
Is it possible to import formatted text from .rtf files into a new mail without the formatting codes (I choose rtf because mostly bold and color are used to format text)?
Here is another approach:
set the clipboard to (read "/Users/kiara/Desktop/mailer/technology/tech-en-content.rtf" as «class RTF »)
tell application "Mail"
activate
set theMessage to make new outgoing message with properties {visible:true, subject:"mysubject"}
end tell
tell application "System Events"
tell process "Mail"
repeat until focused of UI element 1 of scroll area 4 of window 1
keystroke tab
end repeat
keystroke "v" using command down
end tell
end tell
I think Mail sends an email as either plain text or html, not rtf. So you would need to send your email as html not rtf. Note there's a trick to sending a html email with Mail via applescript. For some reason you can't set the visible property of the new message to true. It won't work if you do.
Here's how this can help you. You can use the command line tool textutil to convert the rtf to html and then send it as an email. Notice I use "tidy" in the command to make sure the html code is clean.
So all you need to do is put the receiver's email address and a subject in this script and run it...
set emailAddress to "someone#somewhere.com"
set theSubject to "My converted rtf to html"
set rtfFile to choose file with prompt "Choose the RTF file to email as HTML:" without invisibles
set theHTML to do shell script "/usr/bin/textutil " & " -stdout -format rtf -convert html " & quoted form of POSIX path of rtfFile & " | /usr/bin/tidy -b -utf8"
tell application "Mail"
set newMessage to make new outgoing message at end of outgoing messages with properties {visible:false}
tell newMessage
make new to recipient at end of to recipients with properties {address:emailAddress}
set subject to theSubject
set html content to theHTML
send
end tell
end tell
adayzdone answer works for me - more or less.
under Mac os X 10.15.6 , Mail 13.4 the scroll area is at another position / index.
And instead of using keystroke tab to get the content-Area an alternative way is to
set value of attribute "AXFocused" of UI element of scroll area 1 of window 1 to true
if you need to find the scroll area or identify other UI elements - you might want to use
on FindScrollAreas()
set Indices to {}
set the clipboard to "hello World"
tell application "System Events"
tell process "Mail"
activate
set i to 1
repeat with UIElement in UI elements of front window
-- button, text field, scroll area, static text
if class of UIElement is scroll area then
set end of Indices to i
end if
set i to i + 1
end repeat
end tell
end tell
return Indices
end FindScrollAreas
and there is also the fantastic approach in using Automator watch me do to get the menu items:
see this post
(you have to copy-paste the Events from Automator to some text-Editor to get some corresponding applescript)
and there is Xcode -> Xcode(menu) -> Open Developer Tool -> Accessibility Inspector - but i find it hard to transfer info to applescript
hope that helps, best -tom

Applescript for creating New Message with Mail application

I have an AppleScript for Mail.app which opens a new message window with pre-defined recipient address and subject. This script opens a new window every time I run it:
tell application "Mail"
set newMessage to make new outgoing message with properties {subject:"some subject", content:"" & return & return}
tell newMessage
set visible to true
make new to recipient at end of to recipients with properties {name:"some name", address:"some address"}
end tell
activate
end tell
But I want the script to open a new message window only when the previously opened window is closed – otherwise, the previously opened window should come to the front.
Can anyone please help me in modifying this script to achieve the above mentioned functionality?
I didn't test this but it should do what you need... at least it shows you the proper approach. You basically use a "property" to keep track of some value from the last time the script was run. In this case we check for the name of the frontmost window and see if it matches your criteria. If the window name doesn't do what you need then just find some other value to track between launches of the script. The basic approach should work.
EDIT: using the ID of the message, which is unique, the following will do what you want:
property lastWindowID : missing value
tell application "Mail"
set windowIDs to id of windows
if windowIDs does not contain lastWindowID then
set newMessage to make new outgoing message with properties {subject:"some subject", content:"" & return & return}
tell newMessage
set visible to true
make new to recipient at end of to recipients with properties {name:"some name", address:"some address"}
end tell
activate
set lastWindowID to id of window 1
else
tell window id lastWindowID
set visible to false
set visible to true
end tell
activate
end if
end tell
the visibility toggle seems to be the only way to get the window in front, as frontmost is a read-only property. The lastWindowID property will store the ID as long as the script is not re-compiled (caveat empteor: do not put this into an Automator service, as these get re-compiled every time the service is loaded).

How to Populate Pages file from Excel Worksheet using Applescript

I'm trying to populate a template from data in an Excel spreadsheet. This is my first foray into Applescript. My plan is to have unique text strings in the template (i.e. Value_1, Value_2, etc) then store each value in a variable and find and replace each variable with each string.
I would very much appreciate any help; here is where I'm at:
tell application "Microsoft Excel"
tell active workbook
activate object worksheet "Page 2"
copy range range "B7" of active sheet
// Store as variable?
end tell
end tell
tell application "Pages"
activate
tell application "System Events"
keystroke "v" using command down
// Find and replace?
end tell
end tell
Something like this will do what you want :
tell application "Microsoft Excel"
tell active workbook
activate object worksheet "Sheet2"
set B7_Value to string value of range "B7" of active sheet
end tell
end tell
tell application "Pages"
set my_text to first document's body text
set temp to do shell script "echo " & quoted form of my_text & " | sed -e 's/B7_Value/" & B7_Value & "/g'"
set first document's body text to temp
end tell
This script does not keep formatting. If you want to keep formatting perhaps something like this :
tell application "Microsoft Excel"
tell active workbook
activate object worksheet "Sheet2"
set B7_Value to string value of range "B7" of active sheet
end tell
end tell
tell application "Pages"
activate
tell application "System Events"
keystroke "f" using command down
set the clipboard to "B7_Value"
keystroke "v" using command down
delay 0.1
keystroke tab
delay 0.1
set the clipboard to B7_Value
keystroke "v" using command down
delay 0.1
keystroke tab
delay 0.1
keystroke space
delay 0.1
key code 53 -- escape
end tell
end tell
For this to work you will need to enable System Preferences --> Keyboard --> All controls.

Resources