make applescript run in the background - macos

I have tried using the script in Outlook 2011: Adding some messages to "Waiting for reply" folder. Basically, when I compose an Outlook message and hit Ctrl-Shift-W (as specified in the script name), it tries to send the message, find it in the sent folder and move it to a "waiting" folder. To make sure Outlook has a chance to send the message, it tries to delay before finding the message in the sent folder, repeatedly.
Unfortunately, the delay doesn't work (it doesn't delay), making it fail. Checking to do shell script "sleep 1s" means that Outlook is then stuck and doesn't send the message until the script fails.
I figured that outlook is waiting for the script to end before doing any background tasks. So the script needs to run in the background. Maybe launching an auxiliary script or something similar.
The problem is I don't know how to do this. Hence this question...

While AppleScript does have its own sleep method (delay), what you are probably looking for is the idle handler. If you save your script as an application and check “Stay open after run handler”, Mac OS X will call your script’s idle handler immediately after your “run” handler finishes. Your idle handler then returns the number of seconds until the next time it should be called; this can happen indefinitely. In your case, it might look something like this:
global messageID
on run
-- send message
-- set messageID to whatever you are using as the just-sent message’s identifier
end run
on idle
--check whether message exists in the sent folder
--note that this is just pseudocode, you’ll need to change it for your purposes
if exists messageID in sent folder then
--do something with it
quit
else
--wait another second
return 1
end if
end idle
This will repeat the idle every second until the criteria in the if is met at which point it will perform the task and quit the script.

You can try something like this:
ignoring application responses
tell application "Microsoft Outlook"
--send code goes here
sync
end tell
end ignoring
delay 5
tell application "Microsoft Outlook"
-- Find code goes here...
end tell

Related

Applescript for Outlook v15 - Recipients won't populate

I have copied some scripts on the site and the result is that Outlook creates a new forwarded message but the recipient is blank. Have tried multiple forms but always the blank recipient. It's a script I will only run for messages I manually select, so I can't use a simple forwarding rule in the app.
Any ideas?
tell application "Microsoft Outlook"
repeat with eachSelectedMessage in selectedMessages
set theForwardedMessage to forward eachSelectedMessage with opening window
make new recipient at theForwardedMessage with properties {email address:{address:"recipient#somewhere.com", name:"Lumpkin Skinbark"}}
send theForwardedMessage
end repeat
end tell
New information: It turns out the recipient is being populated correctly and put in my Outbox, but not being sent. In addition, a new window pops up with the same forwarded message and a blank recipient. That new window was the one I saw that led me to believe the recipient was not populating.
My workaround for now is to periodically hit the send/receive button in Outlook to send the messages sitting in the Outbox (I don't know why they don't just get sent automatically like everything else). Then I added:
close window 1
to close the open window -- don't know why it's created and don't want it, but might as well just close it and move on.
So I do have it working, but it's not elegant.
I finally figured out how to make this work: DON'T Open the forwarded email window until AFTER you've set the email properties, especially the Recipients.
Here's my test script:
set newSubjectStr to "TEST -- DON'T OPEN at first."
set recipientEmail to "somebody#InvalidDomainName.com"
tell application "Microsoft Outlook"
set selMsg to current messages
set oMsg to item 1 of selMsg
set subjectStr to subject of oMsg
set oFwdMsg to ¬
forward oMsg without opening window ## This is KEY. Don't Open Window
make new recipient at oFwdMsg ¬
with properties {email address:{address:recipientEmail}}
set subject of oFwdMsg to newSubjectStr
--- NOW OPEN FWD EMAIL WINDOW ---
open oFwdMsg
end tell
--- SEND NOW IF NO MORE CHANGES ---
-- Works fine if I manually send, or use keyboard shortcut --
-- Send and Close Window ---
delay 0.2
tell application "Microsoft Outlook" to activate
tell application "System Events"
key code 36 using {command down} -- CMD-RETURN
end tell
--- BELOW SEND BY SCRIPT LEAVES MSG IN OUTBOX ---
--send oFwdMsg
--close window 1
--sync folder "Outbox"
NOTE to Moderators: Is there a way to get syntax highlight for AppleScript?

Attaching File To Microsoft Outlook with AppleScript

I am trying to make a script that will attach a file to Outlook and then send it. I have that part of the script down fine. My only problem is that for some reason the file isn't attaching. When I look at the replies box at the bottom, it says that it attached and sent the email successfully, however, the file is not attached. What is wrong with my script?
tell write_email
delay 1
make new attachment with properties {file:fileAttachment}
delay 1
end tell
send write_email
I already defined prior to that bit of code this:
set fileAttachment to /Desktop/file-name

Send a key code to an application without activating it first?

I'm trying to send the spacebar key to an application without activating it first. The following code almost does what I want but it brings the application to the foreground first.
tell application "X"
activate
tell application "System Events" to key code 49
end tell
I don't think you can send a keystroke to an inactive application, but you can hide an app immediately after activating it and executing code for it. This does however, cause the app to briefly flash before it hides.
tell application "System Events"
tell application "X" to activate
key code 49
set visible of process "X" to false
end tell
Sending a keystroke can basically be seen as using a keyboard, but the only difference is that the keys that need to be pressed are already predefined. The rest of the process revolving around this doesn't change. This means that the application itself still needs to be opened and activated before you can actually send keystrokes to it.
Depending on the application however, it might be possible to use certain Applescript functions in the application's API to send different inputs to the application without having to activate it first. Take the Messages API for instance:
tell application "Messages"
set theBuddy to buddy "someone#mac.com" of service "iMessage"
send "Hi there" to theBuddy
end tell
How about deactivating it afterwards?
activate application "X"
tell application "System Events" to key code 49
activate me
Position is offscreen to prevent it from flashing where the user can see it or reduce its opacity.
There are courses of investigation.
If the application is one you have developed yourself you have the following options:
*simply have a public property exposed and set that to the key you want to send it.
*have your application polling a folder for a file and you send your instructions via that.
With a windows API hook of some type you can get control of the application without activating it. I am pretty certain if I put my mind to it I could take control of anything on the computer.
In simple terms think out of the box, it does not necessarily need to be a key press you send, you just want to instruct it to do something. There is loads of options Interface Marshalling, Interops, OLE, DDE, looks like I have turned up on this site just in time!

Can an applescript "tell" call execute without visibly launching the application?

I have a Mail rule set up to launch the following applescript:
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
-- do stuff, including...
CheckAddressBook(theName, theAddress)
end tell
end perform mail action with messages
end using terms from
on CheckAddressBook(theName, theAddress)
tell application "Address Book"
-- do stuff
end tell
end CheckAddressBook
Whenever this mail rule executes, it launches address book. Its not activated, but it suddenly shows up on my desktop. My question is, can tell blocks be instructed to launch the application silently, and quit when complete?
AppleScript can't control an application without it running. That's just the way it works. There are other methods you might use to access the Address Book database without launching the application, but if you're using AppleScript to get data from your Address Book database the application has to launch. My recommendation would be to simply add a quit command as suggested by Fábio.
To read the Address Book Database without launching "Address Book.app" I´d suggest to have a look at the command line tool "contacts" available for free here. You would then run it from Applescript like do shell script "/usr/bin/contacts Peter" and handle the values returned.

Automator / AppleScript to process incoming emails in Mac Mail

I'm designing an app that allows users to email me crash reports if my app ever crashes. I'd like to leave Mac Mail running on a computer and when an email comes through, an automator script / AppleScript runs to process the contents of the body of the email.
I've got the entire parsing/processing done in a python script, except I have to manually copy the contents of the email into a file and then run my parser on that file.
What's the best way to set this up so I can the contents of the email be pushed into my parsing script?
Many thanks!
Probably the simplest approach is to define a Mail.app Rule. You can set up filtering conditions to specify the set of incoming email to apply the rule to and among the rule actions you can specify is one to run an AppleScript on incoming messages. Rules are managed with Mail.app Preferences -> Rules. Apple supplies examples of Rule Action scripts with Mac OS X. Look in /Library/Scripts/Mail Scripts/Rule Actions or search the web.
Here's a script that extracts from email into a file using a mail rule: MacScripter / Mail rule script for message export. Might be good for sample code for what you're doing.
Use the Dictionary in Applescript Editor to see the properties of mail and you'll quickly be able to see the properties of any mail message. Here's a quick and dirty example of getting the content of a mail message.
tell application "Mail"
set the_messages to selection
repeat with this_message in the_messages
set mytext to content of this_message
end repeat
end tell
Modify the script linked to above that copies output to a temporary file and then pass that file to your Python script to act on.

Resources