Scripts: Set tags and convert to hex set to metadata via Exiftool. Add/Set tags by reading the metatag via Exiftool.

Discuss and share scripts and script files...
Post Reply
Norn
Posts: 417
Joined: 24 Oct 2021 16:10

Scripts: Set tags and convert to hex set to metadata via Exiftool. Add/Set tags by reading the metatag via Exiftool.

Post by Norn »

Set tags and convert to hex set to metadata via Exiftool: https://exiftool.org/
Rename to exiftool.exe and change the path of exiftool in the script.

Code: Select all

//Function : Set tags and convert to hex set to metadata via Exiftool (https://exiftool.org/) (Rename to exiftool.exe and change the path of exiftool in the script)
//Created  : 2022-07-18
//Modified : 2022-07-21
//Version  : 1.1



                                   $count = gettoken(get("selecteditemspathnames"), "count", "<crlf>");                                             //Count selected items
								   if($count == "0") {status "Nothing selected in the file list", "FF0000", "alert"; end '1==1'}                    //Abort, if no item(s) is selected
//Get tags
                                   $taglist = formatlist(taglist(), "t", ", ");        //Get taglist
								   $curitemTag = tagitems("tags", , "<curitem>", 1);   //The tag(s) of the current item
								   $selItemsTag = tagitems("tags", , , 1);             //Tag(s) of selected items
								   $selItemsTag = formatlist($selItemsTag, "sd");      //Remove duplicate tags of $selItemsTag
								   
//$taglist - $curitemTag = $newtaglist
                                   $newtaglist = "";
                                   foreach($taglist1, $taglist, ", ", "e") {
								      $case = "";
								      foreach($curitemTag1, $curitemTag, ", ", "e") {
									     if($taglist1 == $curitemTag1) {$case = "1";}
										 }
									  if($case == "") {$newtaglist .= "$taglist1" . ", ";}
									  }
									
								   $newtaglist2 = substr($newtaglist, 0, -2);
								   
//If $sleItemsTag contains empty tag(s) / Untagged item(s), set $case2 = 1, then break
                                   foreach($selItemsTag1, $selItemsTag, "|") {
								      if($selItemsTag1 == "") {$case2 = "1"; break;}
								      }
									  
								   switch($case2) {                                    //Switch $case2
								      case "1":
									    $taglist2 = $taglist;                          //Show only list of tags
										break;
									  default:
									    //Set $newCuritemTag separated by ", +"
								        $newCuritemTag = "";
								        foreach($curitemTag11, $curitemTag, ", ") {
								           $newCuritemTag .= "+" . "$curitemTag11" . ", ";
									       }
								      $taglist2 = "$newCuritemTag" . "$newtaglist2";   //Combination $newCuritemTag & $newtaglist2
								      }
									  
//Pops up a window to set the tags
                                   $data = inputselect("Set Tags/Metatags (Note: writing metadata will change file size/hash)<crlf>Tick the tags that shall be assigned to the selected items.", "$taglist2",", " ,3:=2+64+4096, "cancel", 500, 665, "XYplorer", , ":tagsset");
								   if($data == "cancel") {end '1==1';}
								   tag($data, , 1, 1);                                                                                              //Set new tag(s) to selected items
								   
								   $newData = "";
								   foreach($tag1, $data, ", ") {
                                      $hexdump = hexdump($tag1, 1, r);                                                                              //Text to hex
								      $hexdump = replace($hexdump, " ", "");                                                                        //Replace space
									  $newData .= "$hexdump" . ";";                                                                                 //Stack hex list
									  }
									  
								   $taggedItems = "";
                                   foreach($item1, "<get selecteditemspathnames>", "<crlf>") {
								      $result = runret("""D:\Tools\Exiftool\exiftool.exe"" -charset filename= *-wm* -*TAG*=$newData ""$item1""");             //Write metadata to each of the selected files via Exiftool
									  if(regexmatches($result, "1 image files updated")) {$taggedItems .= "$item1" . "<crlf>";}
									  }
									  
//Status
								   //Status
								   switch($count) {
									    case "1":
										  if($data == "") {status "1 item untagged (Tags) | settings saved: Tags"; end '1==1';}
										  status "1 item tagged (Tags: $data) | settings saved: Tags";
										  break;
										default:
										  if($data == "") {status "$count items untagged (Tags) | settings saved: Tags"; end '1==1';}
										  status "$count items tagged (Tags: $data) | settings saved: Tags";
										}
										
								   if($taggedItems != "") {echo "Items that have been metatagged:<crlf><crlf>$taggedItems";}
									  
									  
									  
//Show the metatag of the selected items
/*								   
								   foreach($item2, "<get selecteditemspathnames>", "<crlf>") {
								      if(exists($item2) != "2") {$metatag =  runret("""D:\Tools\Exiftool\exiftool.exe"" -charset filename= -*TAGSLIST* ""$item2""");}           //If $item2 is not a folder, read the metadata of each file via Exiftool
								      if(regexmatches($metatag, ";")) {
									    $metatag = gettoken($metatag, 2, ": ");
									    $metatag = gettoken($metatag, 1, "<crlf>");
								        $taglist3 = "";
								        foreach($metatag1, $metatag, ";", "e") {
                                           $text2 = hexdump($metatag1, 1, "ri");                                                                      //Hex to text
								           $taglist3 .= "$text2" . ", ";                                                                              //Stack tags list
										   $taglist33 = substr($taglist3, 0, -2);
									       }
										   text "$item2<crlf><crlf>$taglist33", 1200;                                                                 //Show item and tags
									    }
									  $metatag = "";
									  }
*/
Last edited by Norn on 28 Jul 2022 05:15, edited 5 times in total.
Win10, Win11 @100% 2560x1440 22H2

Norn
Posts: 417
Joined: 24 Oct 2021 16:10

Re: Set tags and convert to hex set to metadata via Exiftool.

Post by Norn »

Add/Set tags by reading the metatag of the list files via Exiftool: https://exiftool.org/
Rename to exiftool.exe and change the path of exiftool in the script.

Code: Select all

//Function : Add/Set tags by reading the metatag of the list files via Exiftool (https://exiftool.org/) (Rename to exiftool.exe and change the path of exiftool in the script)
//Created  : 2022-07-19
//Modified : 2022-07-19
//Version  : 1.0


 $tag = "";
 $metatag = "";
 $taggeditems = "";
 $count = "";
 
//Get current list
 $list = report("{fullname}<crlf>",0);
 foreach($item, $list, "<crlf>") {
	if(exists($item) != "2") {$metatag =  runret("""D:\Tools\Exiftool\exiftool.exe"" -charset filename= -*TAGSLIST* ""$item""");}   //If not a folder, read the metatag of each item via Exiftool
	if(regexmatches($metatag, ";")) {
	  $metatag = gettoken($metatag, 2, ": ");
	  $metatag = gettoken($metatag, 1, "<crlf>");
	
	  $taglist = "";
	  foreach($metatag1, $metatag, ";", "e") {
         $text = hexdump($metatag1, 1, "ri");                                                            //Hex to Text
	     $taglist .= "$text" . ", ";                                                                     //Stack tags
		 }
	  $newtaglist = substr($taglist, 0, -2);
      $tag = tagitems("tags", , "$item", 1);                                                             //Get the current item's tag

	  if($tag == "") {tag($newtaglist, $item, 1, 1); $taggeditems .= "$item" . "|||" . "$newtaglist" . "<crlf>";}                                     //If $tag is empty, set tags $newtaglist to current item, stack the list of tagged items
	  if($tag != "" && $newtaglist != $tag) {tag($newtaglist, $item, 1, 0); $taggeditems .= "$item" . "|||" . "$newtaglist" . "<crlf>"; $tag = "";}   //If $tag is not empty, and $newtaglist != $tag, add tags $newtaglist(Not set) to $item,stack the list of tagged items
	  $newtaglist = "";
	  }
	$metatag = "";
	}
 
 //Show result
 $count = gettoken($taggeditems, "count", "<crlf>");
 $count = $count-1;
 if($count != "" && $count != "-1") {text "$count items that have added/set metadata tags:<crlf>$taggeditems", 1200;}
 if($count == "-1") {msg "No files contain new metadata tags!";}
Last edited by Norn on 20 Jul 2022 21:40, edited 1 time in total.
Win10, Win11 @100% 2560x1440 22H2

Post Reply