Set category with Applescript

R

Russ Harlan

Is there a way to set a category or change the category of a message,
task, or event by Applescript? I can set other properties easily, but
categories seem more problematic.
 
P

Paul Berkowitz

Is there a way to set a category or change the category of a message,
task, or event by Applescript? I can set other properties easily, but
categories seem more problematic.

Yes You need to study the dictionary. Since items can have multiple
categories, the 'category' property of a message or event or task is a
_list_ of categories. You can refer to each one by name. Even if the message
or event has only one category, it still has to be a (one-item) list:

set category of theMessage to {category "Work"}

Arguably, the developers should have implemented a coercion of a single
category to one-item list, but they didn't. You have to use the list
brackets.

--
Paul Berkowitz
MVP MacOffice
Entourage FAQ Page: <http://www.entourage.mvps.org/faq/index.html>
AppleScripts for Entourage: <http://macscripter.net/scriptbuilders/>

Please "Reply To Newsgroup" to reply to this message. Emails will be
ignored.

PLEASE always state which version of Microsoft Office you are using -
**2004**, X or 2001. It's often impossible to answer your questions
otherwise.
 
R

Russ Harlan

Yes You need to study the dictionary. Since items can have multiple
categories, the 'category' property of a message or event or task is a
_list_ of categories. You can refer to each one by name. Even if the message
or event has only one category, it still has to be a (one-item) list:

set category of theMessage to {category "Work"}

Thanks for replying Paul. I did study the dictionary (I'm trying!), but
I did a very poor job of phrasing my question. Apologies.

I am adapting the "Create Task From Message" script to let me choose a
category for the text. I am trying to do something like this (this is a
piece of the script):

-- create a new task with the information from the message
set newTask to make new task with properties {name:theName,
content:theContent, priority:thePriority}
set x to categories
repeat with an_item in x
set the end of catNames to name of an_item
end repeat
-- choose a category from the list
set catChoose to choose from list catNames with prompt "Choose
Category" OK button name "Choose" without multiple selections allowed
and empty selection allowed
if catChoose is not false then
set catChoice to item 1 of catChoose
end if
if catChoice = "@Computer" then
set category of newTask to {category "@Computer"}
end if

I've been able to make it work in the version you see here, and would
then add if/thens for the other categories. Seems kludgey. Is there a
way that I can set the category from the "catChoice" variable without
all the if/then statements?
 
P

Paul Berkowitz

Thanks for replying Paul. I did study the dictionary (I'm trying!), but
I did a very poor job of phrasing my question. Apologies.

I am adapting the "Create Task From Message" script to let me choose a
category for the text. I am trying to do something like this (this is a
piece of the script):

-- create a new task with the information from the message
set newTask to make new task with properties {name:theName,
content:theContent, priority:thePriority}
set x to categories
repeat with an_item in x
set the end of catNames to name of an_item
end repeat
-- choose a category from the list
set catChoose to choose from list catNames with prompt "Choose
Category" OK button name "Choose" without multiple selections allowed
and empty selection allowed
if catChoose is not false then
set catChoice to item 1 of catChoose
end if
if catChoice = "@Computer" then
set category of newTask to {category "@Computer"}
end if

I've been able to make it work in the version you see here, and would
then add if/thens for the other categories. Seems kludgey. Is there a
way that I can set the category from the "catChoice" variable without
all the if/then statements?

Yes. Just like that. I'm not quite clear why you didn't do just that, once
you'd already gone to the trouble to get catChoice: just use it. You don't
need that repeat loop, nor the previous one: [property] of every [element]
to get a complete list can always be used in AppleScript. (And it's not
necessary to specify the default values of the 'choose from list'
parameters. However you should allow for 'empty selection' to allow the user
to mean "None of the above", since "None" doesn't appear in the list. Or -
to make it easier for the user, include "None" as an option. You need to put
it in list brackets to concatenate it with the list of all category names.
Similarly using 'is in' (obverse of 'contains') requires the same class
('list' here) on both sides of the 'is in' operator: neater than a run-on of
several 'or' conditions.


tell application "Microsoft Entourage"
-- create a new task with the information from the message
set newTask to make new task with properties {name:theName,
content:theContent, priority:thePriority}
set catNames to {"None"} & name of every category

-- choose a category from the list
set catChoose to choose from list catNames with prompt "Choose Category"
OK button name "Choose" with empty selection allowed
if {catChoose} is not in {false, {}, {"None"}} then
set catChoice to item 1 of catChoose
set category of newTask to {category catChoice}
end if
end tell


--
Paul Berkowitz
MVP MacOffice
Entourage FAQ Page: <http://www.entourage.mvps.org/faq/index.html>
AppleScripts for Entourage: <http://macscripter.net/scriptbuilders/>

Please "Reply To Newsgroup" to reply to this message. Emails will be
ignored.

PLEASE always state which version of Microsoft Office you are using -
**2004**, X or 2001. It's often impossible to answer your questions
otherwise.
 
R

Russ Harlan

Yes. Just like that. I'm not quite clear why you didn't do just that, once
you'd already gone to the trouble to get catChoice: just use it. You don't
need that repeat loop, nor the previous one: [property] of every [element]
to get a complete list can always be used in AppleScript. (And it's not
necessary to specify the default values of the 'choose from list'
parameters. However you should allow for 'empty selection' to allow the user
to mean "None of the above", since "None" doesn't appear in the list. Or -
to make it easier for the user, include "None" as an option. You need to put
it in list brackets to concatenate it with the list of all category names.
Similarly using 'is in' (obverse of 'contains') requires the same class
('list' here) on both sides of the 'is in' operator: neater than a run-on of
several 'or' conditions.

It all seems so simple now. :)

Seriously, thanks. I see it now. The reason I ended up with the mess
of if/ends you saw before was experimentation. I didn't know about the
need for a bracketed list at first, so I tried all kinds of things.
When I finally figured out the need for the brackets I tried putting
"catChoice" in quotes, which didn't work. Then I think I tried the form
"{Category: catChoice}" which didn't work either. The if/ends worked,
but I knew there had to be a better way.

I neglected to try the simplest option -- I should know by now that is
often the correct choice with Applescript.

You've given me a huge jumpstart. I appreciate it.
 
Top