Outlook script not working, error: to item 1 of result

Have the following script.

tell application "Microsoft Outlook" to activate
delay 0.5
tell application "Microsoft Outlook"
	set selectedMsg to (current messages)
	set selectedMsg to item 1 of result
	set msgAttachment to attachments of selectedMsg
	set msgAttachment to result's item 1
	
	set attachmentName to name of msgAttachment
	set attachmentFile to ((path to desktop) & attachmentName) as text
	
	set submitfolder to "OS E:Users:e:Desktop:" as alias
	set collectionfolder to ":Volumes:Data E:Consulting F&M:Telephone Switch Record" as alias
	
	delay 1
	
	--> save message to disk before we move it
	save msgAttachment in file attachmentFile -- NB. 'file' specifier.
	
end tell

it used to work flawlessly under OS 10.13 and MSO 2011. Stopped working under OS10.13 and MSO 365.

The error I get is:

error "Can’t get item 1 of {}." number -1728 from item 1 of {}

I played around with it but cannot get it to work. Any tips?

Using the variable result often introduce difficult to trace errors.

I guess that what you wanted to achieve is what is coded below.

tell application "Microsoft Outlook" to activate
delay 0.5
tell application "Microsoft Outlook"
	set selectedMsg to (current messages)
	log point "1" -- ADDED
	set selectedMsg to item 1 of selectedMsg -- EDITED
	set msgAttachment to attachments of selectedMsg
	log point "2"-- ADDED
	set msgAttachment to msgAttachment's item 1 -- EDITED
	
	set attachmentName to name of msgAttachment
	set attachmentFile to ((path to desktop) & attachmentName) as text
	
	set submitfolder to "OS E:Users:e:Desktop:" as alias
	set collectionfolder to ":Volumes:Data E:Consulting F&M:Telephone Switch Record" as alias
	
	delay 1
	
	--> save message to disk before we move it
	save msgAttachment in file attachmentFile -- NB. 'file' specifier.
	
end tell

Once again, the way you describe the reported error doesn’t help, because we can’t guess which instruction really issued it.
Here two instructions were able to return it : the two ones which I edited.
The two added log instructions would help.
If only “point 1” is logged we will know that
set msgAttachment to attachments of selectedMsg failed
If “point 2” is logged before the crash, we will know that
set msgAttachment to msgAttachment’s item 1 failed

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 20 juillet 2020 11:20:24

Good morning. I thought I had been clear, obviously not, so sorry.

This script takes an enclosed document from an email and puts it on the DT and from there in a folder. next if trashes the file from the DT.

I ran your script and get following.

tell application "Microsoft Outlook"
	activate
	get current messages
		--> {}
	(*point 1*)
Result:
error "Can’t get item 1 of {}." number -1728 from item 1 of {}

It’s always what you got several times when helpers got a correct behavior.

We can’t guess what you really selected.
What is sure is that, from the application’s point of view, nothing was selected.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 20 juillet 2020 12:19:25

tell application "Microsoft Outlook"
   activate
   get current messages
end

If I read well, the code above return an empty list.
So there was no object available matching the description “current messages” when you ran your script.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 20 juillet 2020 12:29:37

Just glancing over, and noticed a malformed file path. It sounds like the script hasn’t reached this part in its execution yet, and the file path will probably self-correct. But just for thoroughness:

":Volumes:Data E:Consulting F&M:Telephone Switch Record" as alias

As far as I’m aware–and I’m not particularly an expert on HFS paths–absolute file paths don’t begin with a colon. I think there’s an AppleScript application (either Finder or SystemEvents) that will (or did) print paths to insertable USB drives as alias “:USB 1”, for instance, but I’ve been told that’s a no no.

The second characteristic of which I’m aware is that folder paths should end with a colon.

And, finally, absolutely file paths would typically begin with the name of the drive to which the file path relates. So, assuming your hard drive is called “Macintosh HD”, then one possible corrected form of the file path would be:

alias "Macintosh HD:Volumes:Data E:Consulting F&M:Telephone Switch Record:"

But it’s also evident you’re crossing over into another disk, so it can be shortened to this:

alias "Data E:Consulting F&M:Telephone Switch Record:"

Although I’ve just noticed your script doesn’t end up using it anyway, so blah.

HFS folder paths are often spelled with an ending colon but it’s not required to get a valid pathname.

If you want to check that I am right, run this short script:

set p2d to path to desktop as string
set p2d to text 1 thru -2 of p2d
log p2d
p2d as alias

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 20 juillet 2020 15:20:56

Thanks for that, I experimented with the : and no diference

as we used this script for about 10 years now I do nothing differently compared to before when it worked. the email is selected (as we always did) and it gives this error.

As long as you will not find what fails at this level trying to execute other instruction is just wasting time.

I have some difficulty to understand why the code

tell application "Microsoft Outlook" to activate
delay 0.5
tell application "Microsoft Outlook"
   set selectedMsg to (current messages)
end tell

return an empty list on your machine when it return a list of messages on other users machines.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 20 juillet 2020 16:15:48

I have no idea either Yvan.

Hi AppleScript breaks with Office 16.39. Delete all Office Apps, empty trash and install the 16.38 version.

https://officecdn-microsoft-com.akamaized.net/pr/C1297A47-86C4-4C1F-97FA-950631F94777/MacAutoupdate/Microsoft_Office_16.38.20061401_Installer.pkg

Due to sandbox restrictions I think desktop is not a valid path to store. Try the downloads folder.

Armin

Thanks for your reply.

I am still on 16.38 so that could not be it. will try the tip with not using the desktop, maybe that is it.

and do not forget my script gets stuck much earlier at: set selectedMsg to item 1 of result

so DT would not be it and after testing is not it.

In fact your script fails before.

The instruction which fails is :

set selectedMsg to (current messages)

which is supposed to return a list of messages but return an empty list.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 27 juillet 2020 18:00:36

Hi Yvan
getting error
after log point 1
with item 1 highlighted
error “Can’t get item 1 of {}.” number -1728 from item 1 of {}

I have selected the messages in outlook, run the script from scripteditors’ menu

Further testing


set selectedMsg to (current messages)
set msgAttachment to attachments of selectedMsg
--The above too works

set attachmentName to name of msgAttachment -- → error

-- → error "Can’t get name of {}." number -1728 from name of {}

I am not an expert, but I assume as many of you have already stated, it is not possible to select messages in Outlook Mac (365) in the current version due to sandbox restrictions. So every script will fail at the moment. If anybody knows a workaround or has a solution, please let us know.

This fails:

tell application "Microsoft Outlook"
	set selectedMessages to selected objects -- this fails
	return selectedMessages
end tell

This fails:

tell application "Microsoft Outlook"
	set selectedMessages to selection -- this fails
	return selectedMessages
end tell

And this fails:

tell application "Microsoft Outlook"
	set selectedMessages to current messages -- this fails
	return selectedMessages
end tell

Only thing to do is to upvote here: https://outlook.uservoice.com/forums/924856-the-new-outlook-for-mac/suggestions/39253423-support-applescript

My license for Microsoft Outlook is expired. Can someone test, what returns following code when the messages is selected manually?


tell application "Microsoft Outlook" to (get selection as record)

Thanks for the fresh idea KniazidisR!

I think selecting messages would be the base of fixing many many outlook-scripts here in the forum currently, but sadly no luck yet on my system. With your script I get this error message: “error “Can’t make missing value into type record.” number -1700 from missing value to record”. The missing value seems to be the sandbox problem.

Hi,
I did not use outlook at home, (at work use windows, and allowed to install on home (corporate licensing ) installed it to try pdf output functionality.

How very strange, it all worked when I posted on 21/05 about “path to file” in another post
but did not on 22/05 !

I did mange to a perfect pdf from email :slight_smile: for once