As a first step, How can I get the x/y location of the file which is selected in Finder?
Here is my overall goal:
I use MS Outlook on my Mac. I generally do NOT use the mouse. This will work in MacMail, but not in Outlook.
I want to be able to activate finder
Locate a file in my finder window
Copy file from finder
Switch to outlook mail message
Paste file to outlook mail message
This works with Mac Mail, but does not work for Outlook because MS Outlook has a bug–the paste command pastes the location of the file rather than simulating a drag/drop of the file into the mail message.
So, I will have an active finder window with a file selected and an Outlook Message Window with a mail message already started.
All from the keyboard, I want to
Press cmd-C [or a KM macro] to copy the file from finder (OR simulate a click/drag)
Press cmd-Tab [to switch to Outlook email message]
Press cmd-V [or a KM macro] to paste the file (or simulate the Drop)
I have looked at the mouse commands, but do not see a way to click where the current file is selected in finder to initiate the click/drag.
I also am not sure if the macro will keep dragging once the active window has changed so that I can then execute the drop onto the mail message window.
You didn’t mention which version of Office/Outlook. I’m hearing that Office 2016 is still very buggy.
This AppleScript will work on Office 2011 and probably a generation or two back from that.
Select a file in the Finder.
Return to Outlook.
Run the script with an Execute an AppleScript action.
(The draft message must have been saved at least once, so I would add a ‘Save’ menu action at the beginning of the macro.)
------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2015/10/14 20:00
# dMod: 2015/10/14 20:14
# Appl: Microsoft Outlook & Finder
# Task: Attach the selected file in the Finder to the front outgoing message in Outlook.
# Tags: @Applescript, @Script, @Finder, @Microsoft_Outlook, @Attach, @File, @Outgoing, @Message
# Note: A draft message must be saved before this script will work.
------------------------------------------------------------
tell application "Finder" to set selectedItems to selection as alias list
if length of selectedItems = 1 then
set theAttachment to item 1 of selectedItems
else
error "Something wrong with Finder selection!"
end if
tell application "Microsoft Outlook"
set frontOutgoingMessage to last outgoing message
tell frontOutgoingMessage
make new attachment with properties {file:theAttachment}
end tell
end tell
------------------------------------------------------------
That looks very helpful. However, when I run the code, the script gets error on “last outgoing message”. It seems to work better if you use “Mail” as the application, but I don’t think that will help me.
Any Finder-based "select file(s)" dialog should allow almost all the functions of Finder (it is, afaik, just an instance of Finder). If you have the path of the file you want to attach, you can paste it into the dialog. You can select folders via the sidebar, and find folders or files via the search field. (You may have to expand the dialog — look for a disclosure triangle.)
In place of this:
just locate the file using the "Add Attachment" dialog.
Microsoft® Outlook® for Mac 2011
Version 14.5.7 (151005)
You still have not mentioned which version of Office and Outlook you're using – that information is relevant.
How can that be when the script is specifically written for Outlook and not Apple Mail? The syntaxes are not compatible for one thing, and Mail will not allow changes to be made to an existing outgoing message for another.
This is kludgy by comparison, but it should work:
Run the script in an Execute an AppleScript action.
------------------------------------------------------------
tell application "Finder" to set selectedItems to selection as alias list
if length of selectedItems = 1 then
set thePath to POSIX path of (item 1 of selectedItems)
set the clipboard to thePath
else
error "Something wrong with Finder selection!"
end if
------------------------------------------------------------
I have followed your logic. I was able to get the first finder code to work. Part of my problem was that I was not opening the message at the end. I am not sure why that was necessary. Also, outlook is being flaky. Sometimes it takes quite a while to run and/or save the files.
I have the following code:
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"
if false then
set frontOutgoingMessage to make new outgoing message with properties {subject:"Testing subject"}
make new recipient at frontOutgoingMessage with properties {email address:{name:"John Smith", address:"john@example.com"}}
else
set frontOutgoingMessage to last outgoing message
end if
tell frontOutgoingMessage
make new attachment with properties {file:theAttachment}
end tell
open frontOutgoingMessage
end tell
If I run the alternate path to create a new message with an attachment (where I have the false in the above listing), the code runs lightning fast.
However, when I run it as written to get the last outgoing message, the code takes 19 seconds to run on a very fast imac retina 5k.
There seems to be a delay introduced by Outlook when Outlook is queried for its most recent message. I realize this is more an applescript/outlook problem than a KM problem. I am very grateful for the help and the introduction into applescript.
If you see something that would cause Outlook to slow this down so much, let me know.
As it happens I spent quit a bit of time with Outlook yesterday working on a different problem, but it gave me some insight into this one.
Hopefully this will just work.
-Chris
------------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2015/10/15 17:00
# dMod: 2015/10/16 02:05
# Appl: Finder & Microsoft Outlook
# Task: Add Attachment to front draft window.
# Tags: @Applescript, @Script, @Microsoft_Outlook, @Add, @Attachment, @Draft, @Window
------------------------------------------------------------
tell application "Finder" to set selectedItems to selection as alias list
if length of selectedItems = 1 then
set theAttachment to item 1 of selectedItems
else
error "Something wrong with Finder selection!"
end if
------------------------------------------------------------
tell application "Microsoft Outlook"
if class of front window ≠ draft window then
error "Front window is not a draft window!"
else
tell front draft window
if its object = missing value then save
tell its object to make new attachment with properties {file:theAttachment}
end tell
end if
end tell
------------------------------------------------------------
Error: “Expected “then”, etc. but found class name.”
I then put “draft window” in quotes and that got the code to compile. Now, it is complaining that my front window is not a draft window. I open an email in outlook, start it , and save it to drafts, leaving the email open. I then go to finder and select a PDF file. I then go to the applescript editor and tell it to run the code. The applescript then tells me that the front window is not a draft window (the message from the code).
It is now working! Thank you! That is much faster!
I had to change the “≠” to “is not” to get it to work. Here is my version:
tell application "Finder" to set selectedItems to selection as alias list
if length of selectedItems = 1 then
set theAttachment to item 1 of selectedItems
else
error "Something wrong with Finder selection!"
end if
------------------------------------------------------------
tell application "Microsoft Outlook"
if class of front window is not draft window then
error "Front window is not a draft window!"
error errorString
else
tell front window
if its object = missing value then save
tell its object to make new attachment with properties {file:theAttachment}
end tell
end if
end tell
BTW: When you changed draft window to "draft window" you changed an AppleScript class name to literal text, and as you found out that doesn't work very well.