# Intro to assigning tag fields with scripting on Mac

**URL:** https://community.mp3tag.de/t/intro-to-assigning-tag-fields-with-scripting-on-mac/60909
**Category:** Mac
**Created:** [May 6, 2023, 7:04pm UTC](https://community.mp3tag.de/t/intro-to-assigning-tag-fields-with-scripting-on-mac/60909 "2023-05-06T19:04:06Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![greybeard](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@greybeard](https://community.mp3tag.de/u/greybeard)
#### Post date: [May 6, 2023, 7:04pm UTC](https://community.mp3tag.de/t/intro-to-assigning-tag-fields-with-scripting-on-mac/60909/1 "2023-05-06T19:04:06Z")

</div>

_Some things I had to figure out when trying to write my first script - hopefully helpful to someone._

**Important note:** to assign tag fields with mp3tag on a Mac using scripting, you use the  
_Import tag fields_ Action (as mentioned in numerous places in the Forum).

The _Import tag fields_ Action has two entries: the _Source format_ and the _Formatstring_.

You use scripting in the _Source format_ and specify the field(s)  
to match against the _Source format_ string in _Formatstring_.

Below are some examples

### Assign Beatles to the Artist for selected songs using a static string

Source format: `Beatles`  
Formatstring: `%artist%`

### Assign Beatles to the Artist for selected songs using a silly script

Source format: `$reverse(seltaeB)`  
Formatstring: `%artist%`

### Assign Beatles to the Artist and Yellow Submarine to the Title

Source format: `Beatles - Yellow Submarine`  
Formatstring: `%artist% - %title%`

### Real-world example

Take a filename that includes track number, year, artist, title,  
track comment and reformat/assign the appropriate parts to Artist and Title

The filename is: `001 - 1969 Beatles - Yellow Submarine (Ringo Starr, vocal).m4a`

Source format: `$regexp(%_filename%,'\d+ - (\d{4}) (.+) - (.+) \((.+)\)',$2+++$3 ($1, $4))`  
Formatstring: `%ARTIST%+++%TITLE%`

**Result**  
Artist: `Beatles`  
Title: `Yellow Submarine (1969, Ringo Starr, vocal)`

### Notes

1. Because `$regexp` **REPLACES** text in the **SOURCE STRING** and returns it, this often leads you  
write expressions that match the entire string so you can extract just part of the string  
with the regex. While this can lead to long and complicated regex patterns, it also assures  
that the exact part of the string you want to extract is perfectly matched regardless of  
the regex input string value since it inherently makes the match process occur on _both_ value _and_ position.
2. Using a simple string as a delimiter that will never appear in your source makes assigning  
strings to tag fields less error prone. In the example above `+++` is as a field delimiter used between artist and title.
3. I find it much easier to work in _Actions_ vs. _Quick Actions_ when trying to refine a complicated script  
since the dialog is dismissed when you apply it and when it doesn't work for me it's faster to get back to  
where I was working in _Actions_ to fix the problem and try again.

---

<div class="post-metadata">

### Author: ![ohrenkino](https://community.mp3tag.de/user_avatar/community.mp3tag.de/ohrenkino/32/4843_2.png) [@ohrenkino](https://community.mp3tag.de/u/ohrenkino)
#### Post date: [May 6, 2023, 7:14pm UTC](https://community.mp3tag.de/t/intro-to-assigning-tag-fields-with-scripting-on-mac/60909/2 "2023-05-06T19:14:12Z")

</div>

> [@greybeard](#):
>
> ### Assign Beatles to the Title and Yellow Submarine to the Artist
> 
> Source format: `Beatles - Yellow Submarine`  
> Formatstring: `%artist% - %title%`

The headline claims something different from the actual example.  
The example assigns "Beatles" to ARTIST and "Yellow Submarine" to TITLE.

There are many ways to skin a cat - but I think that the action of the type "Format tag field" is a little easier as you can select the target field from a list of fields.  
See also the documentation:

> [@Quick Actions and Action Groups](https://community.mp3tag.de/t/quick-actions-and-action-groups/51882#format-tag-field-5):
>
> Mp3tag for Mac provides a variety of Actions, which can be used on filenames and tags. The actions can be applied individually via Actions → Quick Actions ⌥⌘ 6 or be grouped into named and reusable Action Groups via Actions → Actions ⌘ 6. Action Groups serve as repeatable workflows in Mp3tag for Mac. When using Quick Actions you can select one of the different available action types from the list. This action will be applied to the selected files and the changes are saved immediately. Tip 1…

> [@greybeard](#):
>
> I find it much easier to work in _Actions_ vs. _Quick Actions_ when trying to refine a complicated script

You are right: use the Actions if you want to keep the script definition.  
Some actions like "Replace" are one-offs in many cases, so it may not be worthwhile to save the configuration. But that depends on the use-case.

---

<div class="post-metadata">

### Author: ![greybeard](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@greybeard](https://community.mp3tag.de/u/greybeard)
#### Post date: [May 6, 2023, 8:49pm UTC](https://community.mp3tag.de/t/intro-to-assigning-tag-fields-with-scripting-on-mac/60909/3 "2023-05-06T20:49:44Z")

</div>

Thanks for catching the typo.

You're exactly right there are easier ways to assign simple fields. My issue was I had trouble understanding the examples in the forums since most were fairly complex and as a new user I didn't quite "get it" until I puzzled over various posts for an hour. For me the lights went on once I realized the simplest case was simply a static string vs. some regex I had to parse through.

After I went through (literally) these 4 examples manually I felt like the fog lifted.

Thought I'd post this to hopefully help another new user one day to stick with it and realize how powerful mp3tag is and what a great, feature rich piece of software it is.

---

<div class="post-metadata">

### Author: ![ohrenkino](https://community.mp3tag.de/user_avatar/community.mp3tag.de/ohrenkino/32/4843_2.png) [@ohrenkino](https://community.mp3tag.de/u/ohrenkino)
#### Post date: [May 29, 2023, 5:45am UTC](https://community.mp3tag.de/t/intro-to-assigning-tag-fields-with-scripting-on-mac/60909/4 "2023-05-29T05:45:13Z")

</div>

A post was split to a new topic: [How do I import actions on a Mac](https://community.mp3tag.de/t/how-do-i-import-actions-on-a-mac/61108)
