Hej Steen!

(Back on T guys. Boring, I know.)
I've answered this question a couple times before;
here on this forum and
here on Stack Overflow.
TL;DR: scripts can be stored in the project, in each user's installation (don't -- maintenance nightmare), or in MDG Technologies, which is the fancy name for EA's extension facility. For any type of non-hobbyist installation, this is the way to go.
When it comes to having scripts respond specifically to one type of element, yes, you can do that. Such scripts aren't stored in the element itself, but in special script groups called Diagram Scripts or Project Browser Scripts, depending on where you want to invoke them from. (What Nabil is talking about is an adaptation they've done locally, not part of EA's built-in functionality.) Scripts in these groups are automatically made available to users in the context menu (right-click -- Scripts).
The thing with these diagram / project browser scripts is that they cannot be distributed in an MDG Technology: they have to be stored in each project where they're needed. This is a known limitation.
However, a diagram/browser script can call a script that's located in an MDG Technology. So what I always do in these situations is write a very basic diagram/browser script which just checks what type of element (or connector) it's being invoked for, and then pass control to the "real" script which is in an MDG Technology. A typical use case for this setup is when doing generation of complex documents. (Geert prefers a different approach here, I know.)
In your case, what you want is a diagram script which calculates the tagged value based on the diagram it's invoked from and stores the result in the selected element. I'd put all the calculation stuff in an MDG Technology and have the diagram script just check the basics, like that the element is an artifact and that the diagram is the proper type. Of course, if you're just testing stuff you don't need to bother with an MDG Technology, but if you're working on something that needs to be maintainable and/or have some level of protection from accidental changes there's no other option.
But you also said "started when needed." If by this you mean "run manually when the user decides to", what I've outlined above works. If on the other hand you mean "run automatically when something happens" you need an Add-In, which is a piece of software you must write and distribute to all clients. An Add-In can react to events (like elements being created, deleted, or added to a diagram), which scripts can't.
If all of this is a little overwhelming, feel free to PM me. I'm an EA consultant located in Stockholm, and I have extensive experience of large-scale EA deployments.
HTH,
/Uffe