Custom metadata: batch edit and applescript question

1.

When editing custom metadata (md for short henceforth) I noticed that you can do it with multiple files as long as they have exactly the same values. For exemple, if I select files 1, 2 and 3 I can edit their md fields (a, b and c) all at once. BUT if I add file 4 (thus, having selected files 1, 2, 3 and 4) and edit a single md field (let us say field d or any other), then I files 1, 2 and 3 will loose all their remaining md values. Can this be corrected so that 1, 2 and 3 will preserve the values that weren’t altered?

2.

I have a question about adding md when creating new files. Let us say I have the following command in my script, for creating a new file:

set theAnnotationDocument to create record with {URL:sourceURL, name:annotationName, source:o_theAnnotation, type:markdown} in current group

Where exactly do I add md with the command below?

add custom meta data the_authors for "authors" to theAnnotationDocument

(Also how do I get as code highlight here on the forum?)

  1. I have confirmed this issue and it definitely appears to be a bug. Note the Date is 2002 for all selected files, but only the first two have the Company in common. The third does does not. Selecting the first two files, the Date shows as 2002, but selecting the third file - the one without the common Company attribute - the Date is shown as 2000. Upon adding the common Company, the Date has changed.

  1. You would add the second line after the first or you’d be referring to a variable - theAnnotationDocument that doesn’t yet exist.

Code is created by using single back-ticks - `echo testing` = echo testing
Code blocks start and end with three back-ticks.

```
tell application id “DNtp”
beep
end tell
```
resolves to…

tell application id "DNtp" 
beep
end tell

And here’s an example pertaining to your code…

tell application id "DNtp"
	set newRecord to create record with {name:"New Markdown", content:"## New H2", type:markdown} in current group
	add custom meta data true for "Done" to newRecord
end tell

Got it! Thanks, Jim @BLUEFROG!
I was using a single pair back ticks.

Regarding the script, I realised my problem is slightly different, that is, do I need a conditional statement in case a md field is empty?

set customMD to custom meta data of sourceDocument
set the_editor to mdeditor of customMD

but if mdeditor field is empty I get an error msg: cannot get mdeditor of...

but if mdeditor field is empty I get an error msg: cannot get mdeditor of...

Correct because you’re addressing a value not found in the record of meta data.

@cgrunenberg may have other thoughts on this but you could put this is a try block and make the modification on error.

set customMD to custom meta data of sourceDocument
try
set the_editor to mdeditor of customMD
on error
do something 
end try

Is there a way to tell the script to, on error, set any variables not found to “” or do I have to do it individually for each variable?

There are many…

		set customMD to custom meta data of sourceDocument
		set the_authors to mdauthors of customMD
		set the_editor to mdeditor of customMD
		set the_translator to mdtranslator of customMD
		set the_title to mdtitle of customMD
		set the_publisher to mdpublisher of customMD
		set the_date to mddate of customMD
		set the_link2 to mdlink2 of customMD
		set the_page to mdpage of customMD
		set the_abstract to mdabstract of customMD
		set the_bibkey to mdbibkey of customMD
		set the_doi to mddoi of customMD
		set the_sertitle to mdsertitle of customMD
		set the_journal to mdjournal of customMD
		set the_issue to mdissue of customMD
		set the_reference to mdreference of customMD

@cgrunenberg would have to respond on this. It would need to allow for enumeration over the record returned by custom meta data of ….

Meanwhile, I’ll be going down the long and windy road :smile:

try
				set the_authors to mdauthors of customMD
			on error
				set the_author to ""
			end try
			try
				set the_editor to mdeditor of customMD
			on error
				set the_editor to ""
			end try
			try
				set the_translator to mdtranslator of customMD
			on error
				set the_translator to ""
			end try
			try
				set the_title to mdtitle of customMD
			on error
				set the_title to ""
			end try
			try
				set the_publisher to mdpublisher of customMD
			on error
				set the_publisher to ""
			end try
			try
				set the_date to mddate of customMD
			on error
				set the_date to ""
			end try
			try
				set the_link2 to mdlink2 of customMD
			on error
				set the_link2 to ""
			end try
			try
				set the_page to mdpage of customMD
			on error
				set the_page to ""
			end try
			try
				set the_abstract to mdabstract of customMD
			on error
				set the_abstract to ""
			end try
			try
				set the_bibkey to mdbibkey of customMD
			on error
				set the_bibkey to ""
			end try
			try
				set the_doi to mddoi of customMD
			on error
				set the_doi to ""
			end try
			try
				set the_sertitle to mdsertitle of customMD
			on error
				set the_sertitle to ""
			end try
			try
				set the_journal to mdjournal of customMD
			on error
				set the_journal to ""
			end try
			try
				set the_issue to mdissue of customMD
			on error
				set the_issue to ""
			end try
			try
				set the_reference to mdreference of customMD
			on error
				set the_reference to ""
			end try
			try
				set the_keywords to mdkeywords of customMD
			on error
				set the_author to ""
			end try

Thanks for the help, Jim @BLUEFROG!

It’s not elegant, but it definitely will work :slight_smile: And that’s sometimes the tradeoff we have to make.

You’re welcome!

This is just AppleScript’s handling of records which is unfortunately a little bit cumbersome. Here’s the shortest version I can currently think of:

	set {the_author, the_editor, the_translator, the_title, the_publisher, the_date, the_link2, the_page, the_abstract, the_bibkey, the_doi, the_sertitle, the_journal, the_issue, the_keywords} to {"", "", "", "", "", "", "", "", "", "", "", "", "", "", ""}
	try
		set the_authors to mdauthors of customMD
	end try
	try
		set the_editor to mdeditor of customMD
	end try
	try
		set the_translator to mdtranslator of customMD
	end try
	try
		set the_title to mdtitle of customMD
	end try
	try
		set the_publisher to mdpublisher of customMD
	end try
	try
		set the_date to mddate of customMD
	end try
	try
		set the_link2 to mdlink2 of customMD
	end try
	try
		set the_page to mdpage of customMD
	end try
	try
		set the_abstract to mdabstract of customMD
	end try
	try
		set the_bibkey to mdbibkey of customMD
	end try
	try
		set the_doi to mddoi of customMD
	end try
	try
		set the_sertitle to mdsertitle of customMD
	end try
	try
		set the_journal to mdjournal of customMD
	end try
	try
		set the_issue to mdissue of customMD
	end try
	try
		set the_reference to mdreference of customMD
	end try
	try
		set the_keywords to mdkeywords of customMD
	end try
1 Like
try 
set the_editor to mdeditor of customMD 
on error 
set the_editor to "" 
end try

Hi
Just wanting to understand custom meta data better by using your code as example:
If I understand correctly: the code is to retrieve data in mdeditor if it is not empty and assign the data to variable the_editor. And “” is assigned to the_editor if not.

Now, if I want to put something into mdeditor if it is not empty, should the code be something like this?

try 
set the_editor to mdeditor of customMD 
on error 
Set pluginData to "whatever name"
add custom meta data pluginData for mdeditor
end try

Thank you in advance

Now, if I want to put something into mdeditor if it is not empty, should the code be something like this?

If the mdeditor was not empty it wouldn’t error, so the pluginData value would not get written. It would only set the_editor to the current value of mdeditor.

If you merely want to write a value to mdeditor, just use the add custom meta data command. The attribute will be created if it doesn’t exist.

My typo! I meant to put a plug-in value IF mdeditor is empty.

Hi @ngan,
I am not entirely sure I understand where you are tying to get, but yes, if you want any value written in case the variable is empty the place it after on error.

try 
set the_editor to mdeditor of customMD 
on error 
Set the_editor to "value"
end try

Does this answer the question?

Yes, that answers my question.
I’m just experimenting to get a value from a custom md field (if not empty) or writing some value to it (if empty) for one of my pet script.

Thanks

Hi, I have a related task:
I want to take a spreadsheet of metadata - for example, Author, Subject, CC for records and I want to run a script that will take the data from this sheet and sub it into the custom metadata fields “Author” “Subject” and “CC” from columns in the sheet. I have tried various language from the above, but I am struggling. I think I am missing some simple syntax. Ideally, I could take a sheet that is arranged as for example, Column 1 is the “Author” and “Subject” (c2) and “CC” (c3), and it would read the heading author, then set mdauthor to row 2, column 1, and do the same for subject and CC.
Any help on the syntax would be greatly appreciated. thank you in advance

Is there just one row of values and are you just processing the active sheet?

processing the active sheet - well, that’s the way I have it figured. it requires me to select the sheet, then run the script.

To clarify. The sheet has metadata for files in the database and so the metadata is taken from the sheet and put in the custom metadata of all of the files.