Get reference to an Outook message duplicate - outlook

This code will duplicate a Microsoft Outlook message to the drafts folder:
tell application "Microsoft Outlook"
...
-- find the template give an ID
set theTemplate to message id theID
-- duplicate the message
duplicate theTemplate to drafts
...
end tell
I need a reference to the duplicate for additional processing.
Unfortunately, this doesn't work:
...
-- this will create a duplicate
set theDuplicate to (duplicate theTemplate to drafts)
-- produces an error that reads "The variable theDuplicate is not defined."
display dialog (subject of theDuplicate) & " [" & (id of theDuplicate) & "]"
How do I get a reference to the message that was just duplicated? Its ID would be a satisfactory alternative.

There must be a better way but...
--For Testing
set theID to 39110
tell application "Microsoft Outlook"
set oldIds to my getDraftIds()
-- find the template give an ID
set theTemplate to message id theID
--duplicate the message
duplicate theTemplate to drafts
set newIds to my getDraftIds()
set duplicatedMessage to message id (my findNewID(oldIds, newIds))
end tell
on getDraftIds()
set messageIDs to {}
tell application "Microsoft Outlook"
set draftFolders to (every folder whose name = "Drafts")
repeat with dFolder in draftFolders
set messageIDs to messageIDs & id of dFolder's messages
end repeat
end tell
end getDraftIds
on findNewID(oldList, newList)
repeat with mID in newList
if mID is not in oldList then return mID
end repeat
end findNewID

I guess the duplicate method is pretty limited in this regard. I tried the more generic copy method too - the message is copied, but again no ID is returned.
Here's another possible way to do this with no repeat loops:
Create a new, temporary category
Mark the original message with the new category
Duplicate - the duplicate message will also be marked with the category
search for messages marked with the temporary category - you should only get two - the original and the duplicate
Delete the temporary category
Here is the code (rather unfinished):
tell application "Microsoft Outlook"
try
set tempCategory to category "Temporary Category"
on error number -1728
set tempCategory to (make new category with properties {name:"Temporary Category"})
end try
set messageList to selected objects
set origMsg to item 1 of messageList
display dialog "Original Message ID: " & id of origMsg
set msgCat to category of origMsg
set end of msgCat to tempCategory
set category of origMsg to msgCat
duplicate origMsg to drafts
delay 1 -- sigh, it seems to take a bit of time before the category markings are reflected in the spotlight DB
--set msgList to messages whose category contains tempCategory
set currentIdentityFolder to quoted form of POSIX path of (current identity folder as string)
set tempCatMsgs to words of (do shell script "mdfind -onlyin " & currentIdentityFolder & " 'com_microsoft_outlook_categories == " & id of tempCategory & "' | xargs -I % mdls -name com_microsoft_outlook_recordID '%' | cut -d'=' -f2 | sort -u | paste -s -")
if item 1 of tempCatMsgs is (id of origMsg) as text then
set dupMsgId to item 2 of tempCatMsgs
else
set dupMsgId to item 1 of tempCatMsgs
end if
delete tempCategory
display dialog "Original Message ID: " & id of origMsg & return & "Duplicate Message ID: " & dupMsgId
end tell
I thought it would be easier to find messages with a given category using the OL dictionary, but instead had to resort a spotlight search. I'm sure there is a better way to do this.
Also adding a category to a message was harder than I though - again I'm sure this can be done more efficiently.

-- create a temporary folder to hold duplicates
on createFolder(theName)
tell application "Microsoft Outlook"
set theFolder to make new mail folder with properties {name:theName}
end tell
end createFolder
-- find folder by name
on findFolder(theName)
tell application "Microsoft Outlook"
set theFolder to make new mail folder with properties {name:theName}
end tell
end findFolder
-- duplicate the message and get a reference
on duplicateIt(theID)
set theDestination to findFolder("foo bar")
tell application "Microsoft Outlook"
--find the template
set theTemplate to message id theID
-- duplicate it to the temporary location
(duplicate theTemplate to theDestination)
-- get the first item in the folder
set theDuplicate to item 1 of theDestination
-- do something with it
...
end tell
end duplicateIt

Related

Applescript error: Can't make class xxxx of application "Photos" into type list, record or text

I'm stumped and need some help debugging some AppleScript i have written. For my needs, I have exported my Apple Photos library to an external disk location, and written the media item ID of each photo to the metadata embedded in the file. This way, when the external file's keywords are updated, I can update Apple Photos to reflect that. Here's the relevant script:
tell application "Finder"
set theFile to "G-DRIVE:Media:photo:2015:12:2015-12-25_at_11h53m13s_DSC_0073.NEF" as alias
set fileString to my convertPathToPOSIXString(theFile)
set photosID to do shell script "/usr/local/bin/exiftool -config /Volumes/G-DRIVE/Scratch/customConfig -if 'defined $JustinApplePhotosID' -s -s -s -xmp:JustinApplePhotosID " & "'" & fileString & "'"
end tell
tell application "Photos"
set updateItems to (get media items whose id is photosID)
set numItems to the number of updateItems
if the number of updateItems is equal to 1 then
set mediaItem to the first item in updateItems
set keywordResult to do shell script "/usr/local/bin/exiftool -s -s -s -keywords " & "'" & fileString & "'"
set keywordArray to my theSplit(keywordResult, ",")
set the keywords of mediaItem to keywordArray
if keywordArray contains "5*****" and album "5 Stars" does not contain {mediaItem} then
add {mediaItem} to album "5 Stars"
else if keywordArray does not contain "5*****" and album "5 Stars" contains {mediaItem} then
syncSmartAlbum("5 Stars")
end if
end if
end tell
on syncSmartAlbum(dumbAlbumName)
tell application "Photos"
set smartAlbumName to dumbAlbumName & " Smart"
set smartAlbum to album smartAlbumName
tell album dumbAlbumName to if exists then delete
set newDumbAlbum to make new album named dumbAlbumName
add (get media items of smartAlbum) to newDumbAlbum
end tell
end syncSmartAlbum
Here's the result when run on this test file:
tell current application
offset of "." in "FEN.3700_CSD_s31m35h11_ta_52-21-5102/21/5102/otohp/aideM/EVIRD-G/semuloV/"
end tell
tell application "Finder"
get name extension of alias "G-DRIVE:Media:photo:2015:12:2015-12-25_at_11h53m13s_DSC_0073.NEF"
get name extension of alias "G-DRIVE:Media:photo:2015:12:2015-12-25_at_11h53m13s_DSC_0073.NEF"
do shell script "/usr/local/bin/exiftool -config /Volumes/G-DRIVE/Scratch/customConfig -if 'defined $JustinApplePhotosID' -s -s -s -xmp:JustinApplePhotosID '/Volumes/G-DRIVE/Media/photo/2015/12/2015-12-25_at_11h53m13s_DSC_0073.NEF'"
end tell
tell current application
do shell script "/usr/local/bin/exiftool -config /Volumes/G-DRIVE/Scratch/customConfig -if 'defined $JustinApplePhotosID' -s -s -s -xmp:JustinApplePhotosID '/Volumes/G-DRIVE/Media/photo/2015/12/2015-12-25_at_11h53m13s_DSC_0073.NEF'"
end tell
tell application "Photos"
get every media item whose id = "tshx+Vk5TFC47a8Q5EeCiw"
do shell script "/usr/local/bin/exiftool -s -s -s -keywords '/Volumes/G-DRIVE/Media/photo/2015/12/2015-12-25_at_11h53m13s_DSC_0073.NEF'"
end tell
tell current application
do shell script "/usr/local/bin/exiftool -s -s -s -keywords '/Volumes/G-DRIVE/Media/photo/2015/12/2015-12-25_at_11h53m13s_DSC_0073.NEF'"
end tell
tell application "Photos"
set keywords of media item id "tshx+Vk5TFC47a8Q5EeCiw" to {"4****"}
get album "5 Stars"
album id "b72G+JL2R5qyTJ%w0fAcqQ" contains {media item id "tshx+Vk5TFC47a8Q5EeCiw"}
get album id "b72G+JL2R5qyTJ%w0fAcqQ"
end tell
tell current application
current date
(*2019:11:16:13:18:08: Error reading or setting keywords for /Volumes/G-DRIVE/Media/photo/2015/12/2015-12-25_at_11h53m13s_DSC_0073.NEF with error: Can’t make «class IPal» id "b72G+JL2R5qyTJ%w0fAcqQ" of application "Photos" into type list, record or text.*)
open for access file "G-DRIVE:Scratch:Logs:UpdateKeywords.log" with write permission
write "2019:11:16:13:18:08: Error reading or setting keywords for /Volumes/G-DRIVE/Media/photo/2015/12/2015-12-25_at_11h53m13s_DSC_0073.NEF with error: Can’t make «class IPal» id \"b72G+JL2R5qyTJ%w0fAcqQ\" of application \"Photos\" into type list, record or text.
" to 273 starting at eof
close access 273
end tell
Result:
true
The error at issue appears to be: Can’t make «class IPal» id "b72G+JL2R5qyTJ%w0fAcqQ" of application "Photos" into type list, record or text
What am I doing wrong here??
Figured it out. Apparently can't just ask whether an album "contains" an item.
Here's the fix:
set FiveStarPhotos to (id of media items of album "5 Stars")
if keywordArray contains "5*****" and FiveStarPhotos does not contain {mediaItem} then
add {mediaItem} to album "5 Stars"
else if keywordArray does not contain "5*****" and FiveStarPhotos contains {mediaItem} then
syncSmartAlbum("5 Stars")
end if

Cannot set outlook message body on compose mode using appleScript

I am trying to get and set outlook message body on compose mode.setting value does not work. is something wrong with script ?. but get value is working fine.
activate application "Microsoft Outlook"
tell application "System Events"
tell process "Microsoft Outlook"
get value of static text 1 of group 1 of UI element 1 of scroll area 4 of splitter group 1 of window 1
set value of static text 1 of group 1 of UI element 1 of scroll area 4 of splitter group 1 of window 1 to "sample text"
end tell
end tell
I would avoid using UI scripting unless there is no other way.
This script shows you how to set the message body.
tell (current date) to get (it's month as integer) & "-" & day & "-" & (it's year as integer)
set MyDay to the result as text
set Mytitle to "Daily Email - " as text
set Mytitle to Mytitle & MyDay
tell application "Microsoft Outlook"
set newMessage to make new outgoing message with properties {subject:Mytitle}
make new recipient at newMessage with properties {email address:{name:"Name", address:"test#example.com"}}
#make new cc recipient at newMessage with properties {email address:{name:"Name", address:"test#example.com"}}
--- SET EMAIL BODY USING PLAIN TEXT ---
set plain text content of newMessage to "This is just a TEST."
open newMessage
end tell

How can I check for existence of Itunes track in Applescript without throwing error

the idea of this script (simplified to show the issue) is checks if a track exists,
tell application "iTunes"
set matchtrack to get first track in playlist 1 whose persistent ID is "F1A68AD90AA66648"
if matchtrack is not {} then
print name of matchtrack
else
print "no track found"
end if
end tell
unfortunately if the track does not exist it gives error
"iTunes got an error: Can’t get track 1 of playlist 1 whose persistent ID = \"F1A68AD90AA66648\"." number -1728 from track 1 of playlist 1 whose persistent ID = "F1A68AD90AA66648"
'
instead of just printing 'no track found'
get first track give a track object or an error
tracks give a list of one track or an empty list
tell application "iTunes"
set matchtrack to tracks in playlist 1 whose persistent ID is "F1A68AD90AA66648"
if matchtrack is not {} then
return name of item 1 of matchtrack
else
return "no track found"
end if
end tell
Simplest way would be to use a try block, like this:
set persistentID to "F1A68AD90AA66648"
tell application "iTunes"
try
set matchtrack to get first track in playlist 1 whose persistent ID is persistentID
if matchtrack is not {} then
log (get name of matchtrack)
else
log "no track found"
end if
on error the error_message number the error_number
log "Error (probably no track found)"
-- display dialog "Error: " & the error_number & ". " & the error_message buttons {"Cancel"} default button 1
end try
end tell
BTW: use log instead of print and (get name of matchtrack) instead of (name of matchtrack)
This would work but goes thru all the tracks in a named playlist:
set flagFound to false
set mTrack to ""
# SETUP THOSE FIRST
# -------------------------------------------------------
property Library_Name : "Library" # "Mediathek" in german
property Playlist_Name : "Bob Marley"
set persistentID to "F1A68AD90AA66648"
# -------------------------------------------------------
tell application "iTunes"
tell source Library_Name
tell playlist Playlist_Name
repeat with i from the (count of tracks) to 1 by -1
if (the persistent ID of track i is persistentID) then
set mTrack to track i
set flagFound to true
exit repeat
end if
end repeat
end tell
end tell
if flagFound then
tell mTrack
log "Found…"
log "Name: " & (get name)
log "persistent ID: " & (get persistent ID)
end tell
else
log "no track with persistent ID " & persistentID & " found"
end if
end tell

Mysterious AppleScript error: "4.294967323E+9Mahabalipuram" doesn’t understand the “write” message

A user of a script of mine which communicates via AppleScript with iPhoto is getting this error, which I can't reproduce: 918:955: execution error: iPhoto got an error: "4.294967323E+9Mahabalipuram" doesn’t understand the “write” message. (-1708)
The AppleScript that produces the error is:
set nul to character id 0
set text item delimiters to nul
set albumsFile to "/Users/[user]/Downloads/blah.blah"
set fp to open for access (POSIX file albumsFile) with write permission
tell application "iPhoto"
repeat with anAlbum in albums
if anAlbum's type is regular album then
set albumName to anAlbum's name
if albumName is not "Last Import" then
set albumPhotoIds to (id of every photo of anAlbum) as Unicode text
if length of albumPhotoIds is greater than 0 then
set currentAlbum to anAlbum
repeat while currentAlbum's parent exists
set currentAlbum to currentAlbum's parent
set albumName to currentAlbum's name & " > " & albumName
end repeat
set albumId to anAlbum's id
set albumData to {"", albumId, albumName, ""} as Unicode text
write albumData to fp as Unicode text
write albumPhotoIds to fp as Unicode text
write nul to fp as Unicode text
end if
end if
end if
end repeat
end tell
close access fp
Does anyone have any ideas as to what is going wrong here? There's a bit more background in this Github issue: https://github.com/jawj/iphoto-flickr/issues/7
This may work (untested); it comes up typically with this kind of error. But, as adayzone points out, it is probably best to re-structure the script.
tell me to write albumData to fp as Unicode text
tell me to write albumPhotoIds to fp as Unicode text
tell me to write nul to fp as Unicode text
It's also good to illustrate how tell works (and sometimes "gets in the way")
Write is from StandardAdditions, not iPhoto, so you can't tell iPhoto to write. It would be something along the lines of:
property nul : character id 0
set text item delimiters to nul
set albumsFile to (path to downloads folder as text) & "blah.txt"
tell application "iPhoto"
repeat with anAlbum in albums
if anAlbum's type is regular album then
set albumName to anAlbum's name
if albumName is not "Last Import" then
set albumPhotoIds to (id of every photo of anAlbum)
if length of albumPhotoIds is greater than 0 then
set currentAlbum to anAlbum
repeat while currentAlbum's parent exists
set currentAlbum to currentAlbum's parent
set albumName to currentAlbum's name & " > " & albumName
end repeat
set albumId to anAlbum's id
set albumData to {"", albumId, albumName, ""} as Unicode text
my writeIt(albumsFile, albumData, albumPhotoIds)
end if
end if
end if
end repeat
end tell
on writeIt(albums_File, album_Data, album_PhotoIds)
try
set fp to open for access albums_File with write permission
write album_Data to fp
write album_PhotoIds to fp
write nul to fp
close access fp
on error
try
close access fp
end try
end try
end writeIt

Why doesn't this simple applescript work any more in ML mail.app (as a rule)

It works only when you right click on the mail message and choose "run rules", but not on incoming messages (without interaction).
The first dialog is shown both when incoming or running manually, but the second dialog (with the id) is only shown when running manually. Nothing is shown in console.log
Any ideas?
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
repeat with theMessage in theMessages
display dialog "inside"
set theId to id of theMessage
display dialog "the id is " & theId
end repeat
end tell
end perform mail action with messages
end using terms from
update: i added a try catch block around
set theId to id of theMessage
and this is the error I get:
Can't get class mssg 1 of class mbxp "Incoming POP messages" of class mact "Telenet". Invalid index. -1719
Any idea what this means? I don't get the error when applying rules manually.
Update 2: OK i found out that incoming messages don't have an ID yet. That's a problem since I want to save the email to disk:
set theEmail to (do shell script "mdfind -onlyin ~/Library/Mail \"kMDItemFSName = '" & theId & ".emlx'\"")
set archiveName to theId & "-" & (extract address from theMessage's sender) & ".emlx"
set saveLocation to "Users:wesley:Documents:Incoming:"
do shell script "cp '" & theEmail & "' '" & POSIX path of saveLocation & "';"
Is there any way around this?
As promised in my comment, here's an AppleScript that logs Mail messages' subject, id, and message id to ~/Desktop/log.txt.
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
try
repeat with theMessage in theMessages
LogText("subject: " & (theMessage's subject as string))
LogText("id: " & (theMessage's id as string))
LogText("message id: " & (theMessage's message id as string))
LogText("")
end repeat
on error errorText number errorNumber
tell application "Mail" to display alert ("Error: " & errorNumber) message errorText
return
end try
end perform mail action with messages
end using terms from
on LogText(theText)
tell application "Finder"
set logPath to (path to the desktop as text) & "log.txt"
try
set writeText to open for access file logPath with write permission
set currentDateAsString to do shell script "date +\"%Y-%m-%d %T\""
write (currentDateAsString & " " & (theText as text) & return) to writeText starting at ((get eof writeText) + 1)
close access writeText
on error errorText number errorNumber
close access writeText
error errorText number errorNumber
end try
end tell
end LogText
I'm also running OS X 10.8.2 and Mail 6.2. As I said, I'm surprised to say this, but triggering the above AppleScript via mail rule works just as well for incoming messages as it does when selecting the menu "Apply Rules".

Resources