I'm working with Outlook for Mac 15.30(161251)
I've a script that saves all attachments to a folder. It recently stopped working and I'm trying to figure out why.
The old save command save theAttachment in file savePath
gives Microsoft Outlook got an error: An error has occurred.
I changed to save theAttachment in POSIX file savePath
and now I'm getting Microsoft Outlook got an error: Parameter error.
I've checked the path and it seems to be fine.
Any thoughts?
I can't see all your code (assuming there is more than these two lines) so I am unable to test it with your scenario.
However, I was able to find lots of forums with people having the same question.
Take a look at this code from this forum:
set saveToFolder to (choose folder with prompt "Choose the destination folder") as string
set prefix to the text returned of (display dialog "Enter the text to prefix the saved attachment names with" default answer "" buttons {"Cancel", "OK"} default button 2)
set ctr to 0
tell application "Microsoft Outlook"
set topFolder to mail folder "inbox" of on my computer
set srcFolder to mail folder "myfolder" of topFolder
set destFolder to folder "myFolder2" of topFolder
set selectedMessages to messages of srcFolder
repeat with msg in selectedMessages
set ctr to ctr + 1
set attFiles to attachments of msg
repeat with f in attFiles
set attName to (get the name of f)
log attName
set saveAsName to saveToFolder & prefix & ctr & attName
log saveAsName
save f in saveAsName
end repeat
move msg to destFolder
end repeat
end tell
display dialog "" & ctr & " messages were processed" buttons {"OK"} default button 1
return ctr
Here is a working example, including dealing with posix paths:
set saveToFolder to POSIX path of (choose folder with prompt "Choose the destination folder")
set ctr to 0
tell application "Microsoft Outlook"
set srcFolder to mail folder "SomeFolder" of on my computer
set selectedMessages to messages of srcFolder
repeat with msg in selectedMessages
set sentstamp to time sent of msg
set y to year of sentstamp
set m to month of sentstamp
set d to day of sentstamp
set rdate to y & "-" & m & "-" & d
set ctr to ctr + 1
set attFiles to attachments of msg
set actr to 0
repeat with f in attFiles
set attName to (get the name of f)
log attName
set saveAsName to saveToFolder & "mrp-" & rdate & "-" & actr & ".csv"
set actr to actr + 1
save f in POSIX file saveAsName
end repeat
end repeat
end tell
display dialog "" & ctr & " messages were processed" buttons {"OK"} default button 1
return ctr
Related
Thanks everyone for your help so far. And apologies I asked this in the 'answer' section of a previous question which I now understand I shouldn't have done ..... so I have started a new question here.
SO, I wanted to write a script to save the attachments in emails as they arrive - with a different folder for each email sender. I got a lot of help from people on this site.
It sort of works ..... for new incoming emails it works perfectly, but when I run it against my old emails in my mailbox it saves some attachments and not others.
I thought the problem was an error on finding a duplicate (which I thought would be unlikely as I have added the time stamp to the filename along with the data stamp of the email.) So I added the delFile delete process to check for a file of the same name and if it finds it to delete it.
When I execute the script it processes a few more attachments than before but not all by any means..... and interestingly nothing get put in the trash bin.
I am now stumped!! As a newcomer to AppleScript I don't know how to debug or handle errors yet.
Can anyone help please?
use scripting additions
using terms from application "Mail"
on perform mail action with messages messageList for rule aRule
set destinationPath to (POSIX file "/volumes/Data/Dropbox/WORK ITEMS/Email Attachments/") as string
tell application "Mail"
repeat with aMessage in messageList
repeat with anAttachment in mail attachments of aMessage
set senderName to (extract name from sender of aMessage)
set {year:y, month:m, day:d, hours:h, minutes:min} to date received of aMessage
set timeStamp to (d & "/" & (m as integer) & "/" & y & " " & h & "." & min) as string
set attachmentName to timeStamp & " - " & name of anAttachment
set doSave to true
set originalName to name of anAttachment
if originalName contains "jpg" then
set doSave to false
else if originalName contains "jpeg" then
set doSave to false
else if originalName contains "gif" then
set doSave to false
else if originalName contains "png" then
set doSave to false
else if originalName contains "html" then
set doSave to false
else if originalName contains "ics" then
set doSave to false
end if
if doSave is true then
tell application "System Events"
if not (exists folder (destinationPath & senderName)) then
make new folder at end of alias destinationPath with properties {name:senderName}
end if
end tell
end if
if doSave is true then
set delFile to destinationPath & senderName & ":" & attachmentName
tell application "System Events" to if (exists file delFile) then delete file delFile
end if
if doSave is true then save anAttachment in file (destinationPath & senderName & ":" & attachmentName) with replacing
end repeat
end repeat
end tell
end perform mail action with messages
end using terms from
Thanks
I'm not sure I can help with the precise reason for your script's failure, but I might be able to help you see where it's failing.
First of all, I would substitute a list of file extensions that you wish to exclude for the long if...else if block. Something like:
set ignore list to {".jpg", ".jpeg", ".gif", ".png", ".html", ".ics"} at the top of the script, with set fileExtension to rich text (offset of "." in originalName) thru end of originalName in the loop.
You can then test:
if fileExtension is not in ignoreList then
and wrap this around the saving code (you don't need to do the same test several times).
I think your delete file block is redundant, because it should be doing the same as the following save...with replacing (if the file's already there). (You may want to delete the file if it exists, in which case remove the with replacing later on.)
To start debugging, first of all remove the code up top that works with incoming messages and replace it with set messageList to selection. Try inserting some display dialog <varname> in places where you're not sure what's happening. For example, you know what anAttachment is but are you certain what destinationPath & senderName & ":" & attachmentName are?
Finally, note that I have NOT run this on YOUR data, so be sure to do a back-up. It shouldn't destroy anything, but better safe than sorry!
Please come back with any questions. Good luck!
EDIT:
I have added a function at the top (the on getExtension(fileName) block. This is called by the line set fileExtension to my getExtension(originalName)
This is to refine extension getting by reversing the name string, so that only the first '.' is found. Once got the extension is reversed.
Another crucial part is that this contains try ... on error ... end try. This is how AppleScript does error handling. If there is no '/' an error is thrown. This is caught by on error which returns 'skip'. (At this point this is not used in the main program, but could be used to funnel all the output to a catchall folder.)
The final change is that I have wrapped the saving part in If originalName does not contain "/" then ... end if. This is to catch those files that contain a '/' and to 'jump over' them without doing anything.
I have NOT needed to add a delay, so try without one to start with. It might have been a red herring!
set ignoreList to {".jpg", ".jpeg", ".gif", ".png", ".html", ".ics"}
set destinationPath to (POSIX file "/volumes/Data/Dropbox/WORK ITEMS/Email Attachments/") as string
on getExtension(fileName)
try
set fileName to (reverse of every character of fileName) as string
set extension to text 1 thru (offset of "." in fileName) of fileName
set extension to (reverse of every character of extension) as string
return extension
on error
return "skip"
end try
end getExtension
tell application "Mail"
set messageList to selection
repeat with aMessage in messageList
repeat with anAttachment in mail attachments of aMessage
set senderName to (extract name from sender of aMessage)
set {year:y, month:m, day:d, hours:h, minutes:min} to date received of aMessage
set timeStamp to (d & "/" & (m as integer) & "/" & y & " " & h & "." & min) as string
set attachmentName to timeStamp & " - " & name of anAttachment
set originalName to name of anAttachment
if originalName does not contain "/" then
set fileExtension to my getExtension(originalName)
if fileExtension is not in ignoreList then
tell application "System Events"
if not (exists folder (destinationPath & senderName)) then
make new folder at end of alias destinationPath with properties {name:senderName}
end if
end tell
save anAttachment in file (destinationPath & senderName & ":" & attachmentName) with replacing
end if
end if
end repeat
end repeat
end tell
For calling from a mail rule:
use scripting additions
set ignoreList to {".jpg", ".jpeg", ".gif", ".png", ".html", ".ics"}
set destinationPath to (POSIX file "/Users/bernardharte/test/") as string
on getExtension(fileName)
try
set fileName to (reverse of every character of fileName) as string
set extension to text 1 thru (offset of "." in fileName) of fileName
set extension to (reverse of every character of extension) as string
return extension
on error
return "skip"
end try
end getExtension
using terms from application "Mail"
on perform mail action with messages messageList for rule aRule
tell application "Mail"
repeat with aMessage in messageList
repeat with anAttachment in mail attachments of aMessage
set senderName to (extract name from sender of aMessage)
set {year:y, month:m, day:d, hours:h, minutes:min} to date received of aMessage
set timeStamp to (d & "/" & (m as integer) & "/" & y & " " & h & "." & min) as string
set attachmentName to timeStamp & " - " & name of anAttachment
set originalName to name of anAttachment
if originalName does not contain "/" then
set fileExtension to my getExtension(originalName)
if fileExtension is not in ignoreList then
tell application "System Events"
if not (exists folder (destinationPath & senderName)) then
make new folder at end of alias destinationPath with properties {name:senderName}
end if
end tell
save anAttachment in file (destinationPath & senderName & ":" & attachmentName) with replacing
end if
end if
end repeat
end repeat
end tell
end perform mail action with messages
end using terms from
this code almost works. :) Basically I am trying to zip a file and then attach that email to a new mail message.
The process starts in automator and then the script runs. Everything works except the actual attaching of the file.
My knowledge of AppleScript is pretty limited but trying to wrap my head around this. I combined to different code snippets to get this far.
on run {input, parameters}
set display_text to "Please enter your password:"
repeat
considering case
set init_pass to text returned of (display dialog display_text default answer "" with hidden answer)
set final_pass to text returned of (display dialog "Please verify your password below." buttons {"OK"} default button 1 default answer "" with hidden answer)
if (final_pass = init_pass) then
exit repeat
else
set display_text to "Mismatching passwords, please try again"
end if
end considering
end repeat
tell application "Finder"
set theItems to selection
set theItem to (item 1 of input) as alias
set itemPath to quoted form of POSIX path of theItem
set fileName to name of theItem
set theFolder to POSIX path of (container of theItem as alias)
set zipFile to quoted form of (fileName & ".zip")
do shell script "cd '" & theFolder & "'; zip -x .DS_Store -r0 -P '" & final_pass & "' " & zipFile & " ./'" & fileName & "'"
end tell
tell application "Finder"
#I believe this is where the problem is, I am trying to use the variables from above in order to attach the file in mail.
set folderPath to theFolder
set theFile to zipFile
set fileName to zipFile
end tell
set theSubject to fileName
set theBody to "Hello sir. Here is my " & fileName
set theAddress to "Some Email"
set theAttachment to theFile
set theSender to "Some Sender"
tell application "Mail"
set theNewMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return, visible:true}
tell theNewMessage
set visibile to true
set sender to theSender
make new to recipient at end of to recipients with properties {address:theAddress}
try
make new attachment with properties {file name:theAttachment} at after the last word of the last paragraph
set message_attachment to 0
on error errmess -- oops
log errmess -- log the error
set message_attachment to 1
end try
log "message_attachment = " & message_attachment
#send
end tell
end tell
return input
end run
I've fixed up most of the things that are off with your code. The following works in an automator Run AppleScript action if you pass in a list containing an alias to a file:
on run {input, parameters}
set display_text to "Please enter your password:"
repeat
considering case
set init_pass to text returned of (display dialog display_text default answer "" with hidden answer)
set final_pass to text returned of (display dialog "Please verify your password below." buttons {"OK"} default button 1 default answer "" with hidden answer)
if (final_pass = init_pass) then
exit repeat
else
set display_text to "Mismatching passwords, please try again"
end if
end considering
end repeat
tell application "System Events"
set selected_file to item 1 of input
set selected_file_name to name of selected_file
set containing_folder_path to POSIX path of (container of selected_file) & "/"
set zip_file_path to containing_folder_path & selected_file_name & ".zip"
do shell script "cd " & quoted form of containing_folder_path & "; zip -x .DS_Store -r0 -P " & quoted form of final_pass & " " & quoted form of zip_file_path & " " & quoted form of selected_file_name
set zip_file_alias to file zip_file_path as alias
end tell
set theSubject to selected_file_name
set theBody to "Hello sir. Here is my " & selected_file_name
set theAddress to "Some Email"
set theSender to "Some Sender"
tell application "Mail"
set theNewMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return, visible:true}
tell theNewMessage
set visibile to true
set sender to theSender
make new to recipient at end of to recipients with properties {address:theAddress}
try
make new attachment with properties {file name:zip_file_alias} at end of attachments
on error errmess -- oops
log errmess -- log the error
end try
log "message attachment count = " & (count of attachments)
#send
end tell
end tell
return input
end run
Highlights:
I switched from using the Finder to using System Events. Avoid using the Finder unless you absolutely need to (it's a very busy app)
I set the name property of the attachment to an alias to the compressed file the script creates
I cleaned up this and that to make things clearer.
Here is what I ultimately came up with, which worked but the file handling for the attachment still needed work.
on run {input, parameters}
tell application "Finder"
set theItem to (item 1 of input) as alias
set theItemCopy to theItem
set itemPath to quoted form of POSIX path of theItem
set fileName to name of theItem
set theFolder to POSIX path of (container of theItem as alias)
set zipFile to quoted form of (fileName & ".zip")
set setPass to "Password" #test password
do shell script "cd '" & theFolder & "'; zip -x .DS_Store -r0 -P '" & setPass & "' " & zipFile & " ./'" & fileName & "'"
end tell
tell application "Mail"
set fileLocation to (fileName & ".zip")
set theSubject to "Subject" -- the subject
set theContent to "Attached are the documents from the last session." -- the content
set theAddress to "email#email.com" -- the receiver
set theAttachmentFile to "Macintosh HD:Users:dave:desktop:" & fileLocation -- the attachment path
set msg to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
delay 1
tell msg to make new to recipient at end of every to recipient with properties {address:theAddress}
tell msg to make new attachment with properties {file name:theAttachmentFile as alias}
end tell
return input
end run
Still a Noob / novice here
So I am trying to rename a file within a folder
set newfoldername to DT & "-" & JobName & " - " & Wedding --Adds Date, Couples name and event type to make directory name
MORE CODE HERE
set destFolder to loc & newfoldername & ":Final Delivery:DVD:"
tell application "Finder"
duplicate file sourcedvdtemp to folder destFolder
set the name of file "DVD_template.dspproj" to newfoldername
end tell
But I just get an error
"error "Finder got an error: Can’t set file \"DVD_template.dspproj\" to \"newfoldername.dspproj\"." number -10006 from file "DVD_template.dspproj""
I have tried various ways, with and without the extension "newfoldername" is set above and creates a folder with the users input as a name
Am I just being stupid or have I missed something completely.
EDIT
tell application "Finder" to set frontmost to true
set DT to text returned of (display dialog "Please enter the wedding date" default answer "YYYY/MM/DD") --Asks for date of wedding
set JobName to text returned of (display dialog "Please enter the couples name" default answer "Bride & Groom + Surname") --Asks for couples name
set Wedding to text returned of (display dialog "Please enter the type of day" default answer "Wedding") --Asks for type of day, Wedding, Party, etc
--Creates directory with info from above
set loc to (choose folder "Please choose where you would like to save the files") as text --Loc = Location of where directory will be placed
set newfoldername to DT & "-" & JobName & " - " & Wedding --Adds Date, Couples name and event type to make directory name
set folderStructure to "/{Audio/{Music,RAW\\ Audio\\ Cards},Documents,Exports,FCPX\\ Library,Film\\ Poster,Final\\ Delivery/{DVD,USB,WEB},Images/{Graphics,Stills},RAW\\ Cards/{C100-1,C100-2,7D,100D},XML}"
do shell script "mkdir -p " & quoted form of POSIX path of (loc & newfoldername) & folderStructure
set sourcedvdtemp to (path to movies folder as text) & "## - WeddingTemplate - ##:DVD_template.dspproj" --Gets file DVD_template
set sourceusbtemp to (path to movies folder as text) & "## - WeddingTemplate - ##:USB_template.zip" --Gets file USB_template
set sourcewebtemp to (path to movies folder as text) & "## - WeddingTemplate - ##:WEB_template.zip" --Gets file WEB_template
set sourcefcpxtemp to (path to movies folder as text) & "## - WeddingTemplate - ##:FCPX_template.fcpbundle" --Gets file FCPX_template
set sourcefilmpostemp to (path to movies folder as text) & "## - WeddingTemplate - ##:FILM_POSTER_Template.psd" --Gets file FILM_POSTER_template
set destFolder to loc & newfoldername & ":Final Delivery:DVD:"
tell application "Finder"
duplicate file sourcedvdtemp to folder destFolder
set the name of file "DVD_template.dspproj" to newfoldername
end tell
set destFolder to loc & newfoldername & ":FCPX Library:"
tell application "Finder"
duplicate file sourcefcpxtemp to folder destFolder
set the name of file "FCPX_template.fcpbundle" to newfoldername
end tell
set destFolder to loc & newfoldername & ":Film Poster:"
tell application "Finder"
duplicate file sourcefilmpostemp to folder destFolder
set the name of file "FILM_POSTER_Template.psd" to newfoldername
end tell
You are trying to rename a file "DVD_template.dspproj" on desktop which obviously does not exist. The desktop folder is the root folder of the Finder.
The duplicate command of the Finder returns the duplicated file so it's simply
tell application "Finder"
set fileExtension to name extension of file sourcedvdtemp
set duplicatedFile to duplicate file sourcedvdtemp to folder destFolder
set name of duplicatedFile to newfoldername & "." & fileExtension
end tell
If you want to rename the file to newfoldername you have to copy and add the file extension.
PS: Instead of calculating the template folder 5 times I recommend to write
set weddingTemplateFolder to (path to movies folder as text) & "## - WeddingTemplate - ##:"
set sourcedvdtemp to weddingTemplateFolder & "DVD_template.dspproj" --Gets file DVD_template
set sourceusbtemp to weddingTemplateFolder & "USB_template.zip" --Gets file USB_template
... etc.
I'm attempting to get all my pictures out of the iPhotos and Photos app.
I found this script to export all the pictures from iPhotos. But it doesn't work when i change it to use the Photos app.
set destination to quoted form of POSIX path of (path to pictures folder)
tell application "iPhoto"
repeat with i in (get selection)
tell i to my copyPhoto(date, image path, title, destination)
end repeat
end tell
on copyPhoto(d, p, t, dest) -- the name of the file is the title of the photo in iPhoto, date format for folder name = 2014-09-25
tell d to set d to "" & its year & "-" & text -2 thru -1 of ("0" & ((its month) as integer)) & "-" & text -2 thru -1 of ("0" & its day)
try -- create folder if neccessary, check to not overwrite any files in the subfolder, copy the file, rename with the title
do shell script "f=" & d & ";t=" & (quoted form of t) & ";tFile=" & (quoted form of p) & "; e=${tFile##*.}; cd " & dest & ";mkdir -p \"$f\"; while [ -e \"$f/$t.$e\" ];do t=\"$t _\";done; cp -a \"$tFile\" \"$f/$t.$e\""
end try
end copyPhoto
(from https://discussions.apple.com/thread/6565154?start=0&tstart=0)
Would someone be able to help me figure out how to do this with the Photos app?
The format that the script uses it perfect, pictures organized by folder:
2014-12-30
2015-01-01
2015-01-02
...
THANKS!!!!
Vadian is right, no "image path" in Photos (thanks Apple !), so must use Export. see script bellow which does this, also formatting date and creating folder if required.
set TopFolder to (path to desktop folder from user domain) as string -- your main destination folder
tell application "Photos"
repeat with aPhoto in (get selection)
set SName to filename of aPhoto
set theDate to date of aPhoto
-- convert date to string yyyy-mm-dd
set SY to (year of theDate) as string
set SM to text -2 thru -1 of ("0" & ((month of theDate) as integer))
set SD to text -2 thru -1 of ("0" & ((day of theDate) as string))
set DestFolder to SY & "-" & SM & "-" & SD -- DestFolder = folder "date"
-- check if folder DestFolder exists : if not creation
tell application "Finder"
if not (folder (TopFolder & DestFolder) exists) then
make new folder in TopFolder with properties {name:DestFolder}
end if
end tell
-- you may have to add a check if file (TopFolder & DestFolder & SName) already exists
-- and take appropriate action (add index, add word "copy",...)
export {aPhoto} to (TopFolder & DestFolder)
end repeat
end tell
During tests, I discovered that Photos is much slower than iPhoto. Just to get Date or filename property it takes time ! Also "Export" does. the script itself could be optimized using shell functions for date formatting or folder check/creation (I am not shell expert), but the speed bottleneck remains Photos !
all folder & subfolder of outlook (list below) pass in applescript variable(selectedMessages).
any way to pass one by one or all folder in applescript (using applescript).
folder structure of outlook mail folder :
SIGMACO
Inbox
-McAfee Anti-Spam
Drafts
Sent Items
Deleted Items
Junk E-mail
RSS Feeds
Sync Issues
-Conflicts
-Local Failures
ON MY COMPUTER
Inbox
Drafts
Sent Items
Deleted Items
Junk E-mail
archive PST
-Deleted Items
-Sent Items
SMART FOLDERS
Flagged Mail
+++++++++++
tell application "Microsoft Outlook"
with timeout of 8.8E+8 seconds
set selectedMessages to every messagge of folder "deleted Items" of on my coputer
end timeout
end tell
find some code for list foldeer and subfoldeeer.
global AllFolders
set AllFolders to {}
global ContainerName
set ContainerName to ""
tell application "Microsoft Outlook"
set allMailFolders to get mail folders
repeat with currentFolder in allMailFolders
set FolderName to name of currentFolder
set PathSoFar to ""
if FolderName is not missing value and (container of currentFolder is not missing value or account of currentFolder is not missing value) then
set ContainerName to ":"
set theContainer to currentFolder
repeat while ContainerName is not ""
set ContainerName to ""
try
set theContainer to container of theContainer
set ContainerName to name of theContainer
if ContainerName is missing value then
set theAccount to account of theContainer
if theAccount is not missing value then
set AccountName to name of theAccount
set PathSoFar to (AccountName) & ":" & PathSoFar
end if
else
set PathSoFar to (ContainerName) & ":" & PathSoFar
end if
end try
end repeat
set end of AllFolders to {PathSoFar & ":" & (FolderName)}
end if
end repeat
return AllFolders
end tell