* Writing my First Plugin and need a little help

For users to report plugin bugs and request plugin enhancements; and for authors to test new/new versions of plugins, and to discuss plugin development (in the Programming Technicalities sub-forum). If you want advice on choosing or using a plugin, please ask in General Usage or an appropriate sub-forum.
Post Reply
avatar
mitcheip
Diamond
Posts: 66
Joined: 21 Aug 2014 08:37
Family Historian: V7

Writing my First Plugin and need a little help

Post by mitcheip » 07 Dec 2014 17:58

Hi,
I am trying to write a Plugin that runs through the Multimedia Objects, checks if there is a Note in the "unlabelled Note" field and if there is create a "Use Note as Caption" Field and populate it with a "Y".

I have added a counter to see how many Multimedia objects have notes and this gives a result that I was expecting. So that part of it is working.

When I look at the values in the stack the pointer "ptrCapt" is always Null. The documentation states that if fhCreateItem fails for any reason it returns a Null value but I can't find anywhere that tells me the reason for the failure. Any ideas?

local count = 0
local ptrNote = fhNewItemPtr() -- Link/Note pointer
local ptrObje = fhNewItemPtr() -- Multimedia pointer
local ptrCapt = fhNewItemPtr() -- Caption pointer
ptrObje:MoveToFirstRecord("OBJE")
while ptrObje:IsNotNull() do -- Loop through each Multimedia record
ptrNote:MoveTo(ptrObje,"~.NOTE2")
while ptrNote:IsNotNull() do -- Loop through each Link/Note field
ptrCapt:MoveTo(ptrObje,"~._CAPT")
if ptrCapt:IsNull() then
ptrCapt = fhCreateItem("_CAPT", ptrNote) -- create a _CAPT Field with this record and return ptr to it
fhSetValueAsText(ptrCapt, 'Y') -- populate use note as caption field with Y
count = count + 1
end
ptrNote:MoveNext()
end
print (count)
ptrObje:MoveNext()
end

User avatar
tatewise
Megastar
Posts: 27087
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: Writing my First Plugin and need a little help

Post by tatewise » 07 Dec 2014 18:13

That script is almost there, and just needs two refinements.

For each Link/Note before working on ptrCapt you must test if there is any Caption text, otherwise the ptrCapt option will get set 'Yes' on every Link/Note:
if fhGetValueAsText(ptrNote) ~= "" then

To set the ptrCapt option use "Yes" instead of "Y":
fhSetValueAsText(ptrCapt,"Yes")
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry

avatar
mitcheip
Diamond
Posts: 66
Joined: 21 Aug 2014 08:37
Family Historian: V7

Re: Writing my First Plugin and need a little help

Post by mitcheip » 08 Dec 2014 10:25

@Mike,

Thanks for the tips but its still not working.

I feel that the problems lies with the "ptrCapt = fhCreateItem("_CAPT", ptrNote)" statement.

After the execution of the statement the value of ptrCapt is Null indicating that the "fhCreateItem("_CAPT", ptrNote)" errored.

I can't, for the life of me, see what is wrong with the statement.

Regards

Ian

User avatar
tatewise
Megastar
Posts: 27087
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: Writing my First Plugin and need a little help

Post by tatewise » 08 Dec 2014 11:01

Ian, after a little more checking I see the problem.
It must be that in your experimentations you have created "_CAPT" fields with no values.
If you check on the Multimedia tab of the Records Window and expand any failing Link/Note you should see a Use Note as Caption field without any value.

In fact that is what your original Plugin script does. It adds an empty "_CAPT" field to every Link/Note.
When experimenting with Plugins it is important to use Edit > Undo Plugin Updates after each run that does not work.
Otherwise undesired changes will persist and disrupt future experiments.

So the fix is to use:
ptrCapt = fhCreateItem("_CAPT",ptrNote,true)
where the third parameter copes with re-using blank fields.

Don't worry about all the blank "_CAPT" fields, as FH will remove them on the next Save and re-Open cycle.

I have just spotted another minor error in your script, where the statement:
ptrCapt:MoveTo(ptrObje,"~._CAPT")
should be:
ptrCapt:MoveTo(ptrNote,"~._CAPT")
and then the:
if ptrCapt:IsNull() then
does not do what you expect.
So, it is much easier to simply use:
if fhGetValueAsText(ptrNote) ~= "" then
ptrCapt = fhCreateItem("_CAPT",ptrNote,true) -- create a _CAPT Field for this Link/Note and return ptr to it
fhSetValueAsText(ptrCapt,"Yes") -- populate use note as caption field with Yes
end
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry

avatar
mitcheip
Diamond
Posts: 66
Joined: 21 Aug 2014 08:37
Family Historian: V7

Re: Writing my First Plugin and need a little help

Post by mitcheip » 09 Dec 2014 17:55

@Mike,

Thanks, that's fixed it.

I could see that I had blank _CAPT fields on the records with Notes, presumably caused by the original runs that tried to populate the field with "Y" instead of "YES".

Great to see it working and thanks for the tips.

Now to have a look at V6

Ian

Post Reply