How to download an attachment from mail via applescript and mail rules? - email

I automatically receive a lot of vcards via email and I want to import them automatically into my contacts.
I coded an AppleScript that will open the vcard file and import the contact. But first I need to download the file, right?
But how can I download an attachment file from an email using AppleScript and rules in mail?
Thank you.
Cheers,
Chris

The script bellow saves the files attached in a selection of emails into a destination folder. you can then use these files to add them in Address Book.
set Dest to ((path to desktop folder) as string) & "FMail:" -- the folder to save attached files
tell application "Mail"
activate
set ListMessage to selection -- take all emails selected
repeat with aMessage in ListMessage -- loop through each message
set AList to every mail attachment of aMessage
repeat with aFile in AList --loop through each files attached to an email
if (downloaded of aFile) then -- check if file is already downloaded
set Filepath to Dest & (name of aFile)
save aFile in Filepath as native format
end if
end repeat -- next file
end repeat -- next message
end tell
I added many comments to make it clear. then you will be able to adapt it to your needs.

For Mail Version 8.2 (2104) in macOS 10.10.5, you need to download to the download folder. Thus, I change the first line of pbell's solution to
set Dest to ((path to home folder) as string) & "Downloads:" -- the folder to save attached files
(*
Mail Version 8.2 (2104) year 2015
macOS 10.10.5
*)
set Dest to ((path to home folder) as string) & "Downloads:" -- the folder to save attached files
log "Dest is " & Dest
tell application "Mail"
activate
set ListMessage to selection -- take all emails selected
repeat with aMessage in ListMessage -- loop through each message
set AList to every mail attachment of aMessage
repeat with aFile in AList --loop through each files attached to an email
if (downloaded of aFile) then -- check if file is already downloaded
set Filepath to Dest & (name of aFile)
log "Filepath is " & Filepath
save aFile in Filepath as native format
end if
end repeat -- next file
end repeat -- next message
end tell

Related

Setting path for AHK's FileSelectFile function not working

My goal is to create a script that will allow me to paste images into a social media platform's new post instance. This platform allows pasting images from Clipboard.
My logic is:
Open the File Select modal with a specific default path.
Select a file by user.
Pass the selected object to the Clipboard.
Send the Paste command back into the Post instance.
I was able to complete all of this; the only issue I'm having is setting the absolute starting path of the File Select modal; no matter what I do, or how I pass the absolute path, it seems to always open the path of the last physical action outside the script, in other words, if I physically opened a Explorer from Windows itself or any other app, it will use that path, instead of the one I mentioned in the script; it's been driving me nuts. I've rebooted the script, refreshed everything, cleared cache, did a rain dance.
Would love some assistance!
Here's my script:
^+x::
mypath := "C:\Users\MyComputerName\Downloads\SOCIAL-MEDIA\Images"
FileSelectFile, SelectedFile, 3, %mypath%
if (SelectedFile = "")
MsgBox, The user didn't select anything.
else
pToken := Gdip_Startup()
Gdip_SetBitmapToClipboard(pBitmap := Gdip_CreateBitmapFromFile(SelectedFile))
Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pToken)
send ^v
return
It seems that setting the third parameter to "2" aka "Path must exist," did the trick.
https://www.autohotkey.com/docs/v1/lib/FileSelectFile.htm

AppleScript iPhoto import command (how to deal with duplicates)

I'm trying to fully automate a workflow for getting images into iPhoto. The last piece of the puzzle is handling duplicate images. Currently I see no way in Applescripts import command to not import duplicates. Therefore when a duplicate arrises all workflow stops and iPhoto waits for input.
Is there a way to get through this?
You will need to script this. Here is a bit of code that seems to work inside applescript editor. Adjust accordingly for automator...
set iFolder to (choose folder)
set iFiles to (list folder iFolder)
tell application "iPhoto"
repeat with iFile in iFiles
try
set pFound to get (every photo of album "Photos" whose image filename is iFile)
end try
if length of pFound is not 0 then
log ("File '" & iFile as text) & "' exists..."
# Move or delete it here
end if
end repeat
# Continue with import
import from (iFolder as alias)
end tell

Applescript: "set user interaction level to never interact" not working in illustrator

I'm trying to set it so that there's no user interaction when I open up my illustrator file using applescript, but the standard:
tell application id "com.adobe.Illustrator"
activate
set user interaction level to never interact
open theFile without dialogs
doesn't work for this plugin I have installed that checks for white overprints.
If it were up to me I'd just uninstall the plugin but it's for a work pc.
I also tried clicking the button automatically (with help from Tim Joe) by using:
try
tell application "System Events"
tell process "Finder"
click button "OK" of window "Adobe Illustrator"
end tell
end tell
end try
and I've tried
tell application "System Events"
tell process "Adobe Illustrator"
keystroke return
end tell
end tell
Does anyone know a way of solving this?
below is the full code as it currently stands:
set saveLocation to ((path to desktop) as string) --place to save the files
set theFile to choose file with prompt "Choose the Illustrator file to get outlines on"
set outputFolder to choose folder with prompt "Select the output folder"
tell application "Finder" to set fileName to name of theFile
set fullPath to (saveLocation & fileName) --file path of new .ai
set fileName to (text 1 thru ((length of fileName) - 3) of fileName) --remove .ai from fileName
set olPath to text 1 thru ((length of fullPath) - 3) of fullPath & "_OL.ai" --path of outlined file
tell application id "com.adobe.Illustrator"
activate
ignoring application responses
open theFile without dialogs
end ignoring
tell application "System Events"
tell process "Adobe Illustrator"
repeat 60 times -- wait up to 60 seconds for WOPD window to appear
try
tell window "White Overprint Detector"
keystroke return
exit repeat
end tell
on error
delay 1
end try
end repeat
end tell
end tell
save current document in file fullPath as Illustrator with options {class:Illustrator save options, compatibility:Illustrator 15, font subset threshold:0.0, embed linked files:true, save multiple artboards:false} --save file to desktop
convert to paths (every text frame of current document) --convert text to paths
save current document in file olPath as Illustrator with options {class:Illustrator save options, compatibility:Illustrator 15, font subset threshold:0.0, embed linked files:true, save multiple artboards:false} --save another copy to desktop with name + _OL.ai
end tell
tell application "Finder"
set newFolder to make new folder at saveLocation with properties {name:fileName}
move fullPath to newFolder --create new folder and move both new files into it
move olPath to newFolder
set newFolderPath to (newFolder) as string
set newFolderPath to text 1 thru -2 of newFolderPath --remove the trailing ":"
tell current application --zip up the new folder
set qpp to quoted form of POSIX path of newFolderPath
do shell script "cd $(dirname " & qpp & ")
zip -r \"$(basename " & qpp & ").zip\" \"$(basename " & qpp & ")\""
end tell
set zipFile to newFolderPath & ".zip"
move zipFile to outputFolder --move .zip to output
delete newFolder --delete folder on desktop left from zipping
end tell
--prepare a notification email
set presetText to "Hello,
Files Uploaded:
" & fileName & ".zip
To access our FTP Server:
http://217.207.130.162:8080/WebInterface/login.html
To access our FTP server, log onto our website below:
Username:
Password:
Thanks,
Joe"
tell application "Mail" --open up prepared email
activate
set theMEssage to make new outgoing message with properties {visible:true, subject:fileName, content:presetText}
end tell
--open file containing usernames and passwords for the FTP
do shell script "open /Users/produser/Desktop/FTP_Users"
I tracked down and installed White Overprint Detector I could see what you mean. I had to use an older version as I only have CS3, and I saw the dialog it produces when you open a document. The following worked for me to get it to dismiss:
tell application "Adobe Illustrator" to activate
tell application "System Events"
tell process "Adobe Illustrator"
repeat 60 times -- wait up to 60 seconds for WOPD window to appear
try
tell window "White Overprint Detector"
keystroke return
exit repeat
end tell
on error
delay 1
end try
end repeat
end tell
end tell
Since my original post seemed too objective to understand I will revise.
With in the tell block for illustrator look for your line that opens the file. Some commands allow with and without properties. Try applying the "without dialogs" property to look something like this.
tell application id "com.adobe.Illustrator"
open file (VariableOfFilePath) without dialogs
end tell
Update:
Two work arounds I can think of. 1) Try telling system events to tell AI to open without dialogs
tell application "system events"
tell application id "com.adobe.Illustrator"
open file (VariableOfFilePath) without dialogs
end tell
end tell
Other is just add in a bit that will just okay the prompt.
try
tell application "System Events"
tell process "Finder"
click button "Continue" of window "Adobe Illustrator"
end tell
end tell
end try
Can try just having it accept the default button.
tell application "System Events"
tell process "Finder"
keystroke return
end tell
end tell

How to automatically track links in org-mode to gnus messages?

I use org-mode + gnus + Gmail for my daily GTD routine. The concept is that treating all incoming messages as tasks, and converting all messages in INBOX into org-mode's tasks using org-capture. Once all new messages are converted into tasks, archive them, and hopefully INBOX is kept zero.
My workflow is as follows:
Open the summary view of gnus INBOX, and select a new message
Capture the message with org-store-link (C-c l)
Open my todo file (todo.org), and create a new task for it, and paste the captured link to the task's body with org-insert-link (C-c C-l)
Go back to gnus summary view and archive the message (B del)
The problem is that when moving a message into the archive folder, the captured link becomes broken, and I cannot follow the link anymore. This is because the captured link includes the IMAP folders' name and archiving message changes the message's IMAP folder name. E.g.,
Captured link: [[gnus:nnimap%2Blocalhost:%5BGmail%5D.Important#1364607772002.9702fb8c#Nodemailer][Email from Geeklist Team: Geekli.st Suggestions & Activi]] (IMAP folder name is "[Gmail]Important")
Link to archived message: [[gnus:nnimap%2Blocalhost:%5BGmail%5D.All Mail#1364607772002.9702fb8c#Nodemailer][Email from Geeklist Team: Geekli.st Suggestions & Activi]] (IMAP folder name is "[Gmail]All Mail")
So my question is: how can I update the captured link automatically when the message is moved to other folders? I guess there are some hooks to do this, but I could not find a good sample for this purpose. Or any simpler solutions for this kind of routine are welcome. TIA.
I do not use 'org-store-link' and 'org-insert-link' but a capture template, that automatically generates a link to the message (%a below). So you do not have to switch buffers to store a TODO entry:
(setq org-capture-templates
'(
("m" "TODO from Mail" entry (file+headline "~/gitfiles/org/gtd.org" "Inbox")
"* TODO %?, Link: %a")))
Since all my emails arrive in the INBOX and are archived in the folder "Archive" I can just use the following function which replaces the string 'INBOX' by 'Archive' in the Org mode link in the capture buffer:
(defun hs/replace ()
(interactive)
(goto-char 1)
(replace-string "INBOX" "Archive"))
This hook calls the function when I hit C-c C-c to file the capture entry:
(add-hook 'org-capture-prepare-finalize-hook 'hs/replace)
So, my workflow is as follows:
Select a message in Summary buffer
Hit C-c c m to capture a TODO item with Link to message and write a description (since the message is still in the inbox, the generated link contains the group "INBOX")
Hit C-c C-c to file the TODO entry (this calls the function 'hs/replace' which replaces the string INBOX by Archive)
Archive the email in the archive folder.
HTH

Simple script no longer works on Lion

I have a simple Applescript that takes a number of images in a folder that you select and then mirror images the images using Graphic Converter. This script will run if I place it in a new AS file; however, if I try to run it a second time I get the following error "Can't get window 1 of application "GraphicConverter". Invalid index."
This script always ran on OSX 10.6
I'm running OSX 10.7.4 and Graphic Converter 8.1 (latest version).
Here is the script
tell application "Finder"
activate
set loopFinish1 to 0
set pathName to (choose folder with prompt "Choose Folder Containing Images")
set fileList1 to every file of folder pathName
set loopFinish1 to count of items of fileList1
end tell
tell application "GraphicConverter"
activate
repeat with i from 1 to loopFinish1
set currentFile to item i of fileList1
open currentFile as alias
mirror window 1 in horizontal
close window 1 saving yes
end repeat
end tell
This is driving me crazy!
GraphicConverter has other windows (visible and invisible), it's preferable to use the document to get the right window.
Also, perhaps the image doesn't open, so no window, use a try block.
activate
set pathName to (choose folder with prompt "Choose Folder Containing Images")
tell application "Finder" to set fileList1 to (document files of folder pathName) as alias list
tell application "GraphicConverter"
activate
repeat with tFile in fileList1
set currentDoc to open tFile
set CurrWindow to (first window whose its document is currentDoc)
mirror CurrWindow in horizontal
close currentDoc saving yes
end repeat
end tell