Welcome, Guest. Please Login
Tinderbox
  News:
IMPORTANT MESSAGE! This forum has now been replaced by a new forum at http://forum.eastgate.com and no further posting or member registration is allowed. The forum is still accessible via read-only access for reference purposes. If you wish to discuss content here, please use the new forum. N.B. - posting in the new forum requires a fresh registration in the new forum (sorry - member data can't be ported).
  HomeHelpSearchLogin  
 
Pages: 1
Send Topic Print
Extracting an string from $Name with regEx (Read 6254 times)
Antonio Martinez
Full Member
*
Offline



Posts: 28

Extracting an string from $Name with regEx
Oct 01st, 2012, 8:34pm
 
Hi Mark & Co.

I am currently setting up a series of agents to see my notes in different perspectives.

The Agents simply collect all notes that contain a specified tag. Rather than setting up the Query as say $Tags.contains("legal") I wanted the query to pick up required tag name from the $Name field of the Agent. That is, for an Agent that will be colllecting all notes tagged "legal" I will have a name of  say "Legal Notes (legal)"

Everything inside the brackets is the tag that I want to be picked up in the agent query. I can use the following regular expression "\((\w+)" to extract the required tag from the Name field of the agent but I cant seem to find an operator that wil let me use this to complete my $Tags.contains query.

Hope this all makes sense!
Back to top
 
 
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Extracting an string from $Name with regEx
Reply #1 - Oct 1st, 2012, 9:20pm
 
If I understand you correctly, you want to use the agent designator. If the agent name is "Project X" and we want the agent to find notes where $Tags contains "Project X", the the query would be:

$Tags.contains($Name(agent))

N.B>: for lists/sets, a .contains can only match the whole of a discrete list item, unlike String attributes where a regex can match only part of the value. So, if $Tags is a List or Set, the above for agent "Project X" would match a tags value of "Project X" but not "Project XY".

Tip: if using this method I find it better, for real-world use, to add an attribute, e.g. $AgentTagValue, and query $AgentTagValue(agent) rather than $Name(agent). That way you don't have 2 notes with the same title, which can trip you up in other contexts. Also see this note.
Back to top
 
 

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Antonio Martinez
Full Member
*
Offline



Posts: 28

Re: Extracting an string from $Name with regEx
Reply #2 - Oct 2nd, 2012, 10:04am
 
Thanks Mark.

The agent designator helps me reference the Agent Name. With that, I was then hoping to find an opeartor that I could apply to extract just a portion of the Agent Name which would then be referenced in my $Tags.contains query.


The reason I dont want to set the Agents name to the Tag being targeted is is that the values in my $Tags set are typically only 3 or 4 letters and not sometimes not very reader friendly.  

For e.g. I have a tag called "comp" for Competitors and I would rather have the Agent named Competitors rather than just comp. In this case I was going to call the Agent "Competitors (comp)" i.e. I would put the tag being targetted in brackets with the agent name.

The task was to then find a way to extract the tag from the agent name to then use in my $Tags.contains query.

As the $Name attribute is a string I then went looking for an operator that  would let me extract a portion of a string using a reqular expression and this is where I got stuck. I had a look at the substring operator but you need to specify the starting point and length and this will vary depeding on the Agent name that I use (hence something that would let me use a reg exp to pinpoint the portion of the $Name that I was targetting).

I hear you with respect to a .contains can only match the whole of a discrete list item but I was only intending to use a regexp  to pull out the required tag from the agent name, not simply to apply it directly to the .contains operator.

Based on your last tip, a simpler solution be just to reference a separate attribute i.e. $AgentTagValue in my contains query which makes sense. Could I have a separate agent which would search these agents and extracts the tag value from my Agent Name and then use this to populate the $AgentTagValue which would then make the original Agent work?

That is, my first agent would have the simple query

$Tags.contains($AgentTagValue)

And there would be a second agent that would have the following Query and Action.

(Query)
($AgentAction != "") & $Name.contains("\((\w+)"))
(Action)
$AgentTagValue = $1


Apologies if this doesn't make too much sense or I am making things way more complicated than they need to be. Im just trying to find a way where I can be a little lazy and simply type what I want in my Agent Name and have all the Agent queries be generic references so that they can be easily replicated for setting up similar agents.

Thanks again Mark.
Back to top
 
 
  IP Logged
Mark Bernstein
YaBB Administrator
*
Offline

designer of
Tinderbox

Posts: 2871
Eastgate Systems, Inc.
Re: Extracting an string from $Name with regEx
Reply #3 - Oct 2nd, 2012, 10:34am
 
Why not have an attribute -- we'll call it $AgentTag -- to hold the string you're looking for?  Then the Name can be legible, and instead of the query referring to

    $Name(agent)

it refers to

    $AgentTag(agent).

The function you want, I think, is substr().

    $Name.substr(0,3)

extracts the first three letters of the $Name, starting 0 characters from the beginning of the name.
Back to top
 
« Last Edit: Oct 2nd, 2012, 10:34am by Mark Bernstein »  
WWW   IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Extracting an string from $Name with regEx
Reply #4 - Oct 2nd, 2012, 11:19am
 
The last step seems overkill, as the value you need for the query is in $AgentTagValue - the regex isn't needed.

Also, $Name.contains("\((\w+)")) also looks broken. To find only titles with a word in parentheses and create a backreference to just the word inside the parentheses, use:

$Name.contains("\((\w+)\)")

If what you're trying to ask is how to populate $AgentTagValue is a large number existing agents, the you need to make an agent that finds all the agents you need to update (and nothing else). So, let's assume the agents to alter all live in container "Tag agents". Your query would be:

inside("Tag agents") & $Name.contains("\((\w+)\)")

The action would be:

$AgentTagValue = $1

Allow the agent to run, check the $AgentTagValue has been populated. You can now delete this last agent as it's done it's job.

For subsequent new tag agents, you can make a prototype. note to do the steps in order - or at least set $RuleDisabled before the other alterations. Make an new agent and:
  • Title it "pTagAgent"
  • Use Info view or the Inspector to set $RuleDisabled (in the General system attribute group) to true. This prevents the rule stored in the prototype being used by the prototype itself.
  • Open the agent's text window and make $AgentTagValue a key attribute.
  • Make the agent's query: $Tags.contains($AgentTagValue(agent))
  • Make the agent's $Rule: if(!$AgentTagValue & $Name.contains("\((\w+)\)")) {$AgentTagValue = $1}
  • close the prototype agent
To use the prototype:
  • Make a new agent.
  • Give it a name with the desired tag value in parentheses, e.g. "Foo (bar)", where 'bar' is the tag.
  • Set the prototype to pTagAgent.
  • Click OK to close the create agent dialog, the agent will then self-configure - in this case to search for the $Tags value 'bar'.


FWIW, above idea tested in v5.11.2.
Back to top
 
 

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Antonio Martinez
Full Member
*
Offline



Posts: 28

Re: Extracting an string from $Name with regEx
Reply #5 - Oct 2nd, 2012, 10:49pm
 
Mark A: That works perfect and is exactly what I require. The prototype approach is the way to go and I love the $RuleDisabled attribute which is something I had overlooked but will now add to my toolbox.

Mark A & Mark B: I understand that the simplest way would be to just create another attribute directly to hold the AgentTag rather than extracting from the name of the agent.

The thing with this is that I then have to fill out that attribute. Now you may be thinking that filling out another AgentTag attribute is just as much work as including the AgentTag in the name. However, the former requires a bit more work and forces me on to the trackpad which I try to avoid if I can. (Is there a way of navigating to the $KeyAttributes when you open a note and land in the text field? I havent been able to work this out? ). I also do the same thing to tag my notes. I include the tags in the name field using # and then use an agent to set the $Tag Attribute and remove the #Tags from the name field. Again, this is largely motivated by my desire to simply be able to type notes and apply attributes without having to use the trackpad/mouse.

As a side note, with Mark B's solution, I added an $OnAdd action ($Prototype = "pTagAgent") to the container of all my Tag agents. However, when I added a new note to this container it was a note rather than an agent yet its $Prototype value was "pTagAgent" which is an agent. Is there a way to set an $OnAdd action to a note so that it makes all new notes to the container an Agent rather than a note?). I ntoice that if I add an agent within the container then the $OnAdd action applies to the new agent and the prototype for the new agent is already set to "pTagAgent".

Thanks again for all your help. This is by far the best notes taking program I have ever used.
Back to top
 
« Last Edit: Oct 2nd, 2012, 10:56pm by Antonio Martinez »  
  IP Logged
David Bertenshaw
Full Member
*
Offline



Posts: 182

Re: Extracting an string from $Name with regEx
Reply #6 - Oct 3rd, 2012, 3:11am
 
Quote:
(Is there a way of navigating to the $KeyAttributes when you open a note and land in the text field? I havent been able to work this out? )


Opt-tab from the text field will take you to the first key attribute. Opt-tab takes you back to the text field. Once you're in the attributes, tab will move you down the list. For some reason shift-tab also takes you down the list, not back up it, so you have to loop through them all if you miss one. Also,  the loop misses out any boolean attributes so you have to use the mouse for these, which is odd, but still, you are able to avoid most of the unnecessary faffing around with that stupid mouse thing. (As an aside, there are a couple of such shortcut anomalies in other places. For example, you can assign a shortcut to items in the Cleanup menu – that is, the shortcut will appear in the menu – but it has no effect. There's probably a good reason for this, but it's a little odd that you can see the shortcut.)

As you might have guessed, I hate using the mouse too.... Sorry I can't answer the other questions!

David
Back to top
 
 
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Extracting an string from $Name with regEx
Reply #7 - Oct 3rd, 2012, 4:33am
 
FWIW, I'm a mouse-centric user - been doing it that way too long to change! I started out on PCs, before leaving the Dark Side, and there many app features were only accessible on the mouse right-click menu. That said, it's just habit. TB users are a broad church and those eschewing the mouse are probably the majority. Now, back on topic…

@David.
It would be helpful if you could drop an email to support about those keyboard-related anomalies. In Explorer view, Opt+Tab cycles attributes/$Text/left pane - to help the keyboard-centric!

@Antonio.
I have to laugh. I annotated the very issue you describe re $OnAdd to save someone else tripping over it then took out the description so my answer wasn't too long. Oh well! Anyway, as you've found you can't turn a note into an agent by giving it a prototype based on an agent. I'd agree that in the context described it does seem logical that it out to be possible.

I think the underlying issue here is that there is no shortcut for making an agent (on which point I fired off a feature request to Easgate after making my last reply) whilst actions and $OnAdd are a very common way to set prototypes. Still, an $OnAdd to set the prototype works as long as you first make agents and note notes. For the first, make it outside the container and drag it in. After that select an existing child agent and make a new sibling agent and the parent's on add will apply.

Though notes and agents all have, in theory all attributes the two types aren't interchangeable. You can't swap an existing note into an agent or vice versa (although - for reasons elsewhere - doing such has been a past feature request). You must deliberately make either a note, agent or adornment. Only making notes is supported by keyboard shortcuts. Meanwhile, prototypes don't care if set from a note, agent, separator note or adornment though clearly some settings don't make sense if applied to a different type of item.
Back to top
 
« Last Edit: Oct 3rd, 2012, 4:35am by Mark Anderson »  

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
David Bertenshaw
Full Member
*
Offline



Posts: 182

Re: Extracting an string from $Name with regEx
Reply #8 - Oct 3rd, 2012, 4:53am
 
@Mark: Will do... I only noticed the Cleanup anomaly last week when I didn't have internet access and it's on my todo list to report... I'd just got used to the 'doesn't tab to booleans issue', but I'll report it at the same time.
Back to top
 
 
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Extracting an string from $Name with regEx
Reply #9 - Oct 3rd, 2012, 5:06am
 
I suspect the non-selecting of Boolean key attributes is unintentional breakage from when they swapped from being a text input (true/false) to a tick-box. Clearly I'm not the only beta-tester who's mouse-centric!

As the cleanup menu is a pop-up menu from a Map view status bar I suspect it may not have been originally designed to accept keyboard inputs.

Whatever, I believe TB v5 now done and any changes will go into v6. Though Mark B's mentioned v6, we don't know what changes it brings - so no point asking - though the port to XCode/Cocoa will likely force some UI changes. It will still be useful to know about v5 UI glitches if only to ensure they're non unintentionally recreated for v6.
Back to top
 
 

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Antonio Martinez
Full Member
*
Offline



Posts: 28

Re: Extracting an string from $Name with regEx
Reply #10 - Oct 3rd, 2012, 5:24am
 
Thanks for all the help guys.

The Opt+Tab shortcut will be very handy going forward. This is great.

@Mark
That is funny indeed! Anyway, I do agree that it would be great if you could create Prototypes from Agents.

Also, while creating Agents isnt supported natively by a keyboard shortcut its easy enough for me to assign a shortcut to the menu item through the System Preferences (I actually use Keyboard Maestro for this for other reasons). I similarly apply keyboard shortcuts to newly created stamps for quick and easy access and this is great.

I too have found some weird behaviour when navigating in outline view and map view. For e.g. when I shift down to select multiple notes if I go too far I cant go back up. Also once I am finished with the selection (say afteer applying  a stamp) I seem to be stuck with the selection and cant deselect the notes without using the mouse to click another note. Anyway, im jotting these down and will email through as you've suggested Mark.

Thanks again guys.
Back to top
 
 
  IP Logged
David Bertenshaw
Full Member
*
Offline



Posts: 182

Re: Extracting an string from $Name with regEx
Reply #11 - Oct 3rd, 2012, 9:07am
 
Quote:
Only making notes is supported by keyboard shortcuts.


@Mark: ctl-cmd-minus was added as a shortcut to create an adornment in map mode a bit ago -- if you're in Outline / Explorer mode it creates a separator instead. Can't remember which release, but it wasn't too long ago.

@Antonio, the 'going back up doesn't reduce the selection' is an echo of the (IMO) counter-intuitive standard Apple behaviour in Finder, when it will actually extend the selection the other way... I've never understood why Apple do it this  way: I'm sure there's a reason, but it's annoying... I wholly agree about the wish for a way to unselect a selection from the keyboard!

David
Back to top
 
 
  IP Logged
Antonio Martinez
Full Member
*
Offline



Posts: 28

Re: Extracting an string from $Name with regEx
Reply #12 - Oct 3rd, 2012, 9:57am
 
Its funny you mentioned that David because that behaviour definitely seemed familiar but im only a recent convert to mac (will never go back) and I just checked my finder and it seems to work the same as PC now......perhaps this was a change in Mountain Lion?? Could also be an override from an app which I installed (TotalFinder) which helps add some additional features for finder but I doubt it.
Back to top
 
 
  IP Logged
David Bertenshaw
Full Member
*
Offline



Posts: 182

Re: Extracting an string from $Name with regEx
Reply #13 - Oct 3rd, 2012, 10:02am
 
Antonio -- no, you're right! They've finally fixed the old bizarre behaviour (I presume in ML) and I hadn't noticed... Much better.

Thanks

David
Back to top
 
 
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Extracting an string from $Name with regEx
Reply #14 - Oct 3rd, 2012, 11:47am
 
@David. True about Ctrl+Cmd+minus is in aTbRef. The shortcut is only for Map (Create Adornment) or Outline (Create Separator). My memory's at fault, I think these got added alongside the menu changes for v5.0.0. That version's features is the baseline for the current aTbRef, changes since then should have the initial version number.
Back to top
 
 

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Pages: 1
Send Topic Print