Grabbing a bunch of tags


(Peter Stouffer) #1

I want to extract a bunch of tags from the text in the form of

#tag1 #tag2
some text here
#tag3 more text etc.

and then put the tags in the $Tags attribute. I can’t seem to find a way to do it. Ideally I could create a set based on a grep match “#\w+” in the rule but I can’t figure it out


Familiar theme, new Q: state of play for mobile note-taking
(Mark Anderson) #2

I’m not surprised, as your example doesn’t have a unique marker consistent for all tags. Only ‘tag2’ has a preceding hash. How do you know the line starting ‘tag3’ is a tag and not text. You need some sort of definable repeating pattern.

If you control the source I’d tweak the output to ensure all tags have a # and a space after (assumption: tags are single word, i.e. no spaces in them).


(Peter Stouffer) #3

Sorry. All three tags have hash marks but got interpreted as markdown and made the line bold.

I understand that they alll need a common marker :blush: And will always be a single word. I still don’t know how to pull them from the text.

[Edited the original text to fix the problem]


(Desalegn) #5

check this hazel rule if it helps. The rule also assumes that each of the tags are marked by hash(#tag).


(Desalegn) #7

The inconsistency of tags across applications has been a pain. TB’s internal tags are not exported to Finder Tags. It is a major problem for me as it creates gaps across my system. Files I tagged inside TB are untagged when exported.
I am now thinking, if putting tags inside the $Text (#Tag) might be a solution for universal use of tags. Once the files are exported, the tags kept inside the $Text will be exported as well. The hazel or other scrip then can transfrom them to Finder tags.

Still, the proble with this in text tagging is the agents won’t be able to use it. is there any way of doing that? (@pstouffer mentioned grep/regex: but, I don’t know how regex can be used inside TB ).


(Mark Anderson) #8

@Desalegn, Tinderbox’s tags are stored in the attribute $Tags, whereas $Text is stored in $Text. If you want to export $Tags, add this code to your export template:

^value($Tags.format(", " ))^

If you want this in text export (as opposed to HTML export) at the same code as above but directly into the $text of notes you will export.

If you need Text export to have a option to add $Tags, I suggest you make a feature request.

Tags/keywords are a generic concept and not a universally defined thing so it should not be surprising that ‘tags’ means something - or nothing - in different apps. Whilst I understand you might wish to be able to export tags to Finder metadata, if this were possible many would need to turn it off rather than create a mess in Finder. Thus again, this would need to be an option, though at this point a feature request you may want to make.

If you want to pass $Tags data to finder, I suspect you could use runCommand() to call osascript and then hook an AppleScript to set the data.


(Desalegn) #9

Thank you for the suggestion. Yes, I am talking about the text export as I am generally exporting to Latex. I gave up with the html template export method because the html templates are not sufficient to contain all the attributes I want to export to the Latex document. I am relying on the rtf export -->convert the rtf to Latex.

Thank you for the support, as always.


(Mark Anderson) #10

HTML export actually gives you more control. Just insert the export codes for the attributes you require. There is no limit as far as I know.

Aside, you can also configure HTML export to export (some) LaTeX commands: see here.


(Peter Stouffer) #11

Thanks everyone. Got it working with your suggestions.

Tags are marked in the text with a # and are gathered with a ruby script hashtag.rb

$stdin.read.scan(/#\w+/) {|w| print “#{w};”}

and a rule inside the note

$Tags=runCommand(“ruby hashtag.rb”,$Text); $Tags=$Tags.replace("#","");

Tags in the text will now automatically populate the $Tags attribute.


(Mark Anderson) #13

I concur. I think this is a good task for a stamp. That is run once, on all selected notes, when you press the ‘Apply’ button on the Inspector (or click the stamp menu item).


(Sumner Gerard) #14

Here’s a solution using grep.

It doesn’t require an external script file with chmod and all that. Just put this code in a stamp, select the notes, and Apply the stamp to them.

$Tags=runCommand("grep -o '#[a-zA-Z0-9_]\+'",$Text).replace('\r',';')


(Paul Walters) #15

Better solution than mine. I’ve deleted my other posts.

Still need to replace the hash, though. See the OP.


(Sumner Gerard) #16

Ah, yes. Appending another .replace does the trick.

$Tags=runCommand("grep -o '#[a-zA-Z0-9_]\+'",$Text).replace('\r',';').replace('#','')


(Rene Trappel) #17

This is awesome! There is so much to learn just from this one line of code… Thanks a lot!


(Peter Stouffer) #18

This is really cool. Thanks Sumner Gerard. This allows a lot of flexibility to search for anything in the text and act on it (without needing explicit files for each type of search).


(Peter Stouffer) #19

So now I’m having a separate issue. Not sure if I should post it here or start a new thread, but here it goes:

I’m grabbing a set of people mentioned in the notes and eventually creating links to notes of those people. This chunk of code

$Mentions=runCommand(“grep -o ‘@[a-zA-Z0-9_]+’”,$Text).replace(’\r’,’;’).replace(’@’,’’);
$MySet="";
$Mentions.each(m){
$MyString =$FullName(find($FullName.replace(" “,”").substr(0,m.size).lowercase == m.lowercase));
$MySet=$MySet+$MyString;
};

grabs all the people tagged with a @ and puts them in a set attribute I created called $Mentions. Since the mention may only be a partial name (i.e. pete instead of Peter) I am trying to truncate the search string to match the length of the mention. I also convert both to lowercase so that cases don’t have to match and remove the space between the first and last name so that a search for “peters” will match “Peter Stouffer”.

But this doesn’t work! It returns an empty set.

However if I change the line to

$MyString =$FullName(find($FullName.replace(" “,”").Preformatted textsubstr(0,4).lowercase == m.lowercase));

and search for “pete” it works. It seems that the size function is not working in this case. It also doesn’t work if I assign

$MyNumber = m.size;
$MyString =$FullName(find($FullName.replace(" “,”").substr(0,$MyNumber).lowercase == m.lowercase));

Only a discreet number works in this case. All other attempts to use .size seem to be working properly. Any thoughts?


(Mark Anderson) #20

In the last case $MyNumber fails as you’re using the wrong context (I think), i.e. the $MyNumber for each item being tested and not the item calling the find(). I think for the latter you should use $MyNumber(that). Anyway, just to clarify the process and capturing values like m.size in attributes I tried using collect():

$MySet2="";
$MyString='';
$MySet.each(m){
   $MyString2=m.lowercase;
   $MyNumber=$MyString2.size;
   $MyString =collect(find(($FullName.replace(" ","").substr(0,$MyNumber(that)).lowercase)== $MyString2(that)),$FullName);
   $MySet2=$MySet2+$MyString;
   $Text = $Text + $MyString2 + "\n";
   $Text = $Text + " " + $MyNumber + " || " + $MyString +"\n";
   $MyString2=;
   $MyNumber=;
};

The upshot is the indication is that the section $FullName.replace(" ","").substr(0,$MyNumber(that)).lowercase is not getting evaluated as suggested.

There’s quite a nested level of operation here and I’ve no idea if the operators involved. I’d ask Eastgate - noting that MB is en route home from HT’17 so may not be around for some hours yet.


(Marianne) #21

This looks like a really nice way to speed-scan and tag text. Thank you!


(jmm) #22

If this feature request been made, I second it.

I have started using @pat’s great Publish from Tinderbox to DEVONthink My only problem with it is that my tagging system gets broken due to Tinderbox not exporting #Tags to Finder tags.
I read and highlight in MarginNote. This app exports quotations with #tags in their text body to rtf files with Finder tags. When those files are grabbed from DEVONthink into Tinderbox the #Tags attribute is automatically filled. I think it would make sense to equally convert #Tags in the opposite direction.

I agree that Applescript or probably xattr can be a solution. I understand it is not discussed in the posts below how to implement it. Has it been implemented by any other user who can advise on how to do it?


(Mark Anderson) #23

Google? I found this: https://discussions.apple.com/thread/6270801