Type & Text

Recommended readings

Basics

1
// getting the character count
2
l = thisComp.layer("your text layer").text.sourceText.length;
3
​
4
// getting word count
5
l = thisComp.layer("your text layer").text.sourceText;
6
wordsCount = l.split(/ ,| | \./).length;
7
​
8
​
9
// Matching text with regex
10
thisComp.layer("text").text.sourceText.toLowerCase().match(/\w+\./))
11
​
12
// includes() β€” determines whether a string contains the characters of a specified strin
13
var str = "Hello world, welcome to the universe.";
14
var n = str.includes("world");
Copied!

Utility Functions

Find Number of Layers that includes string

1
function findLayerbyName(str) {
2
var n = thisComp.numLayers;
3
var numMattes = 0;
4
for (i = 1; i <= n; i++) {
5
thisComp.layer(i).name.includes(str) == 1 ? numMattes += 1 : 0;
6
}
7
return numMattes
8
}
9
​
10
findLayerbyName("matte")
Copied!

Text repeater

normal
vertical
1
var n = 8; // connect to a slider
2
s = myText = value ;
3
for(i=1; i<n; i++) s += myText;
4
s;
Copied!
1
n = 8; // connect to a slider
2
s = myText = value ;
3
for(i=1; i<n; i++) s += "\r" + myText;
4
s;
Copied!

Parsing text

1
// if comp name is "comp_05", to get number use this
2
thisComp.name.split("_")[1] // replace delimiter based on your needs
Copied!

Separating source text line feeds into an array

1
// Method 1
2
txt = thisComp.layer("insert Text").text.sourceText;
3
txt.split("\r")[3]; // Regex
4
​
5
// Method 2:using name as index
6
txt = thisComp.layer("insert Text").text.sourceText;
7
myIndex = thisLayer.name;
8
txt.split("\r")[myIndex];
Copied!
1
myNum = parseInt(thisComp.name.split(" ")[1],10);
Copied!

Numbers

Rounding numbers

to integer
pow
toFixed
1
// Use any of the following
2
Math.floor(value);
3
Math.ceil(value);
Copied!
1
x = time;
2
pow = Math.pow(10,5) // 5 is the number of decimals
3
x = Math.round(x*pow)/pow;
Copied!
1
// apply to sourcetext
2
x = 2 // number of decimal points
3
value.toFixed(x);
Copied!

Round up if above threshold

1
function roundSpecific(num,thres){
2
dec = num - Math.floor(num)
3
if(dec>=thres) {num = Math.ceil(num);
4
}
5
return num
6
}
7
// usage
8
roundSpecific(385.96,.9)
Copied!

Counter

1
startTime = 0
2
endTime = 2;
3
startNum = 0;
4
endNun = 10000
5
linear(time,startTime,endTime,startNum,endNum);
Copied!

Sequence of number

1
//user variables
2
var startNum = 0;
3
var maxNum = 200;
4
var numCol = 10;
5
var numZero = 2;
6
var numSpace = 1;
7
​
8
var s = "";
9
​
10
​
11
for (var i = 1; i <= maxNum; i++) {
12
// add startNum
13
var n = startNum + i - 1;
14
​
15
// add zeroes
16
for (var j = 1; j <= numZero; j++) {
17
if (n < Math.pow(10, j)) s += 0
18
}
19
// add number to string
20
s += n;
21
​
22
// add spaces
23
for (var j = 0; j < numSpace; j++) s += ' '
24
​
25
// add line break
26
i % numCol == 0 ? s += '\n' : s
27
​
28
}
Copied!

Binary

1
2
1
function binary(maxNum, col, seed) {
2
seedRandom(seed, true);
3
var s = "";
4
for (i = 1; i <= maxNum; i++) {
5
s += Math.round(random()) + ' ';
6
if (i % col == 0) s += '\n'; // add line break
7
}
8
return s
9
}
10
​
11
binary(500, 25, 1)
Copied!
1
n = 300; // connect to slider
2
seedRandom(seed,true);
3
let m = b = '';
4
for(i=0;i<n;i++){
5
seedRandom(i+seed,true);
6
b = Math.round(random(1)).toString()
7
m +=b;
8
}
Copied!

Letters

Random Alphabet

1
function genLetter() {
2
r = random(65, 90); //from a to z
3
return String.fromCharCode(r);
4
}
5
​
6
function randAlphabet(maxNum, col, seed) {
7
seedRandom(seed, true);
8
var s = "";
9
for (i = 1; i <= maxNum; i++) {
10
s += genLetter() + ' ';
11
if (i % col == 0) s += '\n'; // add line break
12
}
13
return s
14
}
15
randAlphabet(500, 25, 1)
Copied!

Dates

Current date

1
D = new Date(Date(0));
2
β€œβ€ + D.getDate() + β€œ/” + (D.getMonth()+1) + β€œ/” + D.getFullYear()
3
​
4
// get year
5
D = new Date(Date(0)); D.getFullYear()
6
​
7
// get month
8
D = new Date(Date(0)); D.getMonth()+1
9
​
10
// get day
11
D = new Date(Date(0)); D.getDate()
Copied!

Expressions selector

Default

1
selectorValue * textIndex/textTotal
Copied!

%

1
if(textIndex%2) 0; else 100
Copied!

​Random position from textIndex

1
seedRandom(textIndex,true);
2
random([100,100]);
Copied!

Select text indexes explicitly; e.g. 1, 3, 7, 14, 22, etc.?

1
If you don't have a lot of numbers, then this works:
2
​
3
if(textIndex === 1 || textIndex ===3) {
4
100;
5
} else {
6
0;
7
}
8
​
9
If you do have a ton of numbers, then this works:
10
​
11
i = [1,3,5,7,10,11];
12
inArray(i);
13
​
14
function inArray(obj) {
15
b = 0;
16
for(i = 0; i < obj.length; i++) {
17
if(obj[i] === textIndex) {
18
b = 100;
19
break;
20
}
21
}
22
return b;
23
}
Copied!

Sin wave effector [source]

1
freq = .5;
2
selectorValue*Math.sin(time*freq*Math.PI*2 + Math.PI*2* textIndex/textTotal)
Copied!

Expression Selector (Regex)

1
https://forums.creativecow.net/thread/227/41461
2
Oleg Pirogov
3
https://dribbble.com/zeplin
Copied!
1
​
2
var t = text.sourceText;
3
var regex = /\d[a-z]+(?=,)/g;
4
​
5
function indexMatched(index){
6
var matched = false;
7
var match;
8
var i=[];
9
​
10
while ((match = regex.exec(t)) !== null){
11
matched |= match.index<index & index<match.index+match[0].length;
12
i.push([match, match.index, match.index+match[0].length]);
13
}
14
return matched;
15
}
16
​
17
indexMatched(textIndex-1) ? 100 : 0;
Copied!
1
var t = text.sourceText;
2
var regex = /\(([^)]+)(?=\))/g;
3
​
4
function indexMatched(index){
5
var matched = false;
6
var match;
7
​
8
while ((match = regex.exec(t)) !== null){
9
matched |= match.index<index & index<match.index+match[0].length;
10
}
11
return matched;
12
}
13
​
14
indexMatched(textIndex-1) ? 100 : 0;
15
​
Copied!

Hexcode

1
var t = text.sourceText;
2
var regex = /[A-Fa-f0-9]{6}/g;
3
​
4
function indexMatched(index){
5
var matched = false;
6
var match;
7
​
8
while ((match = regex.exec(t)) !== null){
9
matched |= match.index<index & index<=match.index+match[0].length;
10
}
11
return matched;
12
}
13
​
14
indexMatched(textIndex) ? 100 : 0;
Copied!
Last modified 3mo ago