-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFractionate.jsx
97 lines (78 loc) · 4.18 KB
/
Fractionate.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Fractionate 0.4
// Lamma Studio Design
// 18-11-2024
// Changelog
// 0.1 Initial release
// 0.2 Make Spaces Great Again
// 0.3 Move Fractions group to main menu
// 0.4 Fixed typo in "Fraction Upper Last"
/////////////////////////// NOTES ///////////////////////////////
// //
// You'll need the character styles: "Fraction Upper", //
// "Fraction Upper Last", "Fraction Slash" and //
// "Fraction Lower", to be in a group called "Fractions" //
// //
// To use, type out a fraction like 11/11 and select it, //
// then run the script. It ignores spaces at either end. //
// //
/////////////////////////////////////////////////////////////////
fraction ();
function fraction(){
try {
/////////////////////////////////////////////////////////////////
///////////////////////// SET IT UP /////////////////////////////
/////////////////////////////////////////////////////////////////
// How big is your selection?
var fsize = app.selection[0].length;
// Did you forget to select something, Private Pyle? If so, whine about it and quit.
if (fsize === null | fsize == 0) {alert ("Nothing selected!"); return;};
// What have you selected?
var fract = app.selection[0].contents;
// Setup the Character Styles
var FUpStyle = app.activeDocument.characterStyleGroups.itemByName("Fractions").characterStyles.itemByName("Fraction Upper");
var FUpLastStyle = app.activeDocument.characterStyleGroups.itemByName("Fractions").characterStyles.itemByName("Fraction Upper Last");
var FSlashStyle = app.activeDocument.characterStyleGroups.itemByName("Fractions").characterStyles.itemByName("Fraction Slash");
var FLowStyle = app.activeDocument.characterStyleGroups.itemByName("Fractions").characterStyles.itemByName("Fraction Lower");
var NoStyle = app.activeDocument.allCharacterStyles[0];
// Set up some flags for the legit fraction check
var slasher = 0;
var notfrac = new Boolean(false);
/////////////////////////////////////////////////////////////////
///////////////////////// CHECK IT //////////////////////////////
/////////////////////////////////////////////////////////////////
// While we're at, is this a legit fraction? If not, whine about it and quit.
// Also ignore spaces at the beginning and the end but not anywhere else.
for (i=0; i < fsize; i++){
if (fract[i]=="/") {slasher += 1};
if ((fract[i]==" ") && (i>1) && (i<fsize)) {notfrac = !notfrac};
if ((isNaN (fract[i])) && (fract[i]!="/") && (fract[i]!=" ")){notfrac = !notfrac};
};
if (notfrac | slasher==0){alert ("Your selection is not a fraction!"); return;};
// Go through the characters until you hit "/" then write down its position
for (i=0; i < fsize; i++){
if (fract[i]=="/") {var slashpos = i;}
};
/////////////////////////////////////////////////////////////////
///////////////////////// STYLE IT //////////////////////////////
/////////////////////////////////////////////////////////////////
// Apply Fraction Upper style to everything before "/".
for (i=0; i < slashpos-1; i++){app.selection[0].characters.itemByRange(i,i).appliedCharacterStyle = FUpStyle;};
// Apply Fraction Upper Last style to the character before "/".
app.selection[0].characters.itemByRange(i, i).appliedCharacterStyle = FUpLastStyle;
// Apply Fraction Slash style to the "/".
app.selection[0].characters.itemByRange(i+1, i+1).appliedCharacterStyle = FSlashStyle;
// Apply Fraction Lower style to the stuff after "/".
for (i=slashpos+1; i < fsize; i++) {app.selection[0].characters.itemByRange(i,i).appliedCharacterStyle = FLowStyle;};
// Ignore spaces. Well, switch them back to "none" anyway.
for (i=0; i < fsize; i++){
if (fract[i]==" ") {
app.selection[0].characters.itemByRange(i,i).appliedCharacterStyle = NoStyle;
app.selection[0].characters.itemByRange(i,i).clearOverrides();
};
};
}
// if none of that works, whine about it and quit.
catch (err) {
alert ("General Error: " + [err,err.line]);
};
};