The Omni Group Forums

The Omni Group Forums (http://forums.omnigroup.com/index.php)
-   OmniFocus 1 for Mac (http://forums.omnigroup.com/forumdisplay.php?f=38)
-   -   AppleScript to create a Project inside a folder? (http://forums.omnigroup.com/showthread.php?t=23812)

RicardoSaporta 2012-03-27 01:40 PM

AppleScript to create a Project inside a folder?
 
Hello All,

Using an applescript, I would like to create a project inside a designated folder. but I am having difficulty with the correct way to tell Omnifocus where to create the project (it keeps placing it at the document (root) level).

I have the folderName and the folderID and am happy to use whichever (though I'd prefer the folderID).

the line for creating the project that I am using is

=========
make new project with properties {name:ProjectName, note:ProjectNote, XXX}
=========

I've tried:
container:FolderID
container:FolderName
folder:FolderID
folder:FolderName

and tried different keywords after the properties close-bracket, but to no avail. Any help would be greatly appreciated.

Thanks in advance!
Ricky

whpalmer4 2012-03-27 02:10 PM

[code]set myFolder to folder id folderID
set myProject to make new project with properties {name:ProjectName, note:ProjectNote} at end of projects of myFolder
[/code]

RicardoSaporta 2012-03-27 04:01 PM

Perfecto!!

Thank you so much. That was exactly what I was looking for

Christian 2012-03-28 11:18 AM

Is this the complete code necessary? Can I run the script with just this and it will work? Thankee.

RicardoSaporta 2012-03-28 11:42 AM

[QUOTE=Christian;108884]Is this the complete code necessary? Can I run the script with just this and it will work? Thankee.[/QUOTE]

Hi Christian,
This is what I am using. (Note that it is part of a bigger script, and so some variables are not properly declared in the example below)
-Ricky


[CODE]
(* Since this code is copied from part of a larger script,
it is missing the variable declerations.
Make sure to Set Variables as needed:

set FolderName to ...
set ProjectName to ...
etc…
*)


tell application "OmniFocus"
--activate
set theDoc to default document

tell theDoc

set StatusContext to context "ContextInHere" of context "ParentContext" --modify as needed

--Create the folder with Project and Task
set NewAcctFolder to (make new folder with properties {name:FolderName} at end of folder id RootFolderID)
set NewOppProject to (make new project with properties {name:ProjectName, note:ProjectNote, context:StatusContext} at end of projects of NewAcctFolder)
set NewStatusTask to (make new task with properties {name:TaskName, note:TaskNote, context:StatusContext} at end of tasks of NewOppProject)

end tell
end tell
[/CODE]

Christian 2012-03-28 12:05 PM

Grand, thanks for the quick reply! I'll test this tomorrow, could save some time for work projects.

RobTrew 2012-03-28 11:51 PM

FWIW it's also possible to nest the [I]make[/I] commands within the scope of the appropriate parent objects using [I]tell ... end tell[/I].

The new objects will be placed, by default, at the end of that parent's children.

[CODE]tell application id "OFOC"
tell default document
set oContext to first flattened context where name = "Library"

tell (first flattened folder where name = "Reading Cycles")
tell (make new folder with properties {name:"Shakespeare"})
tell (make new project with properties {name:"Lear", note:"Sturm und drang", context:oContext}) to ¬
make new task with properties {name:"First reading", note:"Skip the critical theory at this stage", context:oContext}
end tell
end tell
end tell
end tell[/CODE]

whpalmer4 2012-03-29 11:18 AM

[QUOTE=RobTrew;108916]The new objects will be placed, by default, at the end of that parent's children.
[/quote]
And if you should need them placed elsewhere, you can insert the necessary specification:

[code]
tell application id "OFOC"
tell default document
set oContext to first flattened context where name = "Library"

tell (first flattened folder where name = "Reading Cycles")
tell (make new folder with properties {name:"Shakespeare"})
tell (make new project with properties {name:"Lear", note:"Sturm und drang", context:oContext}) to ¬
make new task with properties {name:"First reading", note:"Skip the critical theory at this stage", context:oContext}
tell (make new project with properties {name:"Othello", note:"Jealousy drives men mad", context:oContext}) to ¬
make new task with properties {name:"First reading", note:"Joe Green thinks this might make a good opera, agree?", context:oContext}
tell (make new project with properties {name:"Julius Caesar", note:"Beware the Ides of March!", context:oContext} [b]at after first project[/b]) to ¬
make new task with properties {name:"First reading", note:"Skip the critical theory at this stage", context:oContext}

end tell
end tell
end tell
end tell
[/code]

RobTrew 2012-03-29 02:46 PM

Some good reading projects developing here :-)

RicardoSaporta 2012-04-03 06:32 AM

flattened?
 
Hi Rob / Whpalmer,

Thanks for the extra tips, much appreciated.

Out of curiosity, how does the term flattened work? what happens if it is omitted?

Thanks
Ricky

whpalmer4 2012-04-03 07:35 AM

If you don't use the flattened adjective, you only get the items at the top level. With Rob's example script, if you ran it against an OmniFocus database where you had a nested context called Errands:Library instead of Library, it would not find the Errands:Library context and thus none of the created actions would have a context assigned.

Prior to the advent of the flattened adjective, one had to write some additional code to walk through the entire structure, looking for such matches. Many scripts didn't bother...


All times are GMT -8. The time now is 10:13 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.