The Omni Group Forums

The Omni Group Forums (http://forums.omnigroup.com/index.php)
-   OmniFocus Extras (http://forums.omnigroup.com/forumdisplay.php?f=44)
-   -   Add Text to the Start of an Action (http://forums.omnigroup.com/showthread.php?t=22128)

skillet 2011-09-12 12:41 PM

Add Text to the Start of an Action
 
Is there a way to add text to the start of an OmniFocus action? I have various notes that I put at the start of actions like "Couldn't Do: Get Milk", "Delegated:Get Milk", "Forward:Get Milk", "Dropped:Get Milk" etc. It would be great if I could run a script on several actions to do this at once.

I've spent the last half hour trying to find a way to do this on the forums but no luck.

whpalmer4 2011-09-12 01:00 PM

1 Attachment(s)
Sure, with help from Applescript!

See [URL="http://forums.omnigroup.com/showthread.php?t=7453"]FAQ: Installing & running an OF applescript[/URL] for details on how to install the attached script. Once you've got it installed, just select a bunch of actions, invoke the script, give it the string to prepend, and you're off to the races!

Christian 2011-09-12 11:56 PM

Hi whpalmer4,

Thanks for posting up a solution. However, I installed the script and it appears to be corrupted. It neither works (error "null") nor can be opened in script editor which to me is a sign there is something wrong ;) Can you check if it's actually working for you (this very file)?

I'm on OF 1.9.3, running on Lion.

Thanks,
Christ

skillet 2011-09-13 06:04 AM

2 Attachment(s)
This is awesome! Thank you very much, I will try to modify it to always do the same text every time. Especially since I only have about 5 things I attach to the start of actions at the end of the day.

Christian follow the directions in the link whpalmer4 posted and it should work for you. I was thinking that I would have to open the script, and change the text that was there but the script has you type in the text you would like to add each time you run it so it is flexible for anyone that doesn't want to open a script.

It didn't open for me either just so you know.

[CODE]--Prepend text to select actions
tell application "OmniFocus"
tell front document
tell document window 1
set oTrees to selected trees of content
set IngTrees to count of oTrees
if (IngTrees > 0) then
set strPrepend to text returned of (display dialog "String to prepend to selected actions" default answer "" buttons {"OK", "Cancel"} default button "OK")

repeat with iTree from 1 to IngTrees
set oTask to value of (item iTree of oTrees)
set name of oTask to strPrepend & name of oTask
end repeat
else
display alert "No content selected!"
end if
end tell
end tell
end tell[/CODE]

Thanks again, I will be using this everyday instead of my half-baked QuicKeys action I was running.

Christian 2011-09-13 06:52 AM

Figured out the first thing I wanted to to. Something else though: hoe do I have to modify the code to have it automatically capitalise the first letter of the first word entered into the dialog box?

For example, I enter "waiting for" -> scripts adds "Waiting for" to the task.

skillet 2011-09-13 08:42 AM

[QUOTE=Christian;101739]Figured out the first thing I wanted to to. Something else though: hoe do I have to modify the code to have it automatically capitalise the first letter of the first word entered into the dialog box?

For example, I enter "waiting for" -> scripts adds "Waiting for" to the task.[/QUOTE]

I would check out:

[url]http://www.ettoresoftware.com/products/typeit4me/[/url]

[url]http://startly.com/products/quickeys/mac/4/[/url]

[url]http://www.ergonis.com/products/typinator/[/url]

[url]http://www.smilesoftware.com/TextExpander/[/url]

Obviously you are trying to do this with AppleScript only but I don't know other than those apps or holding down the shift key :).

Christian 2011-09-13 08:47 AM

Haha, thanks for the links. Yeah it's a non-issue, just speeds up the workflow marginally if I don't have to remember pressing the shift key. However, it works fine this way, with the addition of brackets and a space after the added preset. Pretty useful script.

whpalmer4 2011-09-13 08:57 AM

Would you be happy if the text was just capitalized like a sentence? Only the initial alphabetic character would be capitalized...

[code]

property pCapitalizeFirstWord : true -- Make first alphabetic character uppercase, edit to false if not wanted

-- Prepend text to selected actions
tell application "OmniFocus"
tell front document
tell document window 1
set oTrees to selected trees of content
set lngTrees to count of oTrees
if (lngTrees > 0) then
set strPrepend to text returned of (display dialog ¬
"String to prepend to selected actions" default answer "" buttons {"OK", "Cancel"} default button "OK")

if (pCapitalizeFirstWord) then
set strPrepend to my Capitalize(strPrepend)
end if
repeat with iTree from 1 to lngTrees
set oTask to value of (item iTree of oTrees)
set name of oTask to strPrepend & name of oTask
end repeat
else
display alert "No content selected!"
end if
end tell
end tell
end tell

on Capitalize(txt)
return do shell script "python -c \"import sys; print unicode(sys.argv[1], 'utf8').capitalize().encode('utf8')\" " & quoted form of txt
end Capitalize
[/code]

Christian 2011-09-13 09:08 AM

Excellent. If that now had a line in it that would make the script check whether or not I capitalised a word myself when entering in the dialog and then would ignore all already capitalised words, that would be exactly what I have been looking for for months now.

Example: I enter "wait for Call" (Background: I live in Germany and as you might know we capitalise all nouns etc.) the script would automatically capitalise the first letter but not de-capitalise the already capital one. Does that make sense?

whpalmer4 2011-09-13 09:37 AM

Yet another version that offers you a menu of canned choices, with an option to enter text:

[code]
-- Prepend text to selected actions
property pOtherChoice : "Other (prompts for text)"
property lstChoices : {"[Couldn't Do] ", "[Delegated] ", "[Dropped] ", "[Forward] ", pOtherChoice}
property pNoChoice : ""
on MakeChoice()
local varChoice
tell application "System Events"
activate
set varChoice to choose from list lstChoices default items (first item of lstChoices) ¬
with prompt "Choose string to prepend" & return & return ¬
OK button name {"OK"} cancel button name {"Cancel"}
if (varChoice is not false) then
if (first item of varChoice is pOtherChoice) then
set varChoice to text returned of (display dialog ¬
"String to prepend to selected actions" default answer "" buttons {"OK", "Cancel"} default button "OK")
end if
return varChoice as text
else
return pNoChoice
end if
end tell

end MakeChoice

on run
tell application "OmniFocus"
tell front document
tell document window 1
set oTrees to selected trees of content
set lngTrees to count of oTrees
if (lngTrees > 0) then
set strPrepend to my MakeChoice()
if (strPrepend is not pNoChoice) then
repeat with iTree from 1 to lngTrees
set oTask to value of (item iTree of oTrees)
set name of oTask to strPrepend & name of oTask
end repeat
end if
else
display alert "No content selected!"
end if
end tell
end tell
end tell
end run

[/code]

whpalmer4 2011-09-13 10:02 AM

Thanks for recompiling and reposting, skillet. Maybe Script Debugger was having a bad day when it compiled for me. I agree the compiled script in my original post doesn't appear to run after downloading it, so I installed a fresh copy and verified that it works here (at least on Snow Leopard).

Christian 2011-09-13 11:04 AM

Even better! That makes the workflow way easier. Thanks a lot!

If anybody is interested, I added a little bit to one line so when you enter text manually the script puts it into brackets and adds a blank/space between the added prepend and the task title.

Example: you enter "Wait for". With the original, you would get "waitfortasktitle". With my addition you get "[Wait for] task title".

Just exchange the part

[QUOTE]set varChoice to text returned of (display dialog ¬
"String to prepend to selected actions" default answer "" buttons {"OK", "Cancel"} default button "OK")[/QUOTE]

With

[QUOTE]set varChoice to "[" & "text returned" of (display dialog ¬
"String to prepend to selected actions" default answer "" buttons {"OK", "Cancel"} default button "OK") & "]" & ” ”[/QUOTE]

whpalmer4 2011-09-13 11:37 AM

Your proposed change mangles the syntax a bit. A simpler change to apply would be to insert

[code]
set varChoice to "[" & varChoice & "] "
[/code]

as the next statement after the line you proposed changing, so it looks like

[code]

if (varChoice is not false) then
if (first item of varChoice is pOtherChoice) then
set varChoice to text returned of (display dialog ¬
"String to prepend to selected actions" default answer "" buttons {"OK", "Cancel"} default button "OK")
set varChoice to "[" & varChoice & "] "
end if
return varChoice as text
else
return pNoChoice
end if

[/code]

Christian 2011-09-13 11:45 AM

I was so proud that I figured out how to do it!! :(

Yeah yours sounds a tad easier and it's probably more robust. Thanks :)

whpalmer4 2011-09-13 12:24 PM

Hey, jumping in and tinkering with stuff like this is the best way to get started, I think. There are some aspects of how OmniFocus data is exposed in Applescript that caused me to scratch my head for a long time, and would probably have dissuaded me from attempting to create a script of any utility from scratch, but tinkering with a script that does something similar helps surmount that obstacle. If you look at the scripts posted here in the OmniFocus Extras forum, most of them are easily seen to be one of a small number of classes, with only a few huge beasts like RobTrew's [URL="http://bit.ly/OF-Find2"]Where in OF[/URL] that aren't just different uses of the same general structure. It usually helps to have an idea of how you would implement your desired script in some language, of course — not knowing any Applescript [b]and[/b] not knowing how to put a program together would be a daunting challenge.

whpalmer4 2011-09-13 12:53 PM

Christian, speaking of tinkering, I think you've now got essentially all of the pieces to put together that script that applies a "tag" to the beginning of a row if the action is assigned to a certain context :-)

skillet 2011-09-13 01:27 PM

I like the new additions to this script!


[QUOTE=whpalmer4;101758] If you look at the scripts posted here in the OmniFocus Extras forum, most of them are easily seen to be one of a small number of classes, with only a few huge beasts like RobTrew's [URL="http://bit.ly/OF-Find"]Where in OF[/URL] that aren't just different uses of the same general structure. [/QUOTE]

Thanks for the encouragement here, it helped me push through and start to make some more sense out of these scripts!

I don't know if the following script is squerly or not but it's a way to use QuicKeys, Quick Silver, LaunchBar or whatever to run these quickly without a dialog or mouse click. Obviously you would have to create individual scripts for each action.

Hopefully I can figure out a way to make it so it duplicates the action, adds the prepended text and then marks as complete for the day. This way it will at least show some history that I got an action started and is in progress.

[CODE]
--Prepend text to select actions
--http://forums.omnigroup.com/showthread.php?p=101754#post101754
property prependText : "[In Progress] "

tell application "OmniFocus"
tell front document
tell document window 1
set oTrees to selected trees of content
set IngTrees to count of oTrees
if (IngTrees > 0) then
set strPrepend to prependText

repeat with iTree from 1 to IngTrees
set oTask to value of (item iTree of oTrees)
set name of oTask to strPrepend & name of oTask
end repeat
else
display alert "No content selected!"
end if
end tell
end tell
end tell[/CODE]

Christian 2011-09-13 10:45 PM

I actually thought about that script request yesterday, whpalmer. Hwever, the issue with tools is exactly what you described yourself: if you have the parts and no idea how to assemble them then you still have to be extremely lucky ;)

I basically have no idea of coding, all I do sometimes is to take scripts that look remotely like what I want and try to figure out which parts/variables to modify to make "my" script. From what I can tell that put-into-folder-assign-tag would need the "on run" part to be replaced with some sort of trigger that activates when I put one or more actions into a certain context. Tht leaves me with one question though: how the hell do I do that? :p

Christian 2011-09-14 02:06 AM

Another question:

I tried to add color to the text. So if I select the free entry in the dialog and write "Important" it would add the [Important] tag in front and would color the text in the brackets in red.

To do this I added the line
[QUOTE]Set color of varChoice to {255,0,0}[/QUOTE]

Unfortunately, in OF I get an error telling that could not be executed. Any ideas?

skillet 2011-09-14 02:41 AM

[QUOTE=Christian;101798]Another question:

I tried to add color to the text. So if I select the free entry in the dialog and write "Important" it would add the [Important] tag in front and would color the text in the brackets in red.

To do this I added the line


Unfortunately, in OF I get an error telling that could not be executed. Any ideas?[/QUOTE]

I'm pretty sure this can't be done currently in OmniFocus because all the colors of actions and projects etc are handled globally in preferences and not individually.

whpalmer4 2011-09-14 04:05 AM

[QUOTE=Christian;101791]

I basically have no idea of coding, all I do sometimes is to take scripts that look remotely like what I want and try to figure out which parts/variables to modify to make "my" script. From what I can tell that put-into-folder-assign-tag would need the "on run" part to be replaced with some sort of trigger that activates when I put one or more actions into a certain context. Tht leaves me with one question though: how the hell do I do that? :p[/QUOTE]
You don't. As Lizard said, it will have to be a script you run after making some changes. Curt Clifton wrote a script years ago called Verify Next Action Exist that is similar in concept — run it periodically and it marks various projects that meet some criteria.

whpalmer4 2011-09-14 04:08 AM

[QUOTE=skillet;101799]I'm pretty sure this can't be done currently in OmniFocus because all the colors of actions and projects etc are handled globally in preferences and not individually.[/QUOTE]

Correct, the status-based styling OF does of actions includes color, and you aren't able to override it.

Christian 2011-09-14 05:47 AM

I see. Well at least I didn't do it wrong then ;)

To make a script that does "on run, check all items in context X for being tagged with Y. Add Y to Items not already tagged." I would need the syntax for that specific context. Does OF have some sort of library where I can look those variables up?

whpalmer4 2011-09-14 05:31 PM

As usual, the simple case is simple, and the rest, well, not as simple :-)

If you only have a flat list of contexts in your database, and you've got a task in oTask,

[code]
name of context of oTask
[/code]

would give you the name of that context. However, if you might have a nested context, like Errands : Downtown, the name of that context is only going to be Downtown, and you'll have to tiptoe through the tree structures (with apologies to Tiny Tim) to figure out the whole thing. Here's a sample piece of code that simply prints out the names of the contexts of the selected items:

[code]
tell application "OmniFocus"
local strCt
tell front document
tell document window 1
set strMessage to "Contexts of selected actions are:" & return & return
set oTrees to selected trees of content
set lngTrees to count of oTrees
if (lngTrees > 0) then
repeat with iTree from 1 to lngTrees
set oTask to value of (item iTree of oTrees)
set strMessage to strMessage & space & my ContextName(oTask) & return
end repeat
display alert strMessage
else
display alert "No content selected!"
end if
end tell
end tell
end tell

-- return full name of context, or "(None)"
-- uses ":" as hierarchy separator, as OmniFocus does not appear to expose
-- the value conveniently
on ContextName(oTask)
using terms from application "OmniFocus"
local oContext
local strContextName

set strContextName to ""
set oContext to context of oTask
if (oContext is missing value) then
return "(None)" -- easy case!
end if

-- if there is a context set, we need to construct the full name,
-- climbing up the tree to the top. We take the name of our starting
-- point, then look at the container. Each time the container is not
-- of class document, there's another layer, and we tack its name on
-- to the front of the string.
set strContextName to name of oContext
repeat while (class of (container of oContext) is not document)
set strContextName to ":" & strContextName
set oContext to container of oContext
set strContextName to name of oContext & strContextName
end repeat
return strContextName
end using terms from
end ContextName

[/code]

whpalmer4 2011-09-14 05:37 PM

I'll point out that the script that puts a string at the beginning of the row could be modified to check if the string is already there and skip that row if so. Google Applescript string manipulation and you should find some reading material that will help you figure it out; I'll help if you get stuck. With the script so modified, you could just bring up a context view with that context selected and remaining actions shown, select all, and run the script. Later on in the class you'll learn how to have your script select those rows without your assistance :-)

skillet 2011-09-15 04:58 AM

That's pretty cool and complicated, thanks for sharing your skills! I need to quit my day job so I can sit and figure this stuff out ;).

skillet 2011-09-17 05:47 PM

[QUOTE=Christian;101791]

I basically have no idea of coding, all I do sometimes is to take scripts that look remotely like what I want and try to figure out which parts/variables to modify to make "my" script. [/QUOTE]

Christian, just incase you are interested I thought I'd post [URL="http://forums.omnigroup.com/showthread.php?p=101930#post101930"]this link[/URL] it uses this script. It does a lot of other neat things in conjunction with it, thanks to whpalmer4 (and others).

See [URL="http://forums.omnigroup.com/showpost.php?p=101928&postcount=8"]post 8[/URL] in particular.


All times are GMT -8. The time now is 01:45 AM.

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