problems to make a javascript macro work

273 views
Skip to first unread message

NT

unread,
Dec 1, 2015, 8:32:32 AM12/1/15
to TiddlyWiki
Hi there, 
I wanted to make a string replacement and found no other possibility as to do it with a javascript macro.
So I found at http://tw5magick.tiddlyspot.com/ the following source code:



/*\
title: $:/macros/skeeve/mysamplemacro.js
type: application/javascript
module-type: macro

<<mysamplemacro character address>>

Example:
<<mysamplemacro>>
<<mysamplemacro "Donald Duck">>
<<mysamplemacro "Mickey Mouse" "Mouse House">>

\*/
(function(){

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

/*
Information about this macro
This is the mysamplemacro of Tiddly Wiki 5 written in JavaScript 
*/

exports.name = "mysamplemacro";

exports.params = [
{ name: "character" },
{ name: "address" }
];

/*
Run the macro
*/
exports.run = function(character, address) {
if( !character) character = "Bugs Bunny";
if( !address) address = "Rabbit Hole Hill";
var output ="Hi, I'm " + character + " and I live in " + address;
return output;
};

})();



I just copied to my wiki and named it $:/macros/skeeve/mysamplemacro.js like it was before.
Then I tried to call it within a tiddler named jsTest:


<<mysamplemacro>>
<<mysamplemacro "Donald Duck">>
<<mysamplemacro "Mickey Mouse" "Mouse House">>


But this did not work, there was no output.
So what went wrong???

I have read this: http://tiddlywiki.com/dev/index.html#JavaScript%20Macros
I wondered where to find and to set the module type.
Is this just done by putting one line in the macro source code like "module-type: macro"?
As there is the speaking of a module-type "field" I expected to find it at the bottom of a tiddler in "Edit"-Mode between the System fields like current-tiddler, library or plugin type.
Also it is not clear to me if I have to tag the js-macro with $:/tags/Macro as the normal wikitext macros.
Also the naming convention is not clear to me. I want this macro globally accessible, but not mixed up in between the system macros. 

I would be happy about suggestions to why my js macro is not working as I found the documentation about that a bit limited.
Thank you very much!

PMario

unread,
Dec 1, 2015, 8:41:36 AM12/1/15
to TiddlyWiki
Hi NT,

I think it would be easier, if you describe, what you want to do, instead let us guess.

I personally think, we can solve your problem using standard tools instead of a js macro, which contains hard coded strings.

-mario

BJ

unread,
Dec 1, 2015, 10:26:03 AM12/1/15
to TiddlyWiki
Hi NT,
you may be missing the fields

type: application/javascript
module-type: macro

you can set the type by editing the tiddler and selecting 'application/javascript' from the dropdown list,
you can add a field 'module-type' from the drop down list and then add the value 'macro'

As Mario said, if you say what you are trying to achieve then we can advise you if this is the right track,

all the best

BJ

NT

unread,
Dec 1, 2015, 11:38:07 AM12/1/15
to tiddl...@googlegroups.com
Mario, ok that's a point. So I'll split the solution I want to have in two parts. First, a direct solution, second a solution with help of a javascript macro.
First things first:
I have a "normal" macro that works. It's:

\define d1jpg(filename) 
[img[./_new/Scout/$filename$.jpg]]
\end

That macro gets a uri filename like: 20150331_195734.mp4#t=0,17
The purpose of the macro is to store a path (./_new/Scout/) and to generate a valid image name out of the incoming filename.
But because of "#t=0,17" this is not trivial any more. So what I want now is to remove ".mp4#".
Since I did not know how to do it with wikitext my idea was to use a javascript macro instead of the macro above.
What shall be returned in this example would be 20150331_195734t=0,17.jpg as an image link like above.

But yes Mario, maybe you are right and there is a more simple solution to that than a javascript macro.
Thanks for your support!

NT

unread,
Dec 1, 2015, 12:40:00 PM12/1/15
to TiddlyWiki
Hi BJ

Am Dienstag, 1. Dezember 2015 16:26:03 UTC+1 schrieb BJ:
Hi NT,
you may be missing the fields

type: application/javascript
module-type: macro

you can set the type by editing the tiddler and selecting 'application/javascript' from the dropdown list,
thanks, I did this before.

you can add a field 'module-type' from the drop down list and then add the value 'macro'
YES, that's the thing I expected I could do. But there is no 'module-type' in the drop down list!!! I am using TiddlyWiki5.1.9


As Mario said, if you say what you are trying to achieve then we can advise you if this is the right track,
Yes, please see my answer to Mario. But I am also interested to get a javascript macro working. 

all the best

BJ


Thanks, could you give further advice to the module-type?! 

Eric Shulman

unread,
Dec 1, 2015, 12:51:52 PM12/1/15
to TiddlyWiki
On Tuesday, December 1, 2015 at 9:40:00 AM UTC-8, NT wrote:
you can add a field 'module-type' from the drop down list and then add the value 'macro'
YES, that's the thing I expected I could do. But there is no 'module-type' in the drop down list!!! I am using TiddlyWiki5.1.9

"module-type" *is* in listed in the "field name" drop list... scroll down to the "system fields" section... and you will find it after "library" and before "name".

Note: if a desired field name is not in the list, you can just enter it directly in the input field.

-e

BJ

unread,
Dec 1, 2015, 12:59:21 PM12/1/15
to TiddlyWiki

you can type it in - see attached
ex.png

BJ

unread,
Dec 1, 2015, 1:21:55 PM12/1/15
to TiddlyWiki
important!
It is necessary to reload you tw after making changes to a javascript tiddler

NT

unread,
Dec 1, 2015, 1:49:33 PM12/1/15
to tiddl...@googlegroups.com


Am Dienstag, 1. Dezember 2015 18:51:52 UTC+1 schrieb Eric Shulman:
On Tuesday, December 1, 2015 at 9:40:00 AM UTC-8, NT wrote:
you can add a field 'module-type' from the drop down list and then add the value 'macro'
YES, that's the thing I expected I could do. But there is no 'module-type' in the drop down list!!! I am using TiddlyWiki5.1.9

"module-type" *is* in listed in the "field name" drop list... scroll down to the "system fields" section... and you will find it after "library" and before "name".

no, sorry it's not. at least not in my freshly downloaded empty wiki 5.1.9
between "library" and "name" there's "list" in the list.
please see my appended wiki!


Note: if a desired field name is not in the list, you can just enter it directly in the input field.

Yes, I knew and I did, but it did not made the javascript macro work.
Maybe this manual entry didn't worked out or there was another problem left.
I don't know. Feel free to check out the appended wiki and try yourself.
Thank you very much!
 

-e


UPDATE: Eric, it works now, I had a problem with with browser cache and reloading the wiki after changing the js macro went wrong. Please see my answer to BJ.
But the subject is not solved yet, now I try to adapt the working js macro for my purpose...

NT

unread,
Dec 1, 2015, 1:51:49 PM12/1/15
to TiddlyWiki
Yes BJ, I know, please see my answer to Eric.

NT

unread,
Dec 1, 2015, 1:54:32 PM12/1/15
to tiddl...@googlegroups.com


Am Dienstag, 1. Dezember 2015 19:21:55 UTC+1 schrieb BJ:
important!
It is necessary to reload you tw after making changes to a javascript tiddler

Yes I knew and I did, thanks for trying harder :-) 
But I will repeat it to be sure!

UPDATE: S**t, it worked. It was the reload. Seemed to be a Browser cache problem, because I reloaded before. Now the macro from Skeeve works!
Ok, I'll now try to adapt it for my solution...

NT

unread,
Dec 4, 2015, 5:04:30 PM12/4/15
to TiddlyWiki
I adapted the javascript for my purpose and it worked - partly.
When I call the javascript function with a string as parameter it works fine, when I call the javascript function with a wikitext variable the wikitext variable it not "solved" into it's "value".
I'll open a new issue for that.
Thank you all for your help so far!
Reply all
Reply to author
Forward
0 new messages