* Plugin to Set the Title to be the same as the File Name?

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
MFriend
Famous
Posts: 111
Joined: 30 Jan 2021 07:43
Family Historian: V7

Plugin to Set the Title to be the same as the File Name?

Post by MFriend »

Hi Folks:
In a previous database I used the "Rename Selected Media Files (No ID)" to set the file name of media to be the same as the Title of the Media Record.

Background:
With this database I have a different issue: I had an issue at some point in the past with FTM 2019 with the media being attached to the wrong person (This is before I started naming media files and I was letting the ones from Ancestry get auto-named lik: 1900 Federal Census(9) . At some point the wrong media got attached to the wrong people.

Before importing this database/gedcom into FH 7, I had redownloaded and set the file name for about 1000 or so media files. FH7 though kept the 'original' titles that were auto created by FTM. So the file name is good, but the title is 'worthless'.

Question:
Is there a plugin or way to do the exact opposite of the "Rename Selected Media Files (No ID)" so that the file name becomes the Title? That is set the Title of each media to be the same as the file name.

After this, then I could go through and just change those needing it and then run the "Rename Selected Media Files (No ID) plugin.

Here is an example of some of the current records:
Image

Matthew
User avatar
Mark1834
Megastar
Posts: 2458
Joined: 27 Oct 2017 19:33
Family Historian: V7
Location: South Cheshire, UK

Re: Plugin to Set the Title to be the same as the File Name?

Post by Mark1834 »

Yes, I’ve got a custom plugin that does exactly that - it copies the case-sensitive file name (without the full path or file extension) to the title of the media record.

My PC is packed away at the moment during home office refurbishment, but I’ll check it on the laptop and post here either later today or tomorrow.
Mark Draper
avatar
MFriend
Famous
Posts: 111
Joined: 30 Jan 2021 07:43
Family Historian: V7

Re: Plugin to Set the Title to be the same as the File Name?

Post by MFriend »

Thank you Mark, that would be so helpful :)

Matthew
User avatar
Mark1834
Megastar
Posts: 2458
Joined: 27 Oct 2017 19:33
Family Historian: V7
Location: South Cheshire, UK

Re: Plugin to Set the Title to be the same as the File Name?

Post by Mark1834 »

I have simplified the plugin, as my original carries out a number of additional checks that are very specific to my projects.

The attached plugin copies the media filename (without the full path and extension) to the Media Record title. You can select either automatic or prompted change. It is FH7 only, as this simplified extract uses fhFileUtils to extract the file name from the full path.
Mark Draper
avatar
MFriend
Famous
Posts: 111
Joined: 30 Jan 2021 07:43
Family Historian: V7

Re: Plugin to Set the Title to be the same as the File Name?

Post by MFriend »

Thank you very much Mark for your help. This plugin worked perfect and saved a lot of time :)

Matthew
avatar
LeslieP
Diamond
Posts: 78
Joined: 03 Jan 2021 16:38
Family Historian: V7

Re: Plugin to Set the Title to be the same as the File Name?

Post by LeslieP »

Dang. I was just logging in to look around and see if something like this was possible, and here it is.

You guys are so amazing.

But of course I have to ask if a small enhancement is possible...add the subdirectory to the name.
  • file: P:\Genealogy\exhibits\birth_records\fisher_john-nh-birth-record.jpg
  • desired title: birth_records\fisher_john-nh-birth-record.jpg
I've never done any lua, but seems like fhSetValueAsText(pT, File) is the magic line, is there some easy way to specify "file" should be "folder and file" ???

Appreciate any help.
Leslie P
Houston, TX
from TMG to RootsMagic to FH7
publish to web via TNG
User avatar
Mark1834
Megastar
Posts: 2458
Joined: 27 Oct 2017 19:33
Family Historian: V7
Location: South Cheshire, UK

Re: Plugin to Set the Title to be the same as the File Name?

Post by Mark1834 »

Leslie,

Doing that in one step might be tricky, but you should be able to do it in two steps.

line 34 of the plugin gives you the filename without path or extension (SplitPath.basename). If you change SplitPath.basename to SplitPath.abs_path you get the fully resolved name and path as the title, even for files in the project folder. It would also be possible to include the path but not the extension if that is preferred. Just change SplitPath.basename to SplitPath.parent .. '\\' .. SplitPath.basename

So in your case, your example title would be P:\Genealogy\exhibits\birth_records\fisher_john-nh-birth-record.jpg or P:\Genealogy\exhibits\birth_records\fisher_john-nh-birth-record.

To get to your final title, you will need to remove the unwanted leading part of the name, so a global search for P:\Genealogy\exhibits\ to replace with an empty string. Can the Search and Replace plugin do that selectively for media titles? I don't have it loaded, but it has lots of options. I'm sure Mike will be along later to clarify...

Make sure that you experiment on a copy of your project!
Mark Draper
User avatar
tatewise
Megastar
Posts: 28341
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: Plugin to Set the Title to be the same as the File Name?

Post by tatewise »

The most straightforward change is to replace line 34 File = SplitPath.basename with:
Parent = SplitPath.parent:match( "[^\\]+$" ) extract last folder name without \ from parent folders
File = Parent .. "\\" .. SplitPath.basename construct parent with \ and filename
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
User avatar
Mark1834
Megastar
Posts: 2458
Joined: 27 Oct 2017 19:33
Family Historian: V7
Location: South Cheshire, UK

Re: Plugin to Set the Title to be the same as the File Name?

Post by Mark1834 »

Mike loves patterns ;). That's a good addition, but it might need to be set out slightly differently to make it clearer.

Replace the existing line 34 with these two lines if you want one folder level without the file extension (e.g. birth_records\fisher_john-nh-birth-record)

Code: Select all

local Parent =  SplitPath.parent:match("[^\\]+$")
local File = Parent .. "\\" .. SplitPath.basename
If you want to keep the file extension in the name, replace line 34 with theses two lines

Code: Select all

local Parent =  SplitPath.parent:match("[^\\]+$")
local File = Parent .. "\\" .. SplitPath.filename
Mark Draper
avatar
LeslieP
Diamond
Posts: 78
Joined: 03 Jan 2021 16:38
Family Historian: V7

Re: Plugin to Set the Title to be the same as the File Name?

Post by LeslieP »

Must be something wrong.

If I select to run prompted, the popup shows the exact right title YAY!

I cancel out because I have 1104 media files and am not going to step through each one. I run automatic, the window tells me plugin completed, 1104 titles updated. All seems good.

But exactly zero titles are actually updated. I just tried it with the original plugin, same deal, all looks right as it's running, but nothing is being put into the media title field.

My media records have NO title right now, nothing at all. If that matters.
Leslie P
Houston, TX
from TMG to RootsMagic to FH7
publish to web via TNG
User avatar
Mark1834
Megastar
Posts: 2458
Joined: 27 Oct 2017 19:33
Family Historian: V7
Location: South Cheshire, UK

Re: Plugin to Set the Title to be the same as the File Name?

Post by Mark1834 »

Sorry about that Leslie. The missing titles is the key difference. When a Media Record is created in FH, it is given the file name as a default title. Even if you clear this field in the Property Box, it still exists as an empty title. However, media imported from RM will not contain any title field, so there is nothing to change. FH doesn't distinguish between missing and empty titles for the prompt, but doesn't set values for missing ones unless it is created first.

This update fixes that issue, creating new title fields where none exist initially.

Your line to change for your customisation is now line 38 as I have added a version history to the top of the file. For interest, the additional line to create an empty title is line 54.
Attachments
Name Media Record From File (0.1.1).fh_lua
(1.97 KiB) Downloaded 51 times
Mark Draper
avatar
LeslieP
Diamond
Posts: 78
Joined: 03 Jan 2021 16:38
Family Historian: V7

Re: Plugin to Set the Title to be the same as the File Name?

Post by LeslieP »

Worked perfectly! (on a copy of my database, I've not done it on the main yet, but will do soon)

Can't thank you enough for this. Now I can actually use the "title" field in the filter are on the media records page!
Leslie P
Houston, TX
from TMG to RootsMagic to FH7
publish to web via TNG
Post Reply